From 476ae0e06973a220da6dc8476321b98369197b8a Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Fri, 30 Oct 2026 12:59:53 +0000 Subject: [PATCH 1/2] fix(@angular/cli): support array options defined in oneOf schemas Previously, `getSupportedTypes` in `json-schema.ts` only checked top-level `items` when determining if an `array` type in a JSON schema was supported on the CLI. Options like `allowedHosts` in `@angular/build:dev-server`, which define their `array` schema inside a `oneOf` alongside `boolean`, had their `array` type filtered out and were registered in Yargs as `boolean` only. Additionally, when an option supports both `array` (or `string`) and `boolean`, Yargs parses bare flags (e.g. `--allowed-hosts`) as `[]` and `--allowed-hosts=true` as `['true']`. This change checks `oneOf`/`anyOf` subschemas for supported array items and coerces empty or boolean-like values to booleans when `boolean` is also a supported type. Closes #33955 --- .../build/src/builders/dev-server/schema.json | 1 + .../command-builder/utilities/json-schema.ts | 81 ++++++++++++++++++- .../utilities/json-schema_spec.ts | 58 +++++++++++++ 3 files changed, 138 insertions(+), 2 deletions(-) diff --git a/packages/angular/build/src/builders/dev-server/schema.json b/packages/angular/build/src/builders/dev-server/schema.json index 667bde5692da..5d4cc971a1fc 100644 --- a/packages/angular/build/src/builders/dev-server/schema.json +++ b/packages/angular/build/src/builders/dev-server/schema.json @@ -48,6 +48,7 @@ } }, { + "const": true, "type": "boolean", "description": "Indicates that all hosts are allowed. This is not recommended and a security risk." } diff --git a/packages/angular/cli/src/command-builder/utilities/json-schema.ts b/packages/angular/cli/src/command-builder/utilities/json-schema.ts index 78218465cf02..0cf3bf617a07 100644 --- a/packages/angular/cli/src/command-builder/utilities/json-schema.ts +++ b/packages/angular/cli/src/command-builder/utilities/json-schema.ts @@ -50,6 +50,11 @@ export interface Option extends YargsOptions { * Type of the values in a key/value pair field. */ itemValueType?: 'string'; + + /** + * The supported types of the option. + */ + types?: ReadonlyArray<'string' | 'number' | 'boolean' | 'array' | 'object'>; } /** @@ -82,6 +87,41 @@ function checkStringMap(keyValuePairOptions: Set, args: Arguments): bool return true; } +/** + * A Yargs coerce function that converts empty or boolean-like values to a boolean + * when an option supports both `boolean` and another type (such as `array` or `string`). + * @param value The parsed argument value. + * @returns A boolean if the value represents a boolean flag, otherwise the original value. + */ +function coerceToBoolean(value: unknown): unknown { + if (typeof value === 'string') { + if (value === '' || value === 'true') { + return true; + } + + if (value === 'false') { + return false; + } + } else if (Array.isArray(value)) { + if (value.length === 0) { + return true; + } + + if (value.length === 1) { + const firstValue = value[0]; + if (typeof firstValue === 'boolean') { + return firstValue; + } + + if (firstValue === 'true' || firstValue === 'false') { + return firstValue === 'true'; + } + } + } + + return value; +} + /** * A Yargs coerce function that converts an array of `key=value` strings to an object. * @param value An array of `key=value` strings. @@ -177,6 +217,34 @@ function isSupportedArrayItemSchema(schema: json.JsonObject): boolean { return false; } +/** + * Checks if a JSON schema node represents a supported array schema, + * either directly or within `oneOf`/`anyOf` subschemas. + * @param schema The JSON schema node to check. + * @returns `true` if the schema represents a supported array, otherwise `false`. + */ +function isSupportedArraySchema(schema: json.JsonObject): boolean { + if (isJsonObject(schema.items) && isSupportedArrayItemSchema(schema.items)) { + return true; + } + + if ( + json.isJsonArray(schema.oneOf) && + schema.oneOf.some((item) => isJsonObject(item) && isSupportedArraySchema(item)) + ) { + return true; + } + + if ( + json.isJsonArray(schema.anyOf) && + schema.anyOf.some((item) => isJsonObject(item) && isSupportedArraySchema(item)) + ) { + return true; + } + + return false; +} + /** * Gets the supported types for a JSON schema node. * @param current The JSON schema node to get the supported types for. @@ -198,7 +266,7 @@ function getSupportedTypes( case 'string': return true; case 'array': - return isJsonObject(current.items) && isSupportedArrayItemSchema(current.items); + return isSupportedArraySchema(current); case 'object': return isStringMap(current); default: @@ -372,6 +440,7 @@ export async function parseJsonSchemaToOptions( typeof current['x-user-analytics'] === 'string' ? current['x-user-analytics'] : undefined, deprecated: xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined, positional, + types, ...(type === 'object' ? { type: 'array', @@ -424,6 +493,7 @@ export function addSchemaOptionsToCommand( alias, userAnalytics, type, + types, itemValueType, hidden, name, @@ -442,13 +512,20 @@ export function addSchemaOptionsToCommand( keyValuePairOptions.add(dashedName); } + let coerce: YargsOptions['coerce']; + if (itemValueType) { + coerce = coerceToStringMap; + } else if (type !== 'boolean' && types?.includes('boolean')) { + coerce = coerceToBoolean; + } + const sharedOptions: YargsOptions & PositionalOptions = { alias, hidden, description, deprecated, choices, - coerce: itemValueType ? coerceToStringMap : undefined, + coerce, // This should only be done when `--help` is used otherwise default will override options set in angular.json. ...(includeDefaultValues ? { default: defaultVal } : {}), }; diff --git a/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts b/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts index 117b325f26b1..dfe4c679ca8f 100644 --- a/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts +++ b/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts @@ -116,6 +116,20 @@ describe('parseJsonSchemaToOptions', () => { ], }, }, + 'allowedHosts': { + 'default': [], + 'oneOf': [ + { + 'type': 'array', + 'items': { + 'type': 'string', + }, + }, + { + 'type': 'boolean', + }, + ], + }, }, }; @@ -231,6 +245,50 @@ describe('parseJsonSchemaToOptions', () => { }); }); + describe('type=array in oneOf', () => { + it('parses valid option value when specified once', async () => { + expect(await parse(['--allowed-hosts', 'a.example.com', 'b.example.com'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': ['a.example.com', 'b.example.com'], + }), + ); + }); + + it('parses valid option value when specified multiple times', async () => { + expect( + await parse(['--allowed-hosts', 'a.example.com', '--allowed-hosts', 'b.example.com']), + ).toEqual( + jasmine.objectContaining({ + 'allowedHosts': ['a.example.com', 'b.example.com'], + }), + ); + }); + + it('parses as boolean true when specified without values', async () => { + expect(await parse(['--allowed-hosts'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': true, + }), + ); + }); + + it('parses as boolean true when specified with =true', async () => { + expect(await parse(['--allowed-hosts=true'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': true, + }), + ); + }); + + it('parses as boolean false when specified with =false', async () => { + expect(await parse(['--allowed-hosts=false'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': false, + }), + ); + }); + }); + describe('type=string, enum', () => { it('parses valid option value', async () => { expect(await parse(['--ssr', 'never'])).toEqual( From f2dbf33904a01673094d0ccf8882c499c03226b8 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:29:29 +0000 Subject: [PATCH 2/2] fixup! fix(@angular/cli): support array options defined in oneOf schemas --- .../build/src/builders/dev-server/schema.json | 1 - .../command-builder/utilities/json-schema.ts | 140 +++++++++++++++--- .../utilities/json-schema_spec.ts | 133 +++++++++++++++++ 3 files changed, 250 insertions(+), 24 deletions(-) diff --git a/packages/angular/build/src/builders/dev-server/schema.json b/packages/angular/build/src/builders/dev-server/schema.json index 5d4cc971a1fc..667bde5692da 100644 --- a/packages/angular/build/src/builders/dev-server/schema.json +++ b/packages/angular/build/src/builders/dev-server/schema.json @@ -48,7 +48,6 @@ } }, { - "const": true, "type": "boolean", "description": "Indicates that all hosts are allowed. This is not recommended and a security risk." } diff --git a/packages/angular/cli/src/command-builder/utilities/json-schema.ts b/packages/angular/cli/src/command-builder/utilities/json-schema.ts index 0cf3bf617a07..95b51bee3669 100644 --- a/packages/angular/cli/src/command-builder/utilities/json-schema.ts +++ b/packages/angular/cli/src/command-builder/utilities/json-schema.ts @@ -55,6 +55,11 @@ export interface Option extends YargsOptions { * The supported types of the option. */ types?: ReadonlyArray<'string' | 'number' | 'boolean' | 'array' | 'object'>; + + /** + * Allowed boolean values when `boolean` is one of the supported types. + */ + booleanValues?: ReadonlySet; } /** @@ -87,39 +92,70 @@ function checkStringMap(keyValuePairOptions: Set, args: Arguments): bool return true; } +const ALL_BOOLEAN_VALUES: ReadonlySet = new Set([true, false]); + /** - * A Yargs coerce function that converts empty or boolean-like values to a boolean - * when an option supports both `boolean` and another type (such as `array` or `string`). - * @param value The parsed argument value. - * @returns A boolean if the value represents a boolean flag, otherwise the original value. + * Coerces a scalar string value to a boolean when permitted by the schema's allowed boolean values. */ -function coerceToBoolean(value: unknown): unknown { +function coerceStringToBoolean(value: unknown, allowTrue: boolean, allowFalse: boolean): unknown { if (typeof value === 'string') { - if (value === '' || value === 'true') { + if ((value === '' || value === 'true') && allowTrue) { return true; } - if (value === 'false') { + if (value === 'false' && allowFalse) { return false; } - } else if (Array.isArray(value)) { - if (value.length === 0) { - return true; + } + + return value; +} + +/** + * Creates a Yargs coerce function that converts empty or boolean-like values to a boolean + * when an option supports both `boolean` and another type (`array` or `string`). + * @param type The primary Yargs option type (`array` or `string`). + * @param allowedBooleans The boolean literal values permitted by the option's JSON schema. + * @returns A Yargs coerce function. + */ +function createCoerceToBoolean( + type: 'array' | 'string', + allowedBooleans: ReadonlySet = ALL_BOOLEAN_VALUES, +): (value: unknown) => unknown { + const allowTrue = allowedBooleans.has(true); + const allowFalse = allowedBooleans.has(false); + + if (type === 'string') { + return (value: unknown) => + Array.isArray(value) + ? value.map((item) => coerceStringToBoolean(item, allowTrue, allowFalse)) + : coerceStringToBoolean(value, allowTrue, allowFalse); + } + + return (value: unknown) => { + if (!Array.isArray(value)) { + return coerceStringToBoolean(value, allowTrue, allowFalse); } - if (value.length === 1) { - const firstValue = value[0]; - if (typeof firstValue === 'boolean') { - return firstValue; - } + if (value.length === 0) { + return allowTrue ? true : value; + } - if (firstValue === 'true' || firstValue === 'false') { - return firstValue === 'true'; + let result: unknown[] | boolean | undefined; + for (const item of value) { + if (item === true || (item === 'true' && allowTrue)) { + result = true; + } else if (item === false || (item === 'false' && allowFalse)) { + result = false; + } else if (Array.isArray(result)) { + result.push(item); + } else { + result = [item]; } } - } - return value; + return result; + }; } /** @@ -245,6 +281,58 @@ function isSupportedArraySchema(schema: json.JsonObject): boolean { return false; } +/** + * Gets the boolean literal values permitted by a JSON schema node. + * @param schema The JSON schema node to inspect. + * @returns A set of allowed boolean values (`true`, `false`, or both). + */ +function getAllowedBooleanValues(schema: json.JsonObject): ReadonlySet { + if (!json.schema.getTypesOfSchema(schema).has('boolean')) { + return new Set(); + } + + let allowed = new Set(ALL_BOOLEAN_VALUES); + + if (schema.const !== undefined) { + if (typeof schema.const === 'boolean') { + allowed = new Set([schema.const]); + } else { + return new Set(); + } + } + + if (json.isJsonArray(schema.enum)) { + const enumSet = new Set(schema.enum); + allowed = new Set([...allowed].filter((v) => enumSet.has(v))); + } + + if (json.isJsonArray(schema.allOf)) { + for (const subSchema of schema.allOf) { + if (isJsonObject(subSchema)) { + const subAllowed = getAllowedBooleanValues(subSchema); + allowed = new Set([...allowed].filter((v) => subAllowed.has(v))); + } + } + } + + for (const keyword of ['oneOf', 'anyOf'] as const) { + const subSchemas = schema[keyword]; + if (json.isJsonArray(subSchemas)) { + const combined = new Set(); + for (const subSchema of subSchemas) { + if (isJsonObject(subSchema)) { + for (const value of getAllowedBooleanValues(subSchema)) { + combined.add(value); + } + } + } + allowed = new Set([...allowed].filter((v) => combined.has(v))); + } + } + + return allowed; +} + /** * Gets the supported types for a JSON schema node. * @param current The JSON schema node to get the supported types for. @@ -411,7 +499,7 @@ export async function parseJsonSchemaToOptions( return; } - const [type] = types; + const type = types.find((t) => t !== 'boolean') ?? types[0]; const $default = current.$default; const $defaultIndex = isJsonObject($default) && $default['$source'] === 'argv' ? $default['index'] : undefined; @@ -441,6 +529,7 @@ export async function parseJsonSchemaToOptions( deprecated: xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined, positional, types, + booleanValues: types.includes('boolean') ? getAllowedBooleanValues(current) : undefined, ...(type === 'object' ? { type: 'array', @@ -494,6 +583,7 @@ export function addSchemaOptionsToCommand( userAnalytics, type, types, + booleanValues, itemValueType, hidden, name, @@ -515,8 +605,12 @@ export function addSchemaOptionsToCommand( let coerce: YargsOptions['coerce']; if (itemValueType) { coerce = coerceToStringMap; - } else if (type !== 'boolean' && types?.includes('boolean')) { - coerce = coerceToBoolean; + } else if ( + (type === 'array' || type === 'string') && + types?.includes('boolean') && + booleanValues?.size !== 0 + ) { + coerce = createCoerceToBoolean(type, booleanValues); } const sharedOptions: YargsOptions & PositionalOptions = { @@ -527,7 +621,7 @@ export function addSchemaOptionsToCommand( choices, coerce, // This should only be done when `--help` is used otherwise default will override options set in angular.json. - ...(includeDefaultValues ? { default: defaultVal } : {}), + ...(includeDefaultValues && defaultVal !== undefined ? { default: defaultVal } : {}), }; if (positional === undefined) { diff --git a/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts b/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts index dfe4c679ca8f..6441662d48d0 100644 --- a/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts +++ b/packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts @@ -130,6 +130,33 @@ describe('parseJsonSchemaToOptions', () => { }, ], }, + 'localize': { + 'oneOf': [ + { + 'type': 'boolean', + }, + { + 'type': 'array', + 'items': { + 'type': 'string', + }, + }, + ], + }, + 'browsers': { + 'oneOf': [ + { + 'type': 'string', + }, + { + 'const': false, + 'type': 'boolean', + }, + ], + }, + 'runnerConfig': { + 'type': ['string', 'boolean'], + }, }, }; @@ -287,6 +314,112 @@ describe('parseJsonSchemaToOptions', () => { }), ); }); + + it('parses as boolean false when negated with --no-', async () => { + expect(await parse(['--no-allowed-hosts'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': false, + }), + ); + }); + + it('parses as boolean false when array values are followed by negation', async () => { + expect(await parse(['--allowed-hosts', 'a.example.com', '--no-allowed-hosts'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': false, + }), + ); + }); + + it('parses as array when negation is followed by array values', async () => { + expect(await parse(['--no-allowed-hosts', '--allowed-hosts', 'a.example.com'])).toEqual( + jasmine.objectContaining({ + 'allowedHosts': ['a.example.com'], + }), + ); + }); + + it('supports oneOf where boolean is listed before array with values', async () => { + expect(await parse(['--localize', 'fr', 'de'])).toEqual( + jasmine.objectContaining({ + 'localize': ['fr', 'de'], + }), + ); + }); + + it('supports oneOf where boolean is listed before array without values', async () => { + expect(await parse(['--localize'])).toEqual( + jasmine.objectContaining({ + 'localize': true, + }), + ); + }); + + it('supports oneOf where boolean is listed before array with =false', async () => { + expect(await parse(['--localize=false'])).toEqual( + jasmine.objectContaining({ + 'localize': false, + }), + ); + }); + }); + + describe('type=string | boolean', () => { + it('does not coerce empty string when boolean branch has const: false', async () => { + expect(await parse(['--browsers='])).toEqual( + jasmine.objectContaining({ + 'browsers': '', + }), + ); + }); + + it('does not coerce "true" when boolean branch has const: false', async () => { + expect(await parse(['--browsers=true'])).toEqual( + jasmine.objectContaining({ + 'browsers': 'true', + }), + ); + }); + + it('coerces "false" to false when boolean branch has const: false', async () => { + expect(await parse(['--browsers=false'])).toEqual( + jasmine.objectContaining({ + 'browsers': false, + }), + ); + }); + + it('parses --no- negation as false when boolean branch has const: false', async () => { + expect(await parse(['--no-browsers'])).toEqual( + jasmine.objectContaining({ + 'browsers': false, + }), + ); + }); + + it('preserves non-boolean string values when boolean branch has const: false', async () => { + expect(await parse(['--browsers', 'ChromeHeadless'])).toEqual( + jasmine.objectContaining({ + 'browsers': 'ChromeHeadless', + }), + ); + }); + + it('coerces =false in duplicate string | boolean flags', async () => { + expect(await parse(['--runner-config=vitest.config.mts', '--runner-config=false'])).toEqual( + jasmine.objectContaining({ + 'runnerConfig': ['vitest.config.mts', false], + }), + ); + }); + + it('coerces bare flag to true in duplicate string | boolean flags', async () => { + expect(await parse(['--runner-config=vitest.config.mts', '--runner-config'])).toEqual( + jasmine.objectContaining({ + 'runnerConfig': ['vitest.config.mts', true], + }), + ); + }); }); describe('type=string, enum', () => {