Skip to content

Add introspection support for discovering an enum discriminator in a oneOf allOf polymorphic structure#24158

Draft
Mattias-Sehlstedt wants to merge 1 commit into
OpenAPITools:masterfrom
Mattias-Sehlstedt:oneof-allof-enum-discriminator-support
Draft

Add introspection support for discovering an enum discriminator in a oneOf allOf polymorphic structure#24158
Mattias-Sehlstedt wants to merge 1 commit into
OpenAPITools:masterfrom
Mattias-Sehlstedt:oneof-allof-enum-discriminator-support

Conversation

@Mattias-Sehlstedt

@Mattias-Sehlstedt Mattias-Sehlstedt commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

This PR adds so that the useOneOfInterfaces setting properly supports when the discriminator is an enum for the case where the discriminator is inherited in a oneOf -> allOf scenario.

Summary

This PR introduces so that the discriminator discovering (done in method recursiveGetDiscriminator) not only finds the property carrying the discriminator, but also the property's type. This gives the interface creator the opportunity to ensure that its discriminator type field aligns with that of the implementing children.

Example scenario

For example given the specification:

openapi: 3.1.0
info:
  title: Polymorphism example with oneOf, allOf and enum-discriminator
  version: "1.0"
paths:
  /pets:
    get:
      operationId: getAllPets
      responses:
        "200":
          content:
            application/json:
              schema:
                type: array
                items:
                  oneOf:
                    - $ref: "#/components/schemas/Cat"
                    - $ref: "#/components/schemas/Dog"
          description: Success
components:
  schemas:
    PetType:
      type: string
      enum:
        - Cat
        - Dog
    Pet:
      type: object
      discriminator:
        propertyName: petType
      properties:
        petType:
          $ref: '#/components/schemas/PetType'
      required:
        - petType
    Cat:
      description: A representation of a cat
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            huntingSkill:
              type: string
              description: The measured skill for hunting
              enum:
                - clueless
                - lazy
                - adventurous
                - aggressive
          required:
            - huntingSkill
    Dog:
      description: A representation of a dog
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            packSize:
              type: integer
              format: int32
              description: the size of the pack the dog is from
              default: 0
              minimum: 0
          required:
            - packSize

where a oneOf containing both children exists. The children in turn references a parent (Pet) that in turn defines the discriminator value as an enum (PetType).

With the current generator, the property type of the parent is not considered, so the generated interface has type String, while the children have the enum PetType. This means that the code does not compile.

This type of structure is what you natively get with swagger-core and springdoc when using @JsonSubTypes for polymorphism in an array-context. So out-of-the-box support would be very useful.

PR checklist

  • Read the contribution guidelines.
  • 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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Adds introspection to discover enum discriminator types inherited through oneOf/allOf so generated models use the enum instead of string. Fixes compile errors with useOneOfInterfaces when the discriminator is defined on a parent and referenced by children.

  • New Features
    • recursiveGetDiscriminator now returns DiscriminatorData (discriminator + property type), and DefaultCodegen uses it to set CodegenDiscriminator.propertyType, preferring the discovered type.
    • Validates oneOf alternatives share the same discriminator name and type; logs warnings on conflicts.
    • Adds tests and a 3.1 sample spec (Pet/Cat/Dog) to verify enum type propagation across the parent, children, and the oneOf wrapper.

Written for commit 1c1a852. Summary will update on new commits.

Review in cubic

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the oneof-allof-enum-discriminator-support branch from 17e5a5e to ea7b62c Compare June 29, 2026 18:17

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found and verified against the latest diff

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java:1214">
P3: Typo in test method name: 'tesOneOfAllOfEnumDiscriminatorInheritance' should be 'testOneOfAllOfEnumDiscriminatorInheritance' (missing 't')</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the oneof-allof-enum-discriminator-support branch from ea7b62c to 43ee077 Compare June 29, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant