Skip to content

[Java] Use instantiationTypes when the item helpers create a container - #25648

Closed
vinirib wants to merge 2 commits into
OpenAPITools:masterfrom
vinirib:fix/25642-container-helper-instantiation-types
Closed

vinirib wants to merge 2 commits into
OpenAPITools:masterfrom
vinirib:fix/25642-container-helper-instantiation-types

Conversation

@vinirib

@vinirib vinirib commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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_issue25642 and JavaClientCodegenTest.testItemHelpersUseInstantiationTypes_issue25642: with map=LinkedHashMap, array=LinkedList and set=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 + native client generated with these instantiation types and import mappings, including a nullable array, compiles with mvn compile.
  • SpringCodegenTest, JavaClientCodegenTest, AbstractJavaCodegenTest, JavaModelTest and JavaJAXRSSpecServerCodegenTest pass (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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Built the CLI and regenerated the Java sample configs (no changes). No option or docs change.
  • 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

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 25 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

@vinirib

vinirib commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

On the review comments about missing imports: they are valid, and the gap predates this PR. On master the field initializer already renders new LinkedHashMap<>() for map=LinkedHashMap, but only the default ArrayList, LinkedHashSet and HashMap are added to the model imports. Generators without import java.util.*; (for example java with native) did not compile with custom instantiationTypes, even with --import-mappings. Since the helpers now use the same type, they had the same gap, and also for nullable containers, which have no field initializer.

159d54e adds the instantiation type to the model imports when it has an import mapping, either a default one or one passed with --import-mappings. A type without a mapping is not added, because it would be resolved as a model import.

  • JavaClientCodegenTest.testItemHelpersUseInstantiationTypes_issue25642 now also checks the java.util.LinkedHashMap, java.util.LinkedList and java.util.TreeSet imports.
  • A java + native client generated with these instantiation types and import mappings, including a nullable array, compiles with mvn compile.
  • Same suites pass (1161 tests). Regenerated 185 Java sample configs, no changes.

Since OpenAPITools#25001 the add/put item helpers of the Java pojo templates create a hard-coded ArrayList, LinkedHashSet or HashMap when the field is null, ignoring the configured instantiationTypes. AbstractJavaCodegen now exposes the instantiation type of each container property as x-container-instantiation-type, and the templates use it.
…mapping

The field initializer and the item helpers reference the configured instantiation type, but only the default ArrayList, LinkedHashSet and HashMap were imported. Generators without a java.util wildcard import, such as the native client, did not compile with custom instantiationTypes.
@vinirib
vinirib force-pushed the fix/25642-container-helper-instantiation-types branch from fb259c6 to 9f48061 Compare November 15, 2026 16:26
@vinirib

vinirib commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Closing, since #25649 fixed #25642 on master with the same approach (helpers and the instantiation type import). Thanks @AndreyVMarkelov for the review.

@vinirib vinirib closed this Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][JAVA][SPRING] instantiationTypes ignored in add…Item/put…Item helpers since 7.26.0, models no longer compile with custom typeMappings

2 participants