-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
fix(normalizer): simplify oneOf/anyOf with a single inline const/enum… #24821
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: master
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 |
|---|---|---|
|
|
@@ -118,11 +118,20 @@ public static Schema simplifyComposedSchemaWithEnums(Schema schema, | |
| return schema; | ||
| } | ||
|
|
||
| if (subSchemas.size() < 2) { | ||
| //do not process if there's less than 2 sub-schemas. It will be normalized later, and this prevents | ||
| //named enum schemas from being converted to inline enum schemas | ||
| if (subSchemas.isEmpty()) { | ||
| return schema; | ||
| } | ||
|
|
||
| if (subSchemas.size() == 1) { | ||
| Object onlySubSchema = subSchemas.get(0); | ||
| // A lone $ref to a named schema is left as-is (it will be normalized later); this | ||
| // prevents named enum schemas from being converted to inline enum schemas. An inline | ||
| // const/enum sub-schema (e.g. `oneOf: [{const: foo}]`), however, is processed below so | ||
| // that single-value enums are simplified the same way as multi-value ones. | ||
| if (!(onlySubSchema instanceof Schema) || ((Schema) onlySubSchema).get$ref() != null) { | ||
|
Contributor
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. P2: For a valid Prompt for AI agents |
||
| return schema; | ||
| } | ||
| } | ||
| String schemaType = ModelUtils.getType(schema); | ||
|
|
||
| for (Object item : subSchemas) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1725,6 +1725,30 @@ public void testOpenAPINormalizerSingleConstEnum31Spec() { | |||||
| assertEquals(Arrays.asList(originalConst), normalizedTypeSchema.getEnum()); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testOpenAPINormalizerSingleOneOfConstEnum31Spec() { | ||||||
| // reproduces https://github.com/OpenAPITools/openapi-generator/issues/ where a `oneOf` wrapping | ||||||
|
Contributor
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. P3: The comment links to an incomplete issue URL that ends in "issues/" with no issue number, so the link is dead. Remove the placeholder URL or fill in the actual issue number referenced in the PR. Prompt for AI agents
Suggested change
|
||||||
| // a single `const` sub-schema (as opposed to 2+ consts) was not simplified into a proper enum, | ||||||
| // and lost the parent's `type`/`default`/`x-omitempty` in the process. | ||||||
| OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/enum-single-value.yaml"); | ||||||
|
|
||||||
| Schema schema = openAPI.getComponents().getSchemas().get("SingleValueOneOfConst_3_1"); | ||||||
| Schema originalTypeSchema = (Schema) schema.getProperties().get("type"); | ||||||
| assertEquals(originalTypeSchema.getOneOf().size(), 1); | ||||||
|
|
||||||
| OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, Map.of()); | ||||||
| openAPINormalizer.normalize(); | ||||||
|
|
||||||
| Schema schema2 = openAPI.getComponents().getSchemas().get("SingleValueOneOfConst_3_1"); | ||||||
| Schema normalizedTypeSchema = (Schema) schema2.getProperties().get("type"); | ||||||
| assertTrue(ModelUtils.isEnumSchema(normalizedTypeSchema)); | ||||||
| assertNull(normalizedTypeSchema.getOneOf()); | ||||||
| assertEquals(normalizedTypeSchema.getEnum(), List.of("this-is-my-only-value")); | ||||||
|
Contributor
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. P3: These assertions pass the actual value in the expected position (assertEquals takes expected first). On a failed assertion JUnit will report the swapped values, e.g. "expected:<[this-is-my-only-value]> but was:<...>", which misleads when debugging. Swap to assertEquals(expected, actual), or use assertNull/assertTrue where clearer. Prompt for AI agents
Suggested change
|
||||||
| assertEquals(ModelUtils.getType(normalizedTypeSchema), "string"); | ||||||
| assertEquals(normalizedTypeSchema.getDefault(), "this-is-my-only-value"); | ||||||
| assertEquals(normalizedTypeSchema.getExtensions().get("x-omitempty"), true); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
| public void testOpenAPINormalizerProcessingAllOfSchema31Spec() { | ||||||
| // to test array schema processing in 3.1 spec | ||||||
|
|
||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,6 @@ components: | |
| enum: | ||
| - A | ||
| - B | ||
| type: string | ||
| Parent: | ||
| properties: | ||
| number: | ||
|
|
||
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.
P2: In OpenAPI 3.1, this treats
$refschemas with sibling constraints as lone references, so singleoneOf/anyOfbranches combining a reference withconstorenumnever reach enum simplification. Distinguish a pure$reffrom a reference with siblings, and handle the sibling constraints before deciding to skip it.Prompt for AI agents