[java][native] support useOneOfInterfaces and useDeductionForOneOfInterfaces

TH
3d ago
+4054-1

Title

[java][native] support useOneOfInterfaces / useDeductionForOneOfInterfaces

Description

Related to #25727 and #25728.

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.mustache and model_doc.mustache dispatch to the shared oneof_interface / pojo_doc templates for x-is-one-of-interface models. Without useOneOfInterfaces nothing changes (wrapper class with getActualInstance()).
  • The generated api classes call toUrlQueryString(...) on deepObject / exploded query parameters. To keep that compiling for a oneOf type, the interface declares toUrlQueryString(String prefix) (plus a default no-arg variant, because enums only implement the prefix variant). This is guarded by supportUrlQuery, like in the pojo. To avoid copying the whole template, the shared oneof_interface.mustache got a hook partial oneof_interface_members, which is empty for every library except native.
  • 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 that native without useOneOfInterfaces still generates the wrapper class. All generated sources are compiled by validateJavaSourceFiles.
  • New sample samples/client/others/java/native-oneof-interface (added to the JDK11 sample workflow) with deserialization tests in AuthorTest.
  • 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:
    ./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) @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 new oneof_interface_members hook, empty for all other libraries and guarded by supportUrlQuery.
  • Works with both Jackson 2 and Jackson 3.
  • Adds tests and a new native-oneof-interface sample with deserialization tests, registered in the JDK11 sample workflow.
  • useSealedOneOfInterfaces remains unsupported for native.
  • Jackson's deduction matches on property presence only, not required; combine with generateJsonCreator (#25728) to reject payloads with missing required properties.