Skip to content

Commit 476ae0e

Browse files
committed
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
1 parent 88b43b9 commit 476ae0e

3 files changed

Lines changed: 138 additions & 2 deletions

File tree

‎packages/angular/build/src/builders/dev-server/schema.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
}
4949
},
5050
{
51+
"const": true,
5152
"type": "boolean",
5253
"description": "Indicates that all hosts are allowed. This is not recommended and a security risk."
5354
}

‎packages/angular/cli/src/command-builder/utilities/json-schema.ts‎

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ export interface Option extends YargsOptions {
5050
* Type of the values in a key/value pair field.
5151
*/
5252
itemValueType?: 'string';
53+
54+
/**
55+
* The supported types of the option.
56+
*/
57+
types?: ReadonlyArray<'string' | 'number' | 'boolean' | 'array' | 'object'>;
5358
}
5459

5560
/**
@@ -82,6 +87,41 @@ function checkStringMap(keyValuePairOptions: Set<string>, args: Arguments): bool
8287
return true;
8388
}
8489

90+
/**
91+
* A Yargs coerce function that converts empty or boolean-like values to a boolean
92+
* when an option supports both `boolean` and another type (such as `array` or `string`).
93+
* @param value The parsed argument value.
94+
* @returns A boolean if the value represents a boolean flag, otherwise the original value.
95+
*/
96+
function coerceToBoolean(value: unknown): unknown {
97+
if (typeof value === 'string') {
98+
if (value === '' || value === 'true') {
99+
return true;
100+
}
101+
102+
if (value === 'false') {
103+
return false;
104+
}
105+
} else if (Array.isArray(value)) {
106+
if (value.length === 0) {
107+
return true;
108+
}
109+
110+
if (value.length === 1) {
111+
const firstValue = value[0];
112+
if (typeof firstValue === 'boolean') {
113+
return firstValue;
114+
}
115+
116+
if (firstValue === 'true' || firstValue === 'false') {
117+
return firstValue === 'true';
118+
}
119+
}
120+
}
121+
122+
return value;
123+
}
124+
85125
/**
86126
* A Yargs coerce function that converts an array of `key=value` strings to an object.
87127
* @param value An array of `key=value` strings.
@@ -177,6 +217,34 @@ function isSupportedArrayItemSchema(schema: json.JsonObject): boolean {
177217
return false;
178218
}
179219

220+
/**
221+
* Checks if a JSON schema node represents a supported array schema,
222+
* either directly or within `oneOf`/`anyOf` subschemas.
223+
* @param schema The JSON schema node to check.
224+
* @returns `true` if the schema represents a supported array, otherwise `false`.
225+
*/
226+
function isSupportedArraySchema(schema: json.JsonObject): boolean {
227+
if (isJsonObject(schema.items) && isSupportedArrayItemSchema(schema.items)) {
228+
return true;
229+
}
230+
231+
if (
232+
json.isJsonArray(schema.oneOf) &&
233+
schema.oneOf.some((item) => isJsonObject(item) && isSupportedArraySchema(item))
234+
) {
235+
return true;
236+
}
237+
238+
if (
239+
json.isJsonArray(schema.anyOf) &&
240+
schema.anyOf.some((item) => isJsonObject(item) && isSupportedArraySchema(item))
241+
) {
242+
return true;
243+
}
244+
245+
return false;
246+
}
247+
180248
/**
181249
* Gets the supported types for a JSON schema node.
182250
* @param current The JSON schema node to get the supported types for.
@@ -198,7 +266,7 @@ function getSupportedTypes(
198266
case 'string':
199267
return true;
200268
case 'array':
201-
return isJsonObject(current.items) && isSupportedArrayItemSchema(current.items);
269+
return isSupportedArraySchema(current);
202270
case 'object':
203271
return isStringMap(current);
204272
default:
@@ -372,6 +440,7 @@ export async function parseJsonSchemaToOptions(
372440
typeof current['x-user-analytics'] === 'string' ? current['x-user-analytics'] : undefined,
373441
deprecated: xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined,
374442
positional,
443+
types,
375444
...(type === 'object'
376445
? {
377446
type: 'array',
@@ -424,6 +493,7 @@ export function addSchemaOptionsToCommand<T>(
424493
alias,
425494
userAnalytics,
426495
type,
496+
types,
427497
itemValueType,
428498
hidden,
429499
name,
@@ -442,13 +512,20 @@ export function addSchemaOptionsToCommand<T>(
442512
keyValuePairOptions.add(dashedName);
443513
}
444514

515+
let coerce: YargsOptions['coerce'];
516+
if (itemValueType) {
517+
coerce = coerceToStringMap;
518+
} else if (type !== 'boolean' && types?.includes('boolean')) {
519+
coerce = coerceToBoolean;
520+
}
521+
445522
const sharedOptions: YargsOptions & PositionalOptions = {
446523
alias,
447524
hidden,
448525
description,
449526
deprecated,
450527
choices,
451-
coerce: itemValueType ? coerceToStringMap : undefined,
528+
coerce,
452529
// This should only be done when `--help` is used otherwise default will override options set in angular.json.
453530
...(includeDefaultValues ? { default: defaultVal } : {}),
454531
};

‎packages/angular/cli/src/command-builder/utilities/json-schema_spec.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ describe('parseJsonSchemaToOptions', () => {
116116
],
117117
},
118118
},
119+
'allowedHosts': {
120+
'default': [],
121+
'oneOf': [
122+
{
123+
'type': 'array',
124+
'items': {
125+
'type': 'string',
126+
},
127+
},
128+
{
129+
'type': 'boolean',
130+
},
131+
],
132+
},
119133
},
120134
};
121135

@@ -231,6 +245,50 @@ describe('parseJsonSchemaToOptions', () => {
231245
});
232246
});
233247

248+
describe('type=array in oneOf', () => {
249+
it('parses valid option value when specified once', async () => {
250+
expect(await parse(['--allowed-hosts', 'a.example.com', 'b.example.com'])).toEqual(
251+
jasmine.objectContaining({
252+
'allowedHosts': ['a.example.com', 'b.example.com'],
253+
}),
254+
);
255+
});
256+
257+
it('parses valid option value when specified multiple times', async () => {
258+
expect(
259+
await parse(['--allowed-hosts', 'a.example.com', '--allowed-hosts', 'b.example.com']),
260+
).toEqual(
261+
jasmine.objectContaining({
262+
'allowedHosts': ['a.example.com', 'b.example.com'],
263+
}),
264+
);
265+
});
266+
267+
it('parses as boolean true when specified without values', async () => {
268+
expect(await parse(['--allowed-hosts'])).toEqual(
269+
jasmine.objectContaining({
270+
'allowedHosts': true,
271+
}),
272+
);
273+
});
274+
275+
it('parses as boolean true when specified with =true', async () => {
276+
expect(await parse(['--allowed-hosts=true'])).toEqual(
277+
jasmine.objectContaining({
278+
'allowedHosts': true,
279+
}),
280+
);
281+
});
282+
283+
it('parses as boolean false when specified with =false', async () => {
284+
expect(await parse(['--allowed-hosts=false'])).toEqual(
285+
jasmine.objectContaining({
286+
'allowedHosts': false,
287+
}),
288+
);
289+
});
290+
});
291+
234292
describe('type=string, enum', () => {
235293
it('parses valid option value', async () => {
236294
expect(await parse(['--ssr', 'never'])).toEqual(

0 commit comments

Comments
 (0)