Skip to content

[Java] Declare the inline discriminator enum in oneOf interfaces - #25428

Open
cpruijsen wants to merge 2 commits into
OpenAPITools:masterfrom
cpruijsen:fix/java-oneof-interface-enum
Open

cpruijsen wants to merge 2 commits into
OpenAPITools:masterfrom
cpruijsen:fix/java-oneof-interface-enum

Conversation

@cpruijsen

Copy link
Copy Markdown
Contributor

Fixes #25398

A oneOf interface whose discriminator is an inline enum declared PetTypeEnum getPetType() without declaring PetTypeEnum. Each implementor declared a separate nested enum. The interface did not compile, and the implementor enums could not satisfy its getter.

The interface now declares the inline enum, and implementors reuse the inherited nested type.

Changes:

  • AbstractJavaCodegen keeps interface-owned inline enums and clears isEnum on interface properties collected from implementors. It also clears the flag on discriminator copies pushed into implementors. The [REQ] [Java] Allow excluding discriminator property from generated models #24769 implementor-owned case is unchanged. Groovy is excluded because it does not render Java oneOf interfaces.
  • Java, Spring, JAX-RS Spec, Micronaut, and Helidon interface templates render an enum when isEnum and isDiscriminator are set. JAX-RS also renders the required Jackson, JSON-B, or XML imports.
  • Documentation uses isInnerEnum, so copied properties retain their enum type and values after isEnum is cleared.
  • Spring and JAX-RS regression tests use bugs/issue_25398.yaml. No new vendor extension is added.

Implementation choice: shared Java post-processing covers every Java interface template. A Spring-only pass would be smaller but would leave the same defect in Java, JAX-RS Spec, and Micronaut. Repairing the pre-existing class-based oneOf hierarchy in native, okhttp-gson, Jersey, and Helidon is outside this change.

Checks:

  • Affected generator suites: 1376 tests, 0 failures, 0 errors.
  • checkstyle:check: BUILD SUCCESS.
  • Both new regression tests fail on merge-base and pass on this change.
  • Nineteen interface-based generator and library configurations type-check with javac. JAX-RS also type-checks with JSON-B and XML.
  • The generated Spring project compiles 8 source files. On merge-base it fails because PetTypeEnum is missing.
  • Groovy output compiles on master and on this change.
  • Java and Micronaut Dog.md retain the enum type and values.
  • All sample configurations and the generator docs regenerate with no committed changes.

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.

    I ran ./bin/generate-samples.sh ./bin/configs/*.yaml and ./bin/utils/export_docs_generators.sh with this change. Neither changed a committed file, so the PR has no sample changes.

  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg @KannaKim (Java)
@cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente (Java Spring)

A oneOf interface whose discriminator is an inline enum declared the
getter but not the enum, while each implementor declared its own copy.
The interface did not compile. The interface now declares the enum and
the implementors reuse it.

Fixes OpenAPITools#25398

@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 across 15 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache:6">
P2: Property-level oneOf interfaces still omit inline discriminator enums because this template only renders enums from `vars`, while `addOneOfInterfaceModel` creates those synthetic interfaces without populating `vars`. Populate the synthetic interface's discriminator property or render the enum from discriminator metadata, then cover this form with a regression test.</violation>

<violation number="2" location="modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache:9">
P2: The discriminator enum rendered here uses `@JsonCreator`, but the oneOf interface model may not import `com.fasterxml.jackson.annotation.JsonCreator`. Confirm the generated interface compiles with jackson, or add the import explicitly as the JAX-RS template does.</violation>
</file>

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

View guided diff | Re-trigger cubic

{{#vars}}
{{#isEnum}}
{{#isDiscriminator}}
{{>modelInnerEnum}}

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.

P2: The discriminator enum rendered here uses @JsonCreator, but the oneOf interface model may not import com.fasterxml.jackson.annotation.JsonCreator. Confirm the generated interface compiles with jackson, or add the import explicitly as the JAX-RS template does.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache, line 9:

<comment>The discriminator enum rendered here uses `@JsonCreator`, but the oneOf interface model may not import `com.fasterxml.jackson.annotation.JsonCreator`. Confirm the generated interface compiles with jackson, or add the import explicitly as the JAX-RS template does.</comment>

<file context>
@@ -3,6 +3,13 @@
+    {{#vars}}
+    {{#isEnum}}
+    {{#isDiscriminator}}
+{{>modelInnerEnum}}
+    {{/isDiscriminator}}
+    {{/isEnum}}
</file context>

{{{.}}}
{{/vendorExtensions.x-class-extra-annotation}}
public {{>sealed}}interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{{>permits}}{
{{#vars}}

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.

P2: Property-level oneOf interfaces still omit inline discriminator enums because this template only renders enums from vars, while addOneOfInterfaceModel creates those synthetic interfaces without populating vars. Populate the synthetic interface's discriminator property or render the enum from discriminator metadata, then cover this form with a regression test.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache, line 6:

<comment>Property-level oneOf interfaces still omit inline discriminator enums because this template only renders enums from `vars`, while `addOneOfInterfaceModel` creates those synthetic interfaces without populating `vars`. Populate the synthetic interface's discriminator property or render the enum from discriminator metadata, then cover this form with a regression test.</comment>

<file context>
@@ -3,6 +3,13 @@
 {{{.}}}
 {{/vendorExtensions.x-class-extra-annotation}}
 public {{>sealed}}interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{#-last}} {{/-last}}{{/vendorExtensions.x-implements}}{{>permits}}{
+    {{#vars}}
+    {{#isEnum}}
+    {{#isDiscriminator}}
</file context>

@jpfinne

jpfinne commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@cpruijsen Thanks for this PR.
I started an implementation in DefaultCodeGen protected CodegenDiscriminator createDiscriminator(String schemaName, Schema schema). A really complex task...

As I'm only interested at a fix for java and Spring, your PR is probably simpler.

You update AbstractJavaCodegen so the implementation should be valid for all java generators.
Can you add {{>modelInnerEnum}} in JavaSpring/oneof_interface.mustache and in java/oneof_interface.mustache?

@cpruijsen

Copy link
Copy Markdown
Contributor Author

Thanks! java/oneof_interface already includes {{>modelInnerEnum}}. JavaSpring has no modelInnerEnum partial, so it uses {{>enumClass}}, same as JavaSpring/pojo.mustache.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] discriminator with inline enum: missing generated enum

2 participants