Repository navigation
[Java][Spring] Use instantiationTypes in add/put item helpers (fixes #25642) - #25649
Conversation
…penAPITools#25642) Since OpenAPITools#25001 the add<Name>Item/put<Name>Item helpers hard-code ArrayList, LinkedHashSet and HashMap, ignoring instantiationTypes, so models with a custom container typeMapping no longer compile. Expose the configured instantiation type as the x-java-container-instantiation-type vendor extension and use it in the helper templates. Default output is unchanged.
There was a problem hiding this comment.
All reported issues were addressed across 25 files
Reply to a comment to ask cubic a question or push back. It learns from your replies.
View guided diff | Re-trigger cubic
The add/put item helpers now instantiate the configured instantiationTypes class, so import it: a simple java.util collection name gets a java.util import mapping, a user import mapping is respected, and a fully qualified name needs no import. Covers containerDefaultToNull, where the helper is the only place the class is used.
|
Addressed cubic’s import findings in 24a9758: the model now imports the configured instantiation type (simple java.util names get a java.util import mapping, user import mappings are respected, fully qualified names need no import), with a new containerDefaultToNull test. |
|
thanks for the PR cc @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) @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08) @KannaKim (2026/07) |
|
tested locally and the output compiles without issue. |
Fixes #25642
Since 7.26.0 (#25001), the
add<Name>Item/put<Name>Itemhelpers in the Java model templates always create anArrayList,LinkedHashSetorHashMapwhen the container isnull, ignoringinstantiationTypes. The field initializer still uses the configured type, so the two disagree, and with atypeMappingscontainer type thatArrayList/HashMapdoesn't implement (e.g.map=SequencedMapwithinstantiationTypesmap=LinkedHashMap), the model no longer compiles.As suggested in the issue,
AbstractJavaCodegennow exposes the configured instantiation type to the templates:fromPropertysets the vendor extensionx-java-container-instantiation-typeon array, set and map properties, frominstantiationTypes(array,set,map), defaulting to 'ArrayList,LinkedHashSetandHashMap`new {{{vendorExtensions.x-java-container-instantiation-type}}}<>()instead of the hard-coded type (Java client libraries, JavaSpring, JAX-RS, Play, Camel, Micronaut, MSF4J, PKMST)java.utilcollection name (e.g. 'LinkedList') gets ajava.utilimport mapping, a user import mapping is respected, and a fully qualified name needs no importGenerated with
--instantiation-types map=LinkedHashMap,array=LinkedList,set=TreeSet,Basket.java(spring):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#containerItemHelpersUseConfiguredInstantiationTypesandJavaClientCodegenTest#containerItemHelpersImportConfiguredTypesWhenFieldsDefaultToNull, with a new specsrc/test/resources/bugs/issue_25642.yaml. All four pass locally (./mvnw -pl modules/openapi-generator -am test, Windows, JDK 17).PR checklist
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.
cc Java / Java Spring technical committee: @martin-mfg @KannaKim
Summary by cubic
Fixes Java model
add<Name>Item/put<Name>Itemhelpers ignoringinstantiationTypes, so generated models with custom container types (for examplemap=SequencedMap) no longer compile. Since 7.26.0, these helpers always instantiatedArrayList,LinkedHashSet, orHashMap, while field initializers still used the configured types.AbstractJavaCodegennow exposes the configured type as thex-java-container-instantiation-typevendor extension, the 21 affected model templates use it, and generated models import the class. Default output is unchanged.java.utilnames get ajava.utilimport, user import mappings are respected, and fully qualified names need no import. This also coverscontainerDefaultToNull, where the helper is the only use.Written for commit 24a9758. Summary will update on new commits.