Skip to content

Commit f2dbf33

Browse files
committed
fixup! fix(@angular/cli): support array options defined in oneOf schemas
1 parent 476ae0e commit f2dbf33

3 files changed

Lines changed: 250 additions & 24 deletions

File tree

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

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

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

Lines changed: 117 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ export interface Option extends YargsOptions {
5555
* The supported types of the option.
5656
*/
5757
types?: ReadonlyArray<'string' | 'number' | 'boolean' | 'array' | 'object'>;
58+
59+
/**
60+
* Allowed boolean values when `boolean` is one of the supported types.
61+
*/
62+
booleanValues?: ReadonlySet<boolean>;
5863
}
5964

6065
/**
@@ -87,39 +92,70 @@ function checkStringMap(keyValuePairOptions: Set<string>, args: Arguments): bool
8792
return true;
8893
}
8994

95+
const ALL_BOOLEAN_VALUES: ReadonlySet<boolean> = new Set([true, false]);
96+
9097
/**
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.
98+
* Coerces a scalar string value to a boolean when permitted by the schema's allowed boolean values.
9599
*/
96-
function coerceToBoolean(value: unknown): unknown {
100+
function coerceStringToBoolean(value: unknown, allowTrue: boolean, allowFalse: boolean): unknown {
97101
if (typeof value === 'string') {
98-
if (value === '' || value === 'true') {
102+
if ((value === '' || value === 'true') && allowTrue) {
99103
return true;
100104
}
101105

102-
if (value === 'false') {
106+
if (value === 'false' && allowFalse) {
103107
return false;
104108
}
105-
} else if (Array.isArray(value)) {
106-
if (value.length === 0) {
107-
return true;
109+
}
110+
111+
return value;
112+
}
113+
114+
/**
115+
* Creates a Yargs coerce function that converts empty or boolean-like values to a boolean
116+
* when an option supports both `boolean` and another type (`array` or `string`).
117+
* @param type The primary Yargs option type (`array` or `string`).
118+
* @param allowedBooleans The boolean literal values permitted by the option's JSON schema.
119+
* @returns A Yargs coerce function.
120+
*/
121+
function createCoerceToBoolean(
122+
type: 'array' | 'string',
123+
allowedBooleans: ReadonlySet<boolean> = ALL_BOOLEAN_VALUES,
124+
): (value: unknown) => unknown {
125+
const allowTrue = allowedBooleans.has(true);
126+
const allowFalse = allowedBooleans.has(false);
127+
128+
if (type === 'string') {
129+
return (value: unknown) =>
130+
Array.isArray(value)
131+
? value.map((item) => coerceStringToBoolean(item, allowTrue, allowFalse))
132+
: coerceStringToBoolean(value, allowTrue, allowFalse);
133+
}
134+
135+
return (value: unknown) => {
136+
if (!Array.isArray(value)) {
137+
return coerceStringToBoolean(value, allowTrue, allowFalse);
108138
}
109139

110-
if (value.length === 1) {
111-
const firstValue = value[0];
112-
if (typeof firstValue === 'boolean') {
113-
return firstValue;
114-
}
140+
if (value.length === 0) {
141+
return allowTrue ? true : value;
142+
}
115143

116-
if (firstValue === 'true' || firstValue === 'false') {
117-
return firstValue === 'true';
144+
let result: unknown[] | boolean | undefined;
145+
for (const item of value) {
146+
if (item === true || (item === 'true' && allowTrue)) {
147+
result = true;
148+
} else if (item === false || (item === 'false' && allowFalse)) {
149+
result = false;
150+
} else if (Array.isArray(result)) {
151+
result.push(item);
152+
} else {
153+
result = [item];
118154
}
119155
}
120-
}
121156

122-
return value;
157+
return result;
158+
};
123159
}
124160

125161
/**
@@ -245,6 +281,58 @@ function isSupportedArraySchema(schema: json.JsonObject): boolean {
245281
return false;
246282
}
247283

284+
/**
285+
* Gets the boolean literal values permitted by a JSON schema node.
286+
* @param schema The JSON schema node to inspect.
287+
* @returns A set of allowed boolean values (`true`, `false`, or both).
288+
*/
289+
function getAllowedBooleanValues(schema: json.JsonObject): ReadonlySet<boolean> {
290+
if (!json.schema.getTypesOfSchema(schema).has('boolean')) {
291+
return new Set();
292+
}
293+
294+
let allowed = new Set<boolean>(ALL_BOOLEAN_VALUES);
295+
296+
if (schema.const !== undefined) {
297+
if (typeof schema.const === 'boolean') {
298+
allowed = new Set([schema.const]);
299+
} else {
300+
return new Set();
301+
}
302+
}
303+
304+
if (json.isJsonArray(schema.enum)) {
305+
const enumSet = new Set(schema.enum);
306+
allowed = new Set([...allowed].filter((v) => enumSet.has(v)));
307+
}
308+
309+
if (json.isJsonArray(schema.allOf)) {
310+
for (const subSchema of schema.allOf) {
311+
if (isJsonObject(subSchema)) {
312+
const subAllowed = getAllowedBooleanValues(subSchema);
313+
allowed = new Set([...allowed].filter((v) => subAllowed.has(v)));
314+
}
315+
}
316+
}
317+
318+
for (const keyword of ['oneOf', 'anyOf'] as const) {
319+
const subSchemas = schema[keyword];
320+
if (json.isJsonArray(subSchemas)) {
321+
const combined = new Set<boolean>();
322+
for (const subSchema of subSchemas) {
323+
if (isJsonObject(subSchema)) {
324+
for (const value of getAllowedBooleanValues(subSchema)) {
325+
combined.add(value);
326+
}
327+
}
328+
}
329+
allowed = new Set([...allowed].filter((v) => combined.has(v)));
330+
}
331+
}
332+
333+
return allowed;
334+
}
335+
248336
/**
249337
* Gets the supported types for a JSON schema node.
250338
* @param current The JSON schema node to get the supported types for.
@@ -411,7 +499,7 @@ export async function parseJsonSchemaToOptions(
411499
return;
412500
}
413501

414-
const [type] = types;
502+
const type = types.find((t) => t !== 'boolean') ?? types[0];
415503
const $default = current.$default;
416504
const $defaultIndex =
417505
isJsonObject($default) && $default['$source'] === 'argv' ? $default['index'] : undefined;
@@ -441,6 +529,7 @@ export async function parseJsonSchemaToOptions(
441529
deprecated: xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined,
442530
positional,
443531
types,
532+
booleanValues: types.includes('boolean') ? getAllowedBooleanValues(current) : undefined,
444533
...(type === 'object'
445534
? {
446535
type: 'array',
@@ -494,6 +583,7 @@ export function addSchemaOptionsToCommand<T>(
494583
userAnalytics,
495584
type,
496585
types,
586+
booleanValues,
497587
itemValueType,
498588
hidden,
499589
name,
@@ -515,8 +605,12 @@ export function addSchemaOptionsToCommand<T>(
515605
let coerce: YargsOptions['coerce'];
516606
if (itemValueType) {
517607
coerce = coerceToStringMap;
518-
} else if (type !== 'boolean' && types?.includes('boolean')) {
519-
coerce = coerceToBoolean;
608+
} else if (
609+
(type === 'array' || type === 'string') &&
610+
types?.includes('boolean') &&
611+
booleanValues?.size !== 0
612+
) {
613+
coerce = createCoerceToBoolean(type, booleanValues);
520614
}
521615

522616
const sharedOptions: YargsOptions & PositionalOptions = {
@@ -527,7 +621,7 @@ export function addSchemaOptionsToCommand<T>(
527621
choices,
528622
coerce,
529623
// This should only be done when `--help` is used otherwise default will override options set in angular.json.
530-
...(includeDefaultValues ? { default: defaultVal } : {}),
624+
...(includeDefaultValues && defaultVal !== undefined ? { default: defaultVal } : {}),
531625
};
532626

533627
if (positional === undefined) {

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

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,33 @@ describe('parseJsonSchemaToOptions', () => {
130130
},
131131
],
132132
},
133+
'localize': {
134+
'oneOf': [
135+
{
136+
'type': 'boolean',
137+
},
138+
{
139+
'type': 'array',
140+
'items': {
141+
'type': 'string',
142+
},
143+
},
144+
],
145+
},
146+
'browsers': {
147+
'oneOf': [
148+
{
149+
'type': 'string',
150+
},
151+
{
152+
'const': false,
153+
'type': 'boolean',
154+
},
155+
],
156+
},
157+
'runnerConfig': {
158+
'type': ['string', 'boolean'],
159+
},
133160
},
134161
};
135162

@@ -287,6 +314,112 @@ describe('parseJsonSchemaToOptions', () => {
287314
}),
288315
);
289316
});
317+
318+
it('parses as boolean false when negated with --no-', async () => {
319+
expect(await parse(['--no-allowed-hosts'])).toEqual(
320+
jasmine.objectContaining({
321+
'allowedHosts': false,
322+
}),
323+
);
324+
});
325+
326+
it('parses as boolean false when array values are followed by negation', async () => {
327+
expect(await parse(['--allowed-hosts', 'a.example.com', '--no-allowed-hosts'])).toEqual(
328+
jasmine.objectContaining({
329+
'allowedHosts': false,
330+
}),
331+
);
332+
});
333+
334+
it('parses as array when negation is followed by array values', async () => {
335+
expect(await parse(['--no-allowed-hosts', '--allowed-hosts', 'a.example.com'])).toEqual(
336+
jasmine.objectContaining({
337+
'allowedHosts': ['a.example.com'],
338+
}),
339+
);
340+
});
341+
342+
it('supports oneOf where boolean is listed before array with values', async () => {
343+
expect(await parse(['--localize', 'fr', 'de'])).toEqual(
344+
jasmine.objectContaining({
345+
'localize': ['fr', 'de'],
346+
}),
347+
);
348+
});
349+
350+
it('supports oneOf where boolean is listed before array without values', async () => {
351+
expect(await parse(['--localize'])).toEqual(
352+
jasmine.objectContaining({
353+
'localize': true,
354+
}),
355+
);
356+
});
357+
358+
it('supports oneOf where boolean is listed before array with =false', async () => {
359+
expect(await parse(['--localize=false'])).toEqual(
360+
jasmine.objectContaining({
361+
'localize': false,
362+
}),
363+
);
364+
});
365+
});
366+
367+
describe('type=string | boolean', () => {
368+
it('does not coerce empty string when boolean branch has const: false', async () => {
369+
expect(await parse(['--browsers='])).toEqual(
370+
jasmine.objectContaining({
371+
'browsers': '',
372+
}),
373+
);
374+
});
375+
376+
it('does not coerce "true" when boolean branch has const: false', async () => {
377+
expect(await parse(['--browsers=true'])).toEqual(
378+
jasmine.objectContaining({
379+
'browsers': 'true',
380+
}),
381+
);
382+
});
383+
384+
it('coerces "false" to false when boolean branch has const: false', async () => {
385+
expect(await parse(['--browsers=false'])).toEqual(
386+
jasmine.objectContaining({
387+
'browsers': false,
388+
}),
389+
);
390+
});
391+
392+
it('parses --no- negation as false when boolean branch has const: false', async () => {
393+
expect(await parse(['--no-browsers'])).toEqual(
394+
jasmine.objectContaining({
395+
'browsers': false,
396+
}),
397+
);
398+
});
399+
400+
it('preserves non-boolean string values when boolean branch has const: false', async () => {
401+
expect(await parse(['--browsers', 'ChromeHeadless'])).toEqual(
402+
jasmine.objectContaining({
403+
'browsers': 'ChromeHeadless',
404+
}),
405+
);
406+
});
407+
408+
it('coerces =false in duplicate string | boolean flags', async () => {
409+
expect(await parse(['--runner-config=vitest.config.mts', '--runner-config=false'])).toEqual(
410+
jasmine.objectContaining({
411+
'runnerConfig': ['vitest.config.mts', false],
412+
}),
413+
);
414+
});
415+
416+
it('coerces bare flag to true in duplicate string | boolean flags', async () => {
417+
expect(await parse(['--runner-config=vitest.config.mts', '--runner-config'])).toEqual(
418+
jasmine.objectContaining({
419+
'runnerConfig': ['vitest.config.mts', true],
420+
}),
421+
);
422+
});
290423
});
291424

292425
describe('type=string, enum', () => {

0 commit comments

Comments
 (0)