[Java][Spring] Use instantiationTypes in add/put item helpers (fixes #25642)

Merged
RA
16d ago
+233-66

Fixes #25642

Since 7.26.0 (#25001), the add<Name>Item / put<Name>Item helpers in the Java model templates always create an ArrayList, LinkedHashSet or HashMap when the container is null, ignoring instantiationTypes. The field initializer still uses the configured type, so the two disagree, and with a typeMappings container type that ArrayList/HashMap doesn't implement (e.g. map=SequencedMap with instantiationTypes map=LinkedHashMap), the model no longer compiles.

As suggested in the issue, AbstractJavaCodegen now exposes the configured instantiation type to the templates:

  • fromProperty sets the vendor extension x-java-container-instantiation-type on array, set and map properties, from instantiationTypes (array, set, map), defaulting to 'ArrayList, LinkedHashSetandHashMap`
  • The helper lines in the 21 model templates changed by #25001 use new {{{vendorExtensions.x-java-container-instantiation-type}}}<>() instead of the hard-coded type (Java client libraries, JavaSpring, JAX-RS, Play, Camel, Micronaut, MSF4J, PKMST)
  • The model imports the configured type: a simple java.util collection name (e.g. 'LinkedList') gets a java.util import mapping, a user import mapping is respected, and a fully qualified name needs no import

Generated with --instantiation-types map=LinkedHashMap,array=LinkedList,set=TreeSet, Basket.java (spring):

// before
this.entries = new HashMap<>();
this.tags = new ArrayList<>();
this.labels = new LinkedHashSet<>();

// after (matches the field initializers)
this.entries = new LinkedHashMap<>();
this.tags = new LinkedList<>();
this.labels = new TreeSet<>();

Default output is unchanged: I generated the fake petstore spec with the old and new templates for spring, java (okhttp-gson, feign, jersey2, jersey3, microprofile, native, restclient, resttemplate, webclient), jaxrs-spec, jaxrs-cxf, jaxrs-cxf-cdi, jaxrs-jersey, java-play-framework, java-camel, java-micronaut-client, java-msf4j and java-pkmst, and apart from build timestamps the output is identical, so no samples change.

Tests: SpringCodegenTest#containerItemHelpersUseConfiguredInstantiationTypes, SpringCodegenTest#containerItemHelpersDefaultToArrayListLinkedHashSetAndHashMap, JavaClientCodegenTest#containerItemHelpersUseConfiguredInstantiationTypes and JavaClientCodegenTest#containerItemHelpersImportConfiguredTypesWhenFieldsDefaultToNull, with a new spec src/test/resources/bugs/issue_25642.yaml. All four pass locally (./mvnw -pl modules/openapi-generator -am test, Windows, JDK 17).

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.

cc Java / Java Spring technical committee: @martin-mfg @KannaKim


Summary by cubic

Fixes Java model add<Name>Item/put<Name>Item helpers ignoring instantiationTypes, so generated models with custom container types (for example map=SequencedMap) no longer compile. Since 7.26.0, these helpers always instantiated ArrayList, LinkedHashSet, or HashMap, while field initializers still used the configured types. AbstractJavaCodegen now exposes the configured type as the x-java-container-instantiation-type vendor extension, the 21 affected model templates use it, and generated models import the class. Default output is unchanged.

  • Covers Java client libraries, JavaSpring, JAX-RS, Play Framework, Camel, Micronaut, MSF4J, and PKMST templates.
  • Imports use the configured class: simple java.util names get a java.util import, user import mappings are respected, and fully qualified names need no import. This also covers containerDefaultToNull, where the helper is the only use.
  • Adds tests for configured, default, and import behavior.