Skip to content

Conversation

@wing328
Copy link
Member

@wing328 wing328 commented Sep 7, 2025

based on #21893 with updated samples, fixed python tests and fixed unit tests (case sensitive file names)

Fixes #15701

This changes the OpenAPINormalizer: If you specify a oneOf or an anyOf where the schema and subschemas have the no or the same type, and all contain just enums, it will change the schema to an enum schema.

this is to support the following format:

test_enum:
  type: string
  oneOf:
    - enum: [TEST1]
      description: testing 1
    - enum: [TEST2]
      description: testing 2
    - enum: [TEST3]
      description: testing 3

Which it now normalizes into:

test_enum:
  type: string
  enum: [TEST1, TEST2, TEST3]

Which is a relatively common workaround-pattern to support the lack of descriptive labels for enum values.
It supports both anyOf and oneOf, and works with numbers, booleans and strings.
It does not keep the title or description in the model. Doing that would probably require moving this from the normalizer to the model or code generator, as openAPI as far as I know has no way of keeping these titles in enum constants.
I made this behaviour configurable with the SIMPLIFY_ONEOF_ANYOF_ENUM configuration option. I enabled it by default

To check, see the automated tests I added, and the example I added to the documentation.

Potential discussion topics:

  • I am not entirely sure if at some places I should also check instanceof StringSchema, or write a utility function that gets the type from the type of schema, instead of just calling ModelUtils.getType().
  • should this behaviour be enabled by default?
  • Since I made this configurable, should this be against master or 8.0.x?

Potential additions (not within my current scope):

  • there is already processSimplifyAnyOfStringAndEnumString, which does something similar, but only works with two schemas, where one is a string and one an enum. By also recognizing strings, this new normalization feature could also replace this functionality in a more generic way. But that would break compatibility of the old configuration aption
  • recognizing string or integer constants as part of an enum would be a nice addition - that would also support oneOf with several constants as generating an enum

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328 wing328 added Enhancement: Feature OpenAPI Normalizer Normalize the spec for easier processing labels Sep 7, 2025
@wing328 wing328 added this to the 7.16.0 milestone Sep 7, 2025
@wing328 wing328 marked this pull request as ready for review September 7, 2025 09:35
@wing328 wing328 merged commit 6e089f5 into master Sep 7, 2025
50 of 51 checks passed
@wing328 wing328 deleted the nedap-add_oneof_anyof_enum branch September 7, 2025 09:36
Goopher pushed a commit to Goopher/openapi-generator that referenced this pull request Sep 9, 2025
…enAPITools#21917)

* Support normalizing anyof/oneof enum constraints to a single enum

* Add SIMPLIFY_ONEOF_ANYOF_ENUM to the documentation

* Process referenced schemas with oneof/enum as well

* Implement referenced enum merging from oneof/anyof

* Implement retaining the enum description as x-enum-desriptions for oneof enum

* Update samples and docs with oneOf enum normalization

* update samples to fix python tests

* fix test file name

* fix incorrect filename

---------

Co-authored-by: Pieter Bos <[email protected]>
@pekuz
Copy link

pekuz commented Oct 2, 2025

Seemingly a detail but it helps a lot. Thank you @wing328.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement: Feature OpenAPI Normalizer Normalize the spec for easier processing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using oneOf to specify enum values for a property in schema doesn't generate the enums

4 participants