[Java] [okhttp-gson] Add `anyOf` Discriminator Support
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:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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
anyOfadapters 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.
- Added discriminator lookup in
-
Migration
- No migration required. Enable
useOneOfDiscriminatorLookupto apply to bothoneOfandanyOf.
- No migration required. Enable