Skip to content

Commit

Permalink
fix: Fixes 'exludeFromRead' for nested paths where object does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Huysamen authored and sebelga committed Dec 4, 2018
1 parent ad085c4 commit f7c336c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/serializers/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ function fromDatastore(entity, options = {}) {
v = v[segments.shift()];
}

delete v[segments.pop()];
const segment = segments.pop();

if (typeof v !== 'undefined' && segment in v) {
delete v[segment];
}
});
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/entity-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,28 @@ describe('Entity', () => {
expect(plain.embedded.prop4).equal('4');
});

it('should ignore incorrectly specified nested embedded object property paths', () => {
schema = new Schema({
embedded: { excludeFromRead: ['prop3.p1.p1', 'prop4', 'prop5.p1'] },
});

ModelInstance = gstore.model('HasEmbedded', schema);

const entity = new ModelInstance({
embedded: {
prop1: '1',
prop2: { p1: { p2: 'p2' } },
prop3: { p1: { p2: { p3: 'p3' } } },
},
});

const plain = entity.plain({});

expect(plain.embedded.prop1).equal('1');
expect(plain.embedded.prop2.p1.p2).equal('p2');
expect(plain.embedded.prop3.p1.p2.p3).equal('p3');
});

it('should not clear nested embedded object excluded properties when specifying readAll: true', () => {
schema = new Schema({
embedded: { excludeFromRead: ['prop1', 'prop2.p1', 'prop3.p1.p11'] },
Expand Down

0 comments on commit f7c336c

Please sign in to comment.