Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check property existence for excludeFromIndexes with wildcard #788

Closed
45 changes: 25 additions & 20 deletions src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ export namespace entity {
if (
firstPathPartIsArray &&
// check also if the property in question is actually an array value.
entity.properties![firstPathPart] &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no harm in this. If we don't check for this and entity.properties![firstPathPart] ever happens to be undefined then this will throw an error and I can't imagine that being desirable.

entity.properties![firstPathPart].arrayValue &&
// check if wildcard is not applied
!hasWildCard
Expand All @@ -882,15 +883,17 @@ export namespace entity {
}
});
} else if (firstPathPartIsArray && hasWildCard && remainderPath === '*') {
const array = entity.properties![firstPathPart].arrayValue;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
array.values.forEach((value: any) => {
if (value.entityValue) {
excludePathFromEntity(value.entityValue, '.*');
} else {
excludePathFromEntity(value, '');
}
});
if (entity.properties![firstPathPart]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we just check to see that this property is defined before we move further as that prevents the error from being thrown. Makes sense. There seems to be no value in further traversing a path that the entity will not have any corresponding properties for.

const array = entity.properties![firstPathPart].arrayValue;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
array.values.forEach((value: any) => {
if (value.entityValue) {
excludePathFromEntity(value.entityValue, '.*');
} else {
excludePathFromEntity(value, '');
}
});
}
} else if (firstPathPartIsEntity) {
if (firstPathPart === '') {
Object.keys(entity.properties!).forEach(path => {
Expand All @@ -901,17 +904,19 @@ export namespace entity {
});
} else {
if (hasWildCard && remainderPath === '*') {
const parentEntity = entity.properties![firstPathPart].entityValue;

if (parentEntity) {
Object.keys(parentEntity.properties).forEach(path => {
const newPath = parentEntity.properties[path].arrayValue
? path + '[].*'
: path + '.*';
excludePathFromEntity(parentEntity, newPath);
});
} else {
excludePathFromEntity(entity, firstPathPart);
if (entity.properties![firstPathPart]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we just check to see that this property is defined before we move further as that prevents the error from being thrown. Makes sense. There seems to be no value in further traversing a path that the entity will not have any corresponding properties for.

const parentEntity =
entity.properties![firstPathPart].entityValue;
if (parentEntity) {
Object.keys(parentEntity.properties).forEach(path => {
const newPath = parentEntity.properties[path].arrayValue
? path + '[].*'
: path + '.*';
excludePathFromEntity(parentEntity, newPath);
});
} else {
excludePathFromEntity(entity, firstPathPart);
}
}
} else {
const parentEntity = entity.properties![firstPathPart].entityValue;
Expand Down
2 changes: 2 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,8 @@ describe('Datastore', () => {
'metadata.otherProperty',
'metadata.obj.*',
'metadata.longStringArray[].*',
'undefinedData.*',
'undefinedArray[].*',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't modify the existing test. We should make a new test that tests for these specific values so we don't lose any existing test coverage.

],
},
assert.ifError
Expand Down