[java][native] support useOneOfInterfaces and useDeductionForOneOfInterfaces
Title
[java][native] support useOneOfInterfaces / useDeductionForOneOfInterfaces
Description
With library=native, useOneOfInterfaces=true currently produces code that does not compile. native/model.mustache ignores x-is-one-of-interface and always renders the oneof_model wrapper class (extends AbstractOpenApiSchema), but DefaultCodegen still makes the members implement the oneOf type:
[ERROR] .../model/ServiceAuthor.java:[38,39] interface expected here
As a consequence useDeductionForOneOfInterfaces cannot be used with native either, although it is the way to resolve a oneOf without discriminator by the set of properties (as pointed out by @jpfinne in #25727).
This PR makes native honour the option, the same way okhttp already does:
libraries/native/model.mustacheandmodel_doc.mustachedispatch to the sharedoneof_interface/pojo_doctemplates forx-is-one-of-interfacemodels. WithoutuseOneOfInterfacesnothing changes (wrapper class withgetActualInstance()).- The generated api classes call
toUrlQueryString(...)on deepObject / exploded query parameters. To keep that compiling for a oneOf type, the interface declarestoUrlQueryString(String prefix)(plus adefaultno-arg variant, because enums only implement the prefix variant). This is guarded bysupportUrlQuery, like in the pojo. To avoid copying the whole template, the sharedoneof_interface.mustachegot a hook partialoneof_interface_members, which is empty for every library exceptnative. - Works with Jackson 2 and Jackson 3 (the annotations stay in
com.fasterxml.jackson.annotation).
Example (spec from #25727, useOneOfInterfaces=true,useDeductionForOneOfInterfaces=true):
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({
@JsonSubTypes.Type(value = ServiceAuthor.class),
@JsonSubTypes.Type(value = UserAuthor.class)
})
public interface Author { ... }
public class ServiceAuthor implements Author { ... }
{"subject":"Jane"} ā ServiceAuthor, {"id":"u-1","domainId":"acme"} ā UserAuthor, {"unknown":1} is rejected.
Please note: Jackson's deduction only looks at which properties are present, not at required. {"id":"u-1"} is therefore resolved to UserAuthor (it is the only variant that has an id), it is not rejected. Combine it with generateJsonCreator from #25728 if missing required properties must be rejected, too.
useSealedOneOfInterfaces is still not supported for native (the native pojos are not final); this PR does not change that.
Tests
JavaClientCodegenTest: discriminator interface, deduction interface (Jackson 2 and 3),supportUrlQuery=false, and a regression test thatnativewithoutuseOneOfInterfacesstill generates the wrapper class. All generated sources are compiled byvalidateJavaSourceFiles.- New sample
samples/client/others/java/native-oneof-interface(added to the JDK11 sample workflow) with deserialization tests inAuthorTest. - All other Java samples are unchanged by the regeneration.
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) @AndreyVMarkelov (2026/11)
š¤ Generated with Claude Code
Summary by cubic
Makes the native Java library honor useOneOfInterfaces=true, fixing generated code that previously failed to compile, and enables useDeductionForOneOfInterfaces for oneOf schemas without a discriminator.
Previously the native templates always rendered the oneOf wrapper class while members still implemented the oneOf interface type, producing interface expected here errors. The templates now dispatch to the shared oneof_interface template like the okhttp library does.
Details
- The oneOf interface declares
toUrlQueryString()(prefix and default no-arg variants) via a newoneof_interface_membershook, empty for all other libraries and guarded bysupportUrlQuery. - Works with both Jackson 2 and Jackson 3.
- Adds tests and a new
native-oneof-interfacesample with deserialization tests, registered in the JDK11 sample workflow. useSealedOneOfInterfacesremains unsupported fornative.- Jackson's deduction matches on property presence only, not
required; combine withgenerateJsonCreator(#25728) to reject payloads with missing required properties.