From 5c0ddbca91c41e056443eb0b60449f3cdddd6e69 Mon Sep 17 00:00:00 2001 From: David Nagy Date: Wed, 20 Nov 2024 20:47:09 +0100 Subject: [PATCH] fix: not check sub-property existence for `null` values (#1330) Co-authored-by: danieljbruce --- src/entity.ts | 3 +++ test/entity.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/entity.ts b/src/entity.ts index 0437e239..c12e4ee9 100644 --- a/src/entity.ts +++ b/src/entity.ts @@ -819,6 +819,8 @@ export namespace entity { return entityProto; function excludePathFromEntity(entity: EntityProto, path: string) { + if (!entity) return; + const arrayIndex = path.indexOf('[]'); const entityIndex = path.indexOf('.'); const wildcardIndex = path.indexOf('.*'); @@ -905,6 +907,7 @@ export namespace entity { isFirstPathPartDefined ) { const array = entity.properties![firstPathPart].arrayValue; + if (!array) return; // eslint-disable-next-line @typescript-eslint/no-explicit-any array.values.forEach((value: any) => { if (value.entityValue) { diff --git a/test/entity.ts b/test/entity.ts index e4b738ca..88b4c1d1 100644 --- a/test/entity.ts +++ b/test/entity.ts @@ -1116,6 +1116,48 @@ describe('entity', () => { expectedEntityProto ); }); + + it('should not throw when `null` value is supplied for a field with an entity/array index exclusion', () => { + const entityObject = { + excludeFromIndexes: [ + 'entityCompletelyExcluded.*', + 'entityPropertyExcluded.name', + 'entityArrayCompletelyExcluded[].*', + 'entityArrayPropertyExcluded[].name', + ], + + data: { + entityCompletelyExcluded: null, + entityPropertyExcluded: null, + entityArrayCompletelyExcluded: null, + entityArrayPropertyExcluded: null, + }, + }; + + const expectedEntityProto = { + key: null, + properties: { + entityCompletelyExcluded: { + nullValue: 0, + excludeFromIndexes: true, + }, + entityPropertyExcluded: { + nullValue: 0, + }, + entityArrayCompletelyExcluded: { + nullValue: 0, + }, + entityArrayPropertyExcluded: { + nullValue: 0, + }, + }, + }; + + assert.deepStrictEqual( + testEntity.entityToEntityProto(entityObject), + expectedEntityProto + ); + }); }); describe('formatArray', () => {