[Java] Use instantiationTypes when the item helpers create a container
fix #25642
Since #25001 the add<Name>Item and put<Name>Item helpers in the Java pojo.mustache templates create a hard-coded ArrayList, LinkedHashSet or HashMap when the field is null. The field initializer still uses instantiationTypes, so the two disagree. With a type mapping such as map=SequencedMap the model does not compile, and with only map=LinkedHashMap the helper silently switches to a HashMap.
{{{defaultValue}}} cannot go back into the helpers, because since #25001 it can be a filled container built from the schema default. Instead, AbstractJavaCodegen.postProcessModelProperty now sets x-container-instantiation-type on each container property from instantiationTypes (array, set or map), and the 21 Java templates touched by #25001 use it in the helpers. With the default instantiation types the generated code is unchanged.
The configured instantiation type is also added to the model imports when it has an import mapping (a default one or one from --import-mappings). Before this, only ArrayList, LinkedHashSet and HashMap were imported, so the field initializer already failed to compile with custom instantiationTypes in generators without import java.util.*;, such as the native client.
Tests:
SpringCodegenTest.testItemHelpersUseInstantiationTypes_issue25642andJavaClientCodegenTest.testItemHelpersUseInstantiationTypes_issue25642: withmap=LinkedHashMap,array=LinkedListandset=TreeSet, the helpers create those types, and the Java client model imports them. The Spring test fails without the fix (this.entries = new HashMap<>();).- A
java+nativeclient generated with these instantiation types and import mappings, including a nullable array, compiles withmvn compile. SpringCodegenTest,JavaClientCodegenTest,AbstractJavaCodegenTest,JavaModelTestandJavaJAXRSSpecServerCodegenTestpass (1161 tests).- Regenerated 185 Java, Spring, JAX-RS, Micronaut, Play, PKMST, MSF4J and Camel sample configs: no changes, since no sample config sets
instantiationTypes.
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
Built the CLI and regenerated the Java sample configs (no changes). No option or docs change../mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Java technical committee: @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg @KannaKim @AndreyVMarkelov
Java Spring technical committee: @cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente
cc @jorgerod, who changed these helpers in #25001