Skip to content

[Java] [okhttp-gson] Add anyOf Discriminator Support - #24632

Merged
wing328 merged 4 commits into
OpenAPITools:masterfrom
ckoegel:java-anyof
Aug 8, 2026
Merged

wing328 merged 4 commits into
OpenAPITools:masterfrom
ckoegel:java-anyof

Conversation

@ckoegel

@ckoegel ckoegel commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Adds support for discriminators for anyOf models in the Java okhttp-gson library. This functionality is copied over from the oneOf model to match it, and is also gated behind the useOneOfDiscriminatorLookup flag. I kept it behind the flag since it seemed reasonable to assume users would want to toggle discriminator lookup for both oneOf+anyOf at once, tho the flag is now slightly inappropriately named. Lmk if we need to make a change regarding that, that flag is used across a few generators.

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.

@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08) @KannaKim (2026/07)


Summary by cubic

Adds discriminator-based deserialization for anyOf models in the Java okhttp-gson generator, matching oneOf. It now performs fast discriminator lookup when present and safely skips it for non-object payloads, with clearer warnings for missing or non-primitive discriminator values.

  • New Features

    • Added discriminator lookup in anyOf adapters with early switch and fallback to schema-order matching.
    • Guarded lookup to only run for object payloads; improved logs for missing/non-primitive discriminator values.
    • Updated petstore samples (MammalAnyof) and tests for mapped values, unmapped fallback, and empty payload failure.
  • Migration

    • No migration required. Enable useOneOfDiscriminatorLookup to apply to both oneOf and anyOf.

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

Review in cubic

@ckoegel ckoegel changed the title [Java] [okhttp-gson] Add anOf Discriminator Support [Java] [okhttp-gson] Add anyOf Discriminator Support Aug 6, 2026

@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.

All reported issues were addressed across 3 files

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

Re-trigger cubic

@ckoegel

ckoegel commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

@wing328 Let me know what to do about that failing test, I thought that would be a fine place to add a new test but CI doesn't seem to like the fact that file was changed.

@wing328

wing328 commented Aug 8, 2026

Copy link
Copy Markdown
Member

I'll fix it. Don't worry. Thanks for the contribution as always.

@wing328
wing328 merged commit e4835ee into OpenAPITools:master Aug 8, 2026
149 of 151 checks passed
@wing328 wing328 added this to the 7.25.0 milestone Aug 8, 2026
@jpfinne

jpfinne commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

@ckoegel I think you broke master. JsonTest.java was manual updated.
It gives an exception with bin/utils/test_file_list.yaml

@wing328 what is the goal of this Mist Tests to detect changes in test files (introduced in #8020)
If someone adds a custom unit test on generated samples, what is the need to protect it against changes?
Deletion, I might understand the test can be removed accidentally, by why a digest on it?

rar91279 added a commit to rar91279/openapi-generator that referenced this pull request Aug 9, 2026
[Java] [okhttp-gson] Add `anyOf` Discriminator Support (OpenAPITools#24632)
[Java][okhttp-gson] Fix `LocalDateTime` Serialization (OpenAPITools#24643)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants