Skip to content

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

Merged
wing328 merged 2 commits into
OpenAPITools:masterfrom
rajeshvasa:fix/java-container-helper-instantiation-types-25642
Nov 15, 2026
Merged

wing328 merged 2 commits into
OpenAPITools:masterfrom
rajeshvasa:fix/java-container-helper-instantiation-types-25642

Conversation

@rajeshvasa

@rajeshvasa rajeshvasa commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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 [BUG][JAVA] Fix generation of complex default values (fixes #24993) #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.

Written for commit 24a9758. Summary will update on new commits.

View guided diff

…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.

@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 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.
@rajeshvasa

Copy link
Copy Markdown
Contributor Author

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.

@wing328

wing328 commented Nov 15, 2026

Copy link
Copy Markdown
Member

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)

@wing328

wing328 commented Nov 15, 2026

Copy link
Copy Markdown
Member

tested locally and the output compiles without issue.

@wing328
wing328 merged commit 97260b1 into OpenAPITools:master Nov 15, 2026
15 checks passed
@wing328 wing328 added this to the 7.28.0 milestone Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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