openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][Java][webclient] Narrowed discriminator enums in allOf subclasses generate incompatible getter return types

Open 0-mar opened this issue 1 month ago • 3 comments

Bug Report Checklist
  • [X] Have you provided a full/minimal spec to reproduce the issue?
  • [X] Have you validated the input using an OpenAPI validator?
  • [X] Have you tested with the latest master to confirm the issue still exists?
  • [X] Have you searched for related issues/PRs?
  • [X] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

A parent defines a string-enum discriminator. Each allOf child narrows that property to a single member of the parent enum. Java generation emits separate nested enum classes in parent and children. Their getters have incompatible return types, so the generated client cannot compile.

openapi-generator version
  • Reproduced with Docker image openapitools/openapi-generator-cli:latest. Generator version: 7.27.0-SNAPSHOT, commit 1f711a2
  • Also reproduced previously with OpenAPI Generator 7.26.0 through the Gradle plugin.
  • Generator: java; library: webclient
  • Options: dateLibrary=java8, useJakartaEe=true
  • OpenAPI 3.1.0
  • Docker-generated client compilation: Maven Compiler Plugin 3.13.0, Java source/target 17; JDK 25.
  • Earlier Gradle reproduction: Java 17 target using JDK 25.
Tested OpenAPI schema

Here is the minimal OpenAPI schema

Steps to reproduce

Place the sample schema in spec.yml, then run the following commands from that directory. The host directory is mounted as /local in the container.

docker pull openapitools/openapi-generator-cli:latest
docker run --rm openapitools/openapi-generator-cli:latest version --full

docker run --rm \
  --user "$(id -u):$(id -g)" \
  -v "$PWD:/local" \
  openapitools/openapi-generator-cli:latest generate \
  -i /local/spec.yml \
  -g java \
  --library webclient \
  -o /local/generated \
  --additional-properties=dateLibrary=java8,useJakartaEe=true \
  --global-property=apiTests=false,modelTests=false

mvn -f generated/pom.xml -DskipTests compile

Actual result

Docker generation succeeds and writes the three model classes. Maven then compiles 16 source files and fails with two incompatible getter return types.

[ERROR] /tmp/openapi-enum-test/generated/src/main/java/org/openapitools/client/model/HtmlComment.java:[99,19] getType() in org.openapitools.client.model.HtmlComment cannot override getType() in org.openapitools.client.model.ItemDefinition
  return type org.openapitools.client.model.HtmlComment.TypeEnum is not compatible with org.openapitools.client.model.ItemDefinition.TypeEnum
[ERROR] /tmp/openapi-enum-test/generated/src/main/java/org/openapitools/client/model/Textfield.java:[99,19] getType() in org.openapitools.client.model.Textfield cannot override getType() in org.openapitools.client.model.ItemDefinition
  return type org.openapitools.client.model.Textfield.TypeEnum is not compatible with org.openapitools.client.model.ItemDefinition.TypeEnum

Expected result

The generated Java client should compile. Parent and subclass discriminator getters must have compatible Java return types while retaining the schema's narrower subtype constraints.

Suggest a fix

AbstractJavaCodegen.reconcileInlineEnums() only removes a child's enum when the entire property equals the parent's property. Narrower child enum values prevent that match.

Proposed changes:

  1. Detect when an allOf child narrows an inherited discriminator enum to a subset of the parent's values.
  2. Reuse the parent's Java enum type so inherited getters and setters have compatible signatures, without generating another enum or backing field.
  3. Preserve the child's narrower allowed values through validation and initialize singleton discriminator values appropriately.
Related issues/PRs
  • #7252 reported incompatible Java getter return types when an allOf child adds an enum constraint to an inherited property. Its follow-up discussion explicitly includes the arrangement reproduced here: a parent enum containing all values and children narrowing it to subsets.
  • #7267 closed that issue with enum-as-string processing and Jersey2-specific template changes. This report demonstrates that the parent-enum/child-subset case still produces uncompilable code with java / webclient in 7.27.0-SNAPSHOT (1f711a2). It does not establish a regression in the Jersey2 fix.
Current workaround

For client generation only, remove child discriminator property declarations from a temporary schema, allowing them to inherit the parent's enum. Keep the original published schema unchanged. This fixes compilation but does not implement separate subtype allowed-value validation in generated Java.

0-mar avatar Oct 26 '26 13:10 0-mar

@Mattias-Sehlstedt I saw #25451 and #25590 laying the groundwork for this. Are you planning to do the final fix yourself? If it would help, I’m happy to take it on or to test a PR against the reproduction spec. I’m not looking to duplicate work, so I’ll follow your lead.

rajeshvasa avatar Nov 15 '26 03:11 rajeshvasa

Hi @rajeshvasa, I am looking into delivering a solution to the problem, but I have not yet reach a conclusion regarding what approach that might be best. I have already implemented the suggestion by the author here, but I believe it might be a bit invasive and might cause regressions.

I have instead started to investigate improving the overall composition logic so that it more natively supports type union/intersection, but that is a bit complex and a lot of assumptions are currently made.

One might also argue that the author of the issue could potentially consider using oneOfInterfaces, since that most likely has this inbuilt with how it introspects the discriminator and tries to find a common definition.

Mattias-Sehlstedt avatar Nov 15 '26 14:11 Mattias-Sehlstedt

Thanks @Mattias-Sehlstedt for the update and the detailed reasoning. Since you're already working on this, I'll step back so we don't duplicate effort. Agreed that the composition-level approach is the cleaner long-term fix, and oneOfInterfaces sounds like a good workaround for the author in the meantime. Happy to help test or review your PR once it's up.

rajeshvasa avatar Nov 15 '26 19:11 rajeshvasa