Skip to content

[Java] Fix issue 22209 oneof pojo #23594

Open
Chhida wants to merge 3 commits intoOpenAPITools:masterfrom
Chhida:fix-issue-22209-oneof-pojo
Open

[Java] Fix issue 22209 oneof pojo #23594
Chhida wants to merge 3 commits intoOpenAPITools:masterfrom
Chhida:fix-issue-22209-oneof-pojo

Conversation

@Chhida
Copy link
Copy Markdown

@Chhida Chhida commented Apr 21, 2026

fixes #22209

  • Add useOneOfPojo option to generate real POJO classes for oneOf schemas instead of wrapper models

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.

Summary by cubic

Adds a useOneOfPojo option to Java generators to emit real POJO classes for oneOf schemas instead of abstract wrapper models, fixing oneOf inheritance and serialization issues. Fixes #22209. Adds a Jersey3 sample and config to demonstrate the option.

  • New Features

    • New additional-property useOneOfPojo (default: false).
    • When enabled, oneOf models are generated as concrete POJOs; wrapper model is not created.
    • Overrides updateModelForComposedSchema() to skip merging for oneOf and restore original vars; option handled in processOpts(); CLI option added; docs updated across Java generators.
    • Added bin/configs/java-jersey3-oneOf-pojo.yaml and samples/client/petstore/java/jersey3-oneOf-pojo/ to validate output.
  • Migration

    • Opt-in by passing --additional-properties useOneOfPojo=true (or set in config). No breaking changes by default.

Written for commit 7830618. Summary will update on new commits.

… of abstract wrapper and ensure real POJO model is produced

- Add useOneOfPojo option to generate real POJO classes for oneOf schemas instead of wrapper models
- Override updateModelForComposedSchema() to restore original vars when option is enabled
- Process the option in processOpts() method with default value false for backward compatibility

fixes OpenAPITools#22209
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.

No issues found across 118 files

Note: This PR contains a large number of files. cubic only reviews up to 75 files per PR, so some files may not have been reviewed. cubic prioritises the most important files to review.

@Chhida Chhida force-pushed the fix-issue-22209-oneof-pojo branch from 0d98002 to 09ed65d Compare April 21, 2026 10:50
- Added bin/configs/java-jersey3-oneOf-pojo.yaml config with useOneOfPojo: true
- Generated samples/client/petstore/java/jersey3-oneOf-pojo/
- Demonstrates how useOneOfPojo=true generates real POJO classes instead of wrapper models
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.

7 issues found across 47 files (changes from recent commits).

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="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/Configuration.java">

<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/Configuration.java:39">
P2: `AtomicReference.updateAndGet` callback invokes a potentially side-effectful supplier, which may run multiple times under contention.</violation>
</file>

<file name="samples/client/petstore/java/jersey3-oneOf-pojo/README.md">

<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/README.md:18">
P2: README minimum Java version is incorrect for the Jersey 3.1 dependency baseline.</violation>
</file>

<file name="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java">

<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/RFC3339JavaTimeModule.java:32">
P2: Custom deserializers are registered after `super.setupModule(context)`, so they may not be applied to the mapper during module registration.</violation>
</file>

<file name="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh">

<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:42">
P2: The script checks for any remote instead of specifically checking for `origin`, which can cause pull/push failures when only non-`origin` remotes exist.</violation>

<violation number="2" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:48">
P2: Access token is embedded in the Git remote URL, risking plaintext secret exposure in `.git/config` and related tooling.</violation>

<violation number="3" location="samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh:57">
P2: `git push` failures can be masked because the script returns the pipeline/`grep` status instead of the push command status.</violation>
</file>

<file name="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/ApiClient.java">

<violation number="1" location="samples/client/petstore/java/jersey3-oneOf-pojo/src/main/java/org/openapitools/client/ApiClient.java:851">
P2: Form-urlencoded serialization overwrites accumulated parameters, causing only the last field to be sent.</violation>
</file>

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

Comment thread samples/client/petstore/java/jersey3-oneOf-pojo/README.md
Comment thread samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh
Comment thread samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh
Comment thread samples/client/petstore/java/jersey3-oneOf-pojo/git_push.sh
@jpfinne
Copy link
Copy Markdown
Contributor

jpfinne commented Apr 21, 2026

@Chhida I don't think the generated code will work.
For example, how to use SchemaA in public ApiResponse<Void> rootPostWithHttpInfo(@jakarta.annotation.Nullable PostRequest postRequest) throws ApiException ?

The inline oneOf complexifies the generation. This is simpler:

components:
  schemas:
    schemaA:
      properties:
        propA:
          type: string
      type: object
    __post_request:
      oneOf:
        - $ref: "#/components/schemas/schemaA"
        - $ref: "#/components/schemas/SchemaAdditional"
    SchemaAdditional:
      additionalProperties: true
      properties: {}
      type: object

The spring generator has an option USE_DEDUCTION_FOR_ONE_OF_INTERFACES

For example:

generateFromContract(
      "src/test/resources/3_0/spring/issue_22209.yaml", SPRING_BOOT,
      Map.of(USE_DEDUCTION_FOR_ONE_OF_INTERFACES, true,
              USE_ONE_OF_INTERFACES, true,
              GENERATE_ALIAS_AS_MODEL, true));

It generates:

@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({
    @JsonSubTypes.Type(value = SchemaA.class), 
    @JsonSubTypes.Type(value = SchemaAdditional.class)
})
public interface PostRequest {
}

I would implement USE_DEDUCTION_FOR_ONE_OF_INTERFACES for the java generators:

  • move USE_DEDUCTION_FOR_ONE_OF_INTERFACES to AbstractJavaCodegen
  • add the logic in modules/openapi-generator/src/main/resources/Java/oneof_interface.mustache. See modules/openapi-generator/src/main/resources/JavaSpring/oneof_interface.mustache

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.

Fix issue 22209: [JAVA-gen] Fix oneOf inheritance: prevent generation of abstract wrapper and ensure real POJO model is produced

2 participants