Skip to content

Commit

Permalink
fix(@ngtools/json-schema): values of non-existent objects should retu…
Browse files Browse the repository at this point in the history
…rn undefined (#4300)

Also, we verify the value is null and return that instead if thats the case.
  • Loading branch information
hansl authored Jan 31, 2017
1 parent 8e82d17 commit 95f28fd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 8 additions & 0 deletions packages/@ngtools/json-schema/src/schema-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ describe('@ngtools/json-schema', () => {
proto.oneOfKey2 = 'hello';
expect(proto.oneOfKey2 instanceof Array).toBe(false);
});

it('returns undefined for values that are non-existent', () => {
const proto: any = Object.create(null);
const root = new RootSchemaTreeNode(proto, { value: valueJson, schema: schemaJson });

const value = root.children['objectKey1'].children['objectKey'].children['stringKey'].get();
expect(value).toBe(undefined);
});
});


Expand Down
8 changes: 5 additions & 3 deletions packages/@ngtools/json-schema/src/schema-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,12 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
if (!this.defined && this._forward) {
return this._forward.get();
}
if (!this.defined && this._default !== undefined) {
return this._default;
if (!this.defined) {
return this._default !== undefined ? this._default : undefined;
}
return this._value === undefined ? undefined : this.convert(this._value);
return this._value === undefined
? undefined
: (this._value === null ? null : this.convert(this._value));
}
set(v: T, force = false) {
if (this.readOnly && !force) {
Expand Down

0 comments on commit 95f28fd

Please sign in to comment.