[JAVA] Add generic okhttp client
Adds a new okhttp library to the Java client generator, marked [BETA]. It is derived from
okhttp-gson, but decoupled from Gson: a single template set emits working clients for Gson
(default), Jackson 2, Jackson 3 and JSON-B, on OkHttp 5.4.0, and it honours the existing
useJspecify option.
okhttp-gson is untouched and remains the default library — its generated output is byte-identical
to master. This is purely additive for existing users; the new library is opt-in via
--library okhttp.
openapi-generator generate -g java --library okhttp \
-p serializationLibrary=jackson -p useJackson3=true -p useJspecify=true
JavaClientCodegen
- Registers the library, adds it to
JSPECIFY_SUPPORTED_LIBRARIESand to theuseJackson3
allowlist, and reuses theokhttp-gsonsupporting-file branch without forcing a serialization
library. - Resolves
isGson/isJackson/isJsonbafter the serialization switch, where
getSerializationLibrary()is authoritative. No new early property reads, so no behavioural risk
to other libraries. - Forces
openApiNullableoff for JSON-B:jackson-databind-nullableis Jackson-only and the pom
omits it there, so leaving the flag on emittedJsonNullablereferences that could not resolve. - Applies the
x-enum-as-stringdiscriminator rewrite for every serialization library, not just
Jackson. This is not a Jackson nicety — a child schema that narrows an inherited discriminator to
a single-value enum otherwise generates a getter that cannot override the parent'sString
getter.JsonNullable/JsonIgnoreimports stay Jackson-only. - Extends
useSingleRequestParameterand theoneOf/anyOfModelNullhandling to the new
library; both were gated on allowlists that excluded it, even though its templates support them.
Samples — 27 configs covering the four serialization variants, JSpecify for each of Jackson 2/3
and JSON-B, oneOf/anyOf, nullable-required, AWS4 signing, dynamic operations, grouped
parameters, parcelable models, streaming, Swagger 1/2 annotations, OpenAPI 3.1, the echo API and
user-defined templates.
One rename — bin/configs/java-okhttp-user-defined-templates.yaml →
java-okhttp-gson-user-defined-templates.yaml. It generated the okhttp-gson sample despite the
neutral name, and every other okhttp-gson config is already named java-okhttp-gson-*. Content
unchanged, generated sample untouched.
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 @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg @KannaKim
Summary by cubic
Adds an opt-in okhttp library to the Java client generator that produces OkHttp 5.4.0 clients for Gson, Jackson 2/3, and JSON-B. Existing okhttp-gson generation remains the default and byte-identical; the new library disables openApiNullable, and Jackson 3 clients require Java 17.
Client generation
- Adds serializer-specific JSON handling, OAuth retry that reports token-endpoint failures, AWS4 signing, streaming, insecure-TLS hooks, composed models, builders, and JSpecify support.
- Ports the latest
okhttp-gsonfixes: JSON header parameter content, JavaDoc default escaping, complex container defaults, case-insensitive and UUID inner enums, and raw@XmlEnumValue. - Fixes request compression and progress reporting, response cleanup, date handling, header normalization, read-only deserialization, validation, JSON-B discriminator and
additionalPropertieshandling (including parents without a discriminator), and Gson null-safe composed adapters. - Gson and JSON-B now reject enum values excluded via
not: enum— including on inherited properties — so complementaryoneOfbranches resolve to exactly one. - Initializes the shared serializer eagerly, detects
byte[]responses for all serializers, returns raw bodies for String results in JSON-B, and fixes base64 byte serialization. - Preserves
additionalPropertiesthroughtoBuilder().build(), stops enabling reflection-based model equality in the new okhttp samples, and drops unused Jackson imports. - Rejects the misleading
defaultToEmptyContainer=truespelling, supports grouped parameters, dynamic operations,oneOf/anyOf, and Jackson 3JsonTypeNameimports, and depends onokhttp-jvmin the POM and sbt builds.
Samples and validation
- Adds 27 sample configurations across all four serializers, including Jackson 3, JSON-B, OpenAPI 3.1, AWS4, streaming, parcelable models, and user-defined templates.
- Wires the samples into Maven profiles and CI on the appropriate JDK versions, including Java 17 for Jackson 3.
- Existing
okhttp-gsonusers need no migration; generate the new client with--library okhttp.