-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/cli): handle oneOf when converting schema to yargs options
#32554
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -130,21 +130,22 @@ | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| const SUPPORTED_PRIMITIVE_TYPES = new Set(['boolean', 'number', 'string']); | ||||||
| const SUPPORTED_PRIMITIVE_TYPES = new Set(['boolean', 'number', 'string'] as const); | ||||||
| type SupportedPrimitiveType = Parameters<typeof SUPPORTED_PRIMITIVE_TYPES.add>[0]; | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a string is a supported primitive type. | ||||||
| * @param value The string to check. | ||||||
| * @returns `true` if the string is a supported primitive type, otherwise `false`. | ||||||
| */ | ||||||
| function isSupportedPrimitiveType(value: string): boolean { | ||||||
| return SUPPORTED_PRIMITIVE_TYPES.has(value); | ||||||
| function isSupportedPrimitiveType(value: string): value is SupportedPrimitiveType { | ||||||
| return SUPPORTED_PRIMITIVE_TYPES.has(value as any); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the casting, this is not needed if you change the set type.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Recursively checks if a JSON schema for an array's items is a supported primitive type. | ||||||
| * It supports `oneOf` and `anyOf` keywords. | ||||||
| * @param schema The JSON schema for the array's items. | ||||||
| * @param schema The JSON schema to check. | ||||||
| * @returns `true` if the schema is a supported primitive type, otherwise `false`. | ||||||
| */ | ||||||
| function isSupportedArrayItemSchema(schema: json.JsonObject): boolean { | ||||||
|
|
@@ -156,6 +157,10 @@ | |||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| if (isJsonObject(schema.items)) { | ||||||
| return isSupportedArrayItemSchema(schema.items); | ||||||
| } | ||||||
|
|
||||||
| if (json.isJsonArray(schema.items)) { | ||||||
| return schema.items.some((item) => isJsonObject(item) && isSupportedArrayItemSchema(item)); | ||||||
| } | ||||||
|
|
@@ -177,6 +182,40 @@ | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Recursively finds the first supported array primitive type for the given JSON schema. | ||||||
| * It supports `oneOf` and `anyOf` keywords. | ||||||
| * @param schema The JSON schema to inspect. | ||||||
| * @returns The supported primitive type or 'string' if none is found. | ||||||
| */ | ||||||
| function getSupportedArrayType(schema: json.JsonObject): SupportedPrimitiveType { | ||||||
| if (typeof schema.type === 'string' && isSupportedPrimitiveType(schema.type)) { | ||||||
| return schema.type; | ||||||
| } | ||||||
|
|
||||||
| if (json.isJsonArray(schema.enum)) { | ||||||
| return 'string'; | ||||||
| } | ||||||
|
|
||||||
| if (isJsonObject(schema.items)) { | ||||||
| const result = getSupportedArrayType(schema.items); | ||||||
| if (result) return result; | ||||||
| } | ||||||
|
|
||||||
| for (const key of ['items', 'oneOf', 'anyOf']) { | ||||||
| if (json.isJsonArray(schema[key])) { | ||||||
| for (const item in schema[key]) { | ||||||
|
Check failure on line 207 in packages/angular/cli/src/command-builder/utilities/json-schema.ts
|
||||||
| if (isJsonObject(item)) { | ||||||
| const result = getSupportedArrayType(item); | ||||||
| if (result) return result; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return 'string'; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the supported types for a JSON schema node. | ||||||
| * @param current The JSON schema node to get the supported types for. | ||||||
|
|
@@ -198,7 +237,7 @@ | |||||
| case 'string': | ||||||
| return true; | ||||||
| case 'array': | ||||||
| return isJsonObject(current.items) && isSupportedArrayItemSchema(current.items); | ||||||
| return isSupportedArrayItemSchema(current); | ||||||
| case 'object': | ||||||
| return isStringMap(current); | ||||||
| default: | ||||||
|
|
@@ -377,9 +416,13 @@ | |||||
| type: 'array', | ||||||
| itemValueType: 'string', | ||||||
| } | ||||||
| : { | ||||||
| type, | ||||||
| }), | ||||||
| : type === 'array' | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't right as it cause the |
||||||
| ? { | ||||||
| type: getSupportedArrayType(current), | ||||||
| } | ||||||
| : { | ||||||
| type, | ||||||
| }), | ||||||
| }; | ||||||
|
|
||||||
| options.push(option); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the below, which IMO it's slightly easier to read.