Skip to content

Commit 7c65e56

Browse files
committed
feat(@angular-devkit/core): add support for schema in template
Before we were messing with ownKeys to only return defined properties, but that has the side effect that those will not work inside a "with() {}" clause, which is what templates are using. We now need a serialize symbol if we want to only serialize defined properties, but for now parity with @ngtools/json-schema is not a priority.
1 parent ae9cc16 commit 7c65e56

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

packages/angular_devkit/core/src/json/schema/serializers/templates/javascript/prop-object.ejs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ const handlers = Object.create(null);
3737
}
3838
%>
3939

40+
const objectFunctions = {
41+
hasOwnProperty(name) { return objectProxyHandler.has(null, name); },
42+
};
43+
4044

4145
let defined = false;
42-
const proxy = new Proxy({}, {
46+
const objectProxyHandler = {
4347
isExtensible() { return false; },
4448
has(target, prop) {
4549
return (prop in handlers && handlers[prop].isDefined())
@@ -54,6 +58,9 @@ const proxy = new Proxy({}, {
5458
if (prop in handlers) {
5559
return handlers[prop].get();
5660
}
61+
if (prop in objectFunctions) {
62+
return objectFunctions[prop];
63+
}
5764
return undefined;
5865
},
5966
set(target, prop, v) {
@@ -90,15 +97,20 @@ const proxy = new Proxy({}, {
9097
getOwnPropertyDescriptor(target, prop) {
9198
if (prop in handlers) {
9299
return { configurable: true, enumerable: true };
100+
} else if (additionalPropertyHandler && prop in additionalPropertyHandler) {
101+
return { configurable: true, enumerable: true };
93102
}
94103
},
95104
ownKeys(target) {
96105
return [].concat(
97-
Object.keys(handlers).filter(function(key) { return handlers[key].isDefined(); }),
106+
Object.keys(handlers),
98107
additionalPropertyHandler ? Object.keys(additionalProperties) : []
99108
);
100109
},
101-
});
110+
};
111+
112+
113+
const proxy = new Proxy({}, objectProxyHandler);
102114

103115
const objectHandler = {
104116
set(v) {

tests/@angular_devkit/core/json/schema/serializers/1.0.javascript_spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function works(registry: schema.JsonSchemaRegistry, schema: any) {
3636
v.objectKey1.objectKey = { stringKey: 'str2' };
3737
expect(v.objectKey1.objectKey.stringKey).toBe('str2');
3838

39-
expect(Object.keys(v.objectKey1)).toEqual(['stringKey', 'objectKey']);
39+
expect(Object.keys(v.objectKey1)).toEqual(['stringKey', 'stringKeyDefault', 'objectKey']);
4040
}
4141

4242

@@ -55,9 +55,8 @@ export function accessUndefined(registry: schema.JsonSchemaRegistry, schema: any
5555
expect(v.objectKey1).not.toBe(undefined);
5656
expect(v.objectKey1.stringKey).toBe('hello');
5757
expect(v.objectKey1.numberKey).toBe(undefined);
58-
expect(v).toEqual({ 'requiredKey': 1, 'objectKey1': { 'stringKey': 'hello' } });
5958
v.objectKey1.stringKey = undefined;
60-
expect(v).toEqual({ 'requiredKey': 1 });
59+
expect(v.objectKey1.stringKey).toBe(undefined);
6160

6261
expect(v.stringKeyDefault).toBe('defaultValue');
6362
expect(v.objectKey1.stringKeyDefault).toBe('defaultValue2');

0 commit comments

Comments
 (0)