Skip to content

Commit 9a3f17f

Browse files
committed
fix(@angular-devkit/core): fix smart defaults on arrays
By adding undefined and empty arrays everywhere were preventing smart defaults to work on arrays. Also, when ending with an array or object, smart defaults path will have an extra key.
1 parent d66602e commit 9a3f17f

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

packages/angular_devkit/core/src/json/schema/registry.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
187187
}),
188188
switchMap(([data, valid]) => {
189189
if (valid) {
190-
let dataObs = observableOf(data);
190+
let dataObs = this._applySmartDefaults(data);
191191
this._post.forEach(visitor =>
192192
dataObs = dataObs.pipe(
193193
concatMap(data => {
@@ -203,8 +203,6 @@ export class CoreSchemaRegistry implements SchemaRegistry {
203203
);
204204

205205
return dataObs.pipe(
206-
// Apply smart defaults.
207-
concatMap(data => this._applySmartDefaults(data)),
208206
map(data => [data, valid]),
209207
);
210208
} else {
@@ -280,7 +278,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
280278
// We cheat, heavily.
281279
this._smartDefaultRecord.set(
282280
// tslint:disable-next-line:no-any
283-
JSON.stringify((it as any).dataPathArr.slice(1) as string[]),
281+
JSON.stringify((it as any).dataPathArr.slice(1, (it as any).dataLevel + 1) as string[]),
284282
schema,
285283
);
286284

@@ -334,8 +332,14 @@ export class CoreSchemaRegistry implements SchemaRegistry {
334332
.replace(/\\r/g, '\r')
335333
.replace(/\\f/g, '\f')
336334
.replace(/\\t/g, '\t');
335+
336+
// We know we need an object because the fragment is a property key.
337+
if (!data && parent !== null && parentProperty) {
338+
data = parent[parentProperty] = {};
339+
}
337340
parent = data;
338341
parentProperty = property;
342+
339343
data = data[property];
340344
} else {
341345
return;

packages/angular_devkit/core/src/json/schema/registry_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ describe('CoreSchemaRegistry', () => {
273273

274274
return schema['blue'];
275275
});
276+
registry.addSmartDefaultProvider('test3', (schema) => {
277+
return [ 1, 2, 3 ];
278+
});
276279

277280
registry
278281
.compile({
@@ -289,6 +292,20 @@ describe('CoreSchemaRegistry', () => {
289292
},
290293
},
291294
},
295+
arr2: {
296+
$ref: '#/definitions/test3',
297+
},
298+
obj: {
299+
properties: {
300+
deep: {
301+
properties: {
302+
arr: {
303+
$ref: '#/definitions/test3',
304+
},
305+
},
306+
},
307+
},
308+
},
292309
},
293310
definitions: {
294311
example: {
@@ -304,6 +321,12 @@ describe('CoreSchemaRegistry', () => {
304321
blue: 'yep',
305322
},
306323
},
324+
test3: {
325+
type: 'array',
326+
$default: {
327+
$source: 'test3',
328+
},
329+
},
307330
},
308331
})
309332
.pipe(
@@ -312,6 +335,8 @@ describe('CoreSchemaRegistry', () => {
312335
expect(result.success).toBe(true);
313336
expect(data.bool).toBe(true);
314337
expect(data.arr[0].test).toBe('yep');
338+
expect(data.arr2).toEqual([1, 2, 3]);
339+
expect(data.obj.deep.arr).toEqual([1, 2, 3]);
315340
}),
316341
)
317342
.subscribe(done, done.fail);

0 commit comments

Comments
 (0)