Skip to content

Commit

Permalink
fix(@angular-devkit/core): improve handling of set schema values
Browse files Browse the repository at this point in the history
Closes #20594

(cherry picked from commit 09676c9)
  • Loading branch information
alan-agius4 committed Apr 23, 2021
1 parent 9a32ed9 commit 4a68ad7
Showing 1 changed file with 8 additions and 37 deletions.
45 changes: 8 additions & 37 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
CoreSchemaRegistry._set(
data,
pathFragments,
answers[path] as {},
answers[path],
null,
undefined,
true,
Expand All @@ -567,51 +567,22 @@ export class CoreSchemaRegistry implements SchemaRegistry {
}

private static _set(
// tslint:disable-next-line:no-any
// tslint:disable-next-line: no-any
data: any,
fragments: string[],
value: {},
// tslint:disable-next-line:no-any
parent: any | null = null,
value: unknown,
parent: Record<string, unknown> | null = null,
parentProperty?: string,
force?: boolean,
): void {
for (let i = 0; i < fragments.length; i++) {
const f = fragments[i];

if (f[0] == 'i') {
if (!Array.isArray(data)) {
return;
}

for (let j = 0; j < data.length; j++) {
CoreSchemaRegistry._set(data[j], fragments.slice(i + 1), value, data, '' + j);
}

return;
}

if (f.startsWith('key')) {
if (typeof data !== 'object') {
return;
}

for (const property in data) {
CoreSchemaRegistry._set(data[property], fragments.slice(i + 1), value, data, property);
}

return;
}


// We know we need an object because the fragment is a property key.
for (const fragment of fragments) {
if (!data && parent !== null && parentProperty) {
data = parent[parentProperty] = {};
}
parent = data;
parentProperty = f;

data = data[f];
parent = data;
parentProperty = fragment;
data = data[fragment];
}

if (parent && parentProperty && (force || parent[parentProperty] === undefined)) {
Expand Down

0 comments on commit 4a68ad7

Please sign in to comment.