Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular/cli): avoid implicit undefined array defaults for @angular/build builders #29960

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions goldens/public-api/angular_devkit/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { ValidateFunction } from 'ajv';
// @public (undocumented)
function addUndefinedDefaults(value: JsonValue, _pointer: JsonPointer, schema?: JsonSchema): JsonValue;

// @public (undocumented)
function addUndefinedObjectDefaults(value: JsonValue, _pointer: JsonPointer, schema?: JsonSchema): JsonValue;

// @public
class AliasHost<StatsT extends object = {}> extends ResolverHost<StatsT> {
// (undocumented)
Expand Down Expand Up @@ -1297,6 +1300,7 @@ class TransformLogger extends Logger {

declare namespace transforms {
export {
addUndefinedObjectDefaults,
addUndefinedDefaults
}
}
Expand Down
6 changes: 5 additions & 1 deletion modules/testing/builder/src/builder-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ export class BuilderHarness<T> {
...builderInfo,
};

this.schemaRegistry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
if (builderInfo?.builderName?.startsWith('@angular/build:')) {
this.schemaRegistry.addPostTransform(json.schema.transforms.addUndefinedObjectDefaults);
} else {
this.schemaRegistry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
}
}

private resolvePath(path: string): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Schema } from '../schema';
export * from '../../../../../../../modules/testing/builder/src';

export const APPLICATION_BUILDER_INFO = Object.freeze({
name: '@angular-devkit/build-angular:application',
name: '@angular/build:application',
schemaPath: __dirname + '/../schema.json',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,16 @@ export abstract class ArchitectBaseCommandModule<T extends object>
return this.onMissingTarget(e.message);
}

const isAngularBuild = builderName.startsWith('@angular/build:');

const { logger } = this.context;
const run = await this.getArchitect().scheduleTarget(target, options as json.JsonObject, {
logger,
});
const run = await this.getArchitect(isAngularBuild).scheduleTarget(
target,
options as json.JsonObject,
{
logger,
},
);

const analytics = isPackageNameSafeForAnalytics(builderName)
? await this.getAnalytics()
Expand Down Expand Up @@ -150,13 +156,17 @@ export abstract class ArchitectBaseCommandModule<T extends object>
}

private _architect: Architect | undefined;
protected getArchitect(): Architect {
protected getArchitect(skipUndefinedArrayTransform: boolean): Architect {
if (this._architect) {
return this._architect;
}

const registry = new json.schema.CoreSchemaRegistry();
registry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
if (skipUndefinedArrayTransform) {
registry.addPostTransform(json.schema.transforms.addUndefinedObjectDefaults);
} else {
registry.addPostTransform(json.schema.transforms.addUndefinedDefaults);
}
registry.useXDeprecatedProvider((msg) => this.context.logger.warn(msg));

const architectHost = this.getArchitectHost();
Expand Down
21 changes: 19 additions & 2 deletions packages/angular_devkit/core/src/json/schema/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ import { JsonPointer } from './interface';
import { JsonSchema } from './schema';
import { getTypesOfSchema } from './utility';

export function addUndefinedObjectDefaults(
value: JsonValue,
_pointer: JsonPointer,
schema?: JsonSchema,
): JsonValue {
return transformUndefined(value, _pointer, schema, true);
}

export function addUndefinedDefaults(
value: JsonValue,
_pointer: JsonPointer,
schema?: JsonSchema,
): JsonValue {
return transformUndefined(value, _pointer, schema, false);
}

function transformUndefined(
value: JsonValue,
_pointer: JsonPointer,
schema?: JsonSchema,
onlyObjects?: boolean,
): JsonValue {
if (typeof schema === 'boolean' || schema === undefined) {
return value;
Expand Down Expand Up @@ -45,7 +62,7 @@ export function addUndefinedDefaults(
return value;
}

if (type === 'array') {
if (!onlyObjects && type === 'array') {
return value == undefined ? [] : value;
}

Expand Down Expand Up @@ -94,7 +111,7 @@ export function addUndefinedDefaults(
});

if (adjustedSchema && isJsonObject(adjustedSchema)) {
newValue[propName] = addUndefinedDefaults(value, _pointer, adjustedSchema);
newValue[propName] = transformUndefined(value, _pointer, adjustedSchema, onlyObjects);
}
}
}
Expand Down