Skip to content

[Java][jaxrs-spec] Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name#23509

Open
thiswasdumb wants to merge 4 commits intoOpenAPITools:masterfrom
thiswasdumb:fix_jsontypename
Open

[Java][jaxrs-spec] Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name#23509
thiswasdumb wants to merge 4 commits intoOpenAPITools:masterfrom
thiswasdumb:fix_jsontypename

Conversation

@thiswasdumb
Copy link
Copy Markdown

@thiswasdumb thiswasdumb commented Apr 9, 2026

Description

When using allOf and oneOf to generate properly nested classes that extend a parent, the generated @JsonTypeName follows the name of the child schemas as opposed to the discriminator used. This serves problematic when sending requests to a server and receiving responses following the same format, because the openapi contract will expect the key but will attempt to deserialize the schema name.

The fix is to set it so it prioritizes a discriminator value being set, but will take the key and finally the schema name if no key is found.

This MR will fix #10822 and will fix #17343

Changes

  • modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaJAXRSSpecServerCodegen.java holds the logic for determining what should be in the generated JsonTypeName field
  • modules/openapi-generator/src/main/resources/JavaJaxRS/spec/pojo.mustache is responsible to look for the x-discriminator-value but also not automatically set it as the schema name
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java holds testing to verify this works as expected
  • modules/openapi-generator/src/test/resources/3_0/jaxrs/petstore.yaml includes generated classes using the oneOf and allOf logic to ensure the flow works as expected

How to Reproduce

Add the current changes from petstore.yaml and generate that file and view the generated classes. You will find that CatRequest.java and DogRequest.java both extend PetRequest.java properly, but their @JsonTypeName fields contain CatRequest and DogRequest instead of CAT and DOG.

Adding the logic you will find the change to be the expected keys instead.

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.

…the key from the oneOf pattern instead of the schema name
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

1 issue found across 4 files

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/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java">

<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/jaxrs/JavaJAXRSSpecServerCodegenTest.java:1246">
P2: The new test points to `pestore.yaml`, but the only file in this directory is `petstore.yaml`. This will cause the test to fail when the OpenAPIParser cannot load the spec. Use the correct filename.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@thiswasdumb
Copy link
Copy Markdown
Author

If it is okay, could I possibly get some reviews from the tech committee?

I saw @chameleon82 is connected to JAX-RS RestEasy, @hiveship is connected to JAX-RS CXF, and @Zomzog is connected to Java generally. I am sorry if I tagged and you aren't too closely related, but I would greatly appreciate any reviews! Thank you so much :)

@thiswasdumb thiswasdumb changed the title Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name [Java] Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name Apr 9, 2026
@thiswasdumb thiswasdumb changed the title [Java] Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name [Java][jaxrs-spec Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name Apr 9, 2026
@thiswasdumb thiswasdumb changed the title [Java][jaxrs-spec Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name [Java][jaxrs-spec] Fix JsonTypeName Generation With allOf and oneOf Combination to Match Key Instead of Schema Name Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant