-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix(normalizer): support OAS 3.1 annotated enums (oneOf + const) for consistent enum generation (C#) #23869
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?
fix(normalizer): support OAS 3.1 annotated enums (oneOf + const) for consistent enum generation (C#) #23869
Changes from 3 commits
9fbf404
51e0b0f
ba6d6a2
553b721
3496c3b
55450c4
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 |
|---|---|---|
|
|
@@ -1620,10 +1620,15 @@ protected Schema simplifyComposedSchemaWithEnums(Schema schema, List<Object> sub | |
|
|
||
| Schema subSchema = ModelUtils.getReferencedSchema(openAPI, (Schema) item); | ||
|
|
||
| // Check if this sub-schema has an enum (with one or more values) | ||
| if (subSchema.getEnum() == null || subSchema.getEnum().isEmpty()) { | ||
| // Check if this sub-schema has an enum or const value (OpenAPI 3.1 uses const for single-value enums) | ||
| List<Object> subSchemaEnumValues = subSchema.getEnum(); | ||
| if ((subSchemaEnumValues == null || subSchemaEnumValues.isEmpty()) && subSchema.getConst() == 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. Rather than doing if (!definesEnum && subSchema.getConst() == null) {This could allow use to skip the comments almost entirely and instead allow the variable/method names be the explanation themselves. |
||
| return schema; | ||
| } | ||
| // If const is present but enum is not, treat const as a single enum value | ||
| if ((subSchemaEnumValues == null || subSchemaEnumValues.isEmpty()) && subSchema.getConst() != null) { | ||
| subSchemaEnumValues = Arrays.asList(subSchema.getConst()); | ||
| } | ||
|
|
||
| // Ensure all sub-schemas have the same type (if type is specified) | ||
| if(subSchema.getTypes() != null && subSchema.getTypes().size() > 1) { | ||
|
|
@@ -1639,17 +1644,17 @@ protected Schema simplifyComposedSchemaWithEnums(Schema schema, List<Object> sub | |
| } | ||
| } | ||
| // Add all enum values from this sub-schema to our collection | ||
| if(subSchema.getEnum().size() == 1) { | ||
| if(subSchemaEnumValues.size() == 1) { | ||
| String description = subSchema.getTitle() == null ? "" : subSchema.getTitle(); | ||
| if(subSchema.getDescription() != null) { | ||
| if(!description.isEmpty()) { | ||
| description += " - "; | ||
| } | ||
| description += subSchema.getDescription(); | ||
| } | ||
| enumValues.put(subSchema.getEnum().get(0), description); | ||
| enumValues.put(subSchemaEnumValues.get(0), description); | ||
| } else { | ||
| for(Object e: subSchema.getEnum()) { | ||
| for(Object e: subSchemaEnumValues) { | ||
| enumValues.put(e, ""); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1536,9 +1536,10 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() { | |
| assertEquals(schema14.getType(), null); | ||
|
|
||
| Schema schema16 = openAPI.getComponents().getSchemas().get("TypeIntegerWithOneOf"); | ||
| assertEquals(schema16.getOneOf().size(),3); | ||
| assertEquals(((Schema) schema16.getOneOf().get(0)).getConst(), 1); | ||
| assertEquals(((Schema) schema16.getOneOf().get(0)).getDeprecated(), true); | ||
| // After normalization, oneOf with const values should be simplified to enum | ||
| assertEquals(schema16.getOneOf(), null); | ||
| assertEquals(schema16.getEnum().size(), 3); | ||
| assertEquals(schema16.getEnum().get(0), 1); | ||
|
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. (I'm not repo maintainer but the PR is something I was thinking about myself: so thank you) The test against Here, seems it is not possible with current enum support implementation ("simple" list of values and optional list of descriptions with x-enum-descriptions) to support enum as "schema" enabling to keep all "schemas" related information with no loss.
Member
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. @nagabalaji-b can you please take a look and add back the
Author
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. @wing328 Done. I added back the deprecated test coverage: Code change: Modified simplifyComposedSchemaWithEnums() to collect and preserve per-value deprecated flags from OAS 3.1 oneOf/anyOf + const sub-schemas into x-enum-deprecated extension. Test assertion: Added explicit checks in testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() for TypeIntegerWithOneOf schema: Verifies x-enum-deprecated list size = 3 The deprecated metadata from the annotated enum pattern (oneOf + const) is now preserved through normalization, so generators can access it via the x-enum-deprecated extension. |
||
|
|
||
| Schema schema18 = openAPI.getComponents().getSchemas().get("OneOfNullAndRef3"); | ||
| // original oneOf removed and simplified to just $ref (oneOf sub-schema) instead | ||
|
|
||
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.
Are these path changes intentional?