Skip to content

Commit 95f28fd

Browse files
authored
fix(@ngtools/json-schema): values of non-existent objects should return undefined (angular#4300)
Also, we verify the value is null and return that instead if thats the case.
1 parent 8e82d17 commit 95f28fd

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/@ngtools/json-schema/src/schema-tree.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ describe('@ngtools/json-schema', () => {
2727
proto.oneOfKey2 = 'hello';
2828
expect(proto.oneOfKey2 instanceof Array).toBe(false);
2929
});
30+
31+
it('returns undefined for values that are non-existent', () => {
32+
const proto: any = Object.create(null);
33+
const root = new RootSchemaTreeNode(proto, { value: valueJson, schema: schemaJson });
34+
35+
const value = root.children['objectKey1'].children['objectKey'].children['stringKey'].get();
36+
expect(value).toBe(undefined);
37+
});
3038
});
3139

3240

packages/@ngtools/json-schema/src/schema-tree.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,12 @@ export abstract class LeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
409409
if (!this.defined && this._forward) {
410410
return this._forward.get();
411411
}
412-
if (!this.defined && this._default !== undefined) {
413-
return this._default;
412+
if (!this.defined) {
413+
return this._default !== undefined ? this._default : undefined;
414414
}
415-
return this._value === undefined ? undefined : this.convert(this._value);
415+
return this._value === undefined
416+
? undefined
417+
: (this._value === null ? null : this.convert(this._value));
416418
}
417419
set(v: T, force = false) {
418420
if (this.readOnly && !force) {

0 commit comments

Comments
 (0)