Skip to content

Commit 5c0ddbc

Browse files
fix: not check sub-property existence for null values (#1330)
Co-authored-by: danieljbruce <[email protected]>
1 parent ea17528 commit 5c0ddbc

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/entity.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,8 @@ export namespace entity {
819819
return entityProto;
820820

821821
function excludePathFromEntity(entity: EntityProto, path: string) {
822+
if (!entity) return;
823+
822824
const arrayIndex = path.indexOf('[]');
823825
const entityIndex = path.indexOf('.');
824826
const wildcardIndex = path.indexOf('.*');
@@ -905,6 +907,7 @@ export namespace entity {
905907
isFirstPathPartDefined
906908
) {
907909
const array = entity.properties![firstPathPart].arrayValue;
910+
if (!array) return;
908911
// eslint-disable-next-line @typescript-eslint/no-explicit-any
909912
array.values.forEach((value: any) => {
910913
if (value.entityValue) {

test/entity.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,48 @@ describe('entity', () => {
11161116
expectedEntityProto
11171117
);
11181118
});
1119+
1120+
it('should not throw when `null` value is supplied for a field with an entity/array index exclusion', () => {
1121+
const entityObject = {
1122+
excludeFromIndexes: [
1123+
'entityCompletelyExcluded.*',
1124+
'entityPropertyExcluded.name',
1125+
'entityArrayCompletelyExcluded[].*',
1126+
'entityArrayPropertyExcluded[].name',
1127+
],
1128+
1129+
data: {
1130+
entityCompletelyExcluded: null,
1131+
entityPropertyExcluded: null,
1132+
entityArrayCompletelyExcluded: null,
1133+
entityArrayPropertyExcluded: null,
1134+
},
1135+
};
1136+
1137+
const expectedEntityProto = {
1138+
key: null,
1139+
properties: {
1140+
entityCompletelyExcluded: {
1141+
nullValue: 0,
1142+
excludeFromIndexes: true,
1143+
},
1144+
entityPropertyExcluded: {
1145+
nullValue: 0,
1146+
},
1147+
entityArrayCompletelyExcluded: {
1148+
nullValue: 0,
1149+
},
1150+
entityArrayPropertyExcluded: {
1151+
nullValue: 0,
1152+
},
1153+
},
1154+
};
1155+
1156+
assert.deepStrictEqual(
1157+
testEntity.entityToEntityProto(entityObject),
1158+
expectedEntityProto
1159+
);
1160+
});
11191161
});
11201162

11211163
describe('formatArray', () => {

0 commit comments

Comments
 (0)