OpenAPITools / OpenAPITools/openapi-generator

[BUG] The runtime-type specified in instantiation-types option is not respected

Open
#2,704 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Java Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
Description

The OpenAPI Generator does not generate the runtime-type specified in the --instantiation-types mapping option. The generated code defaults to the default runtime-time ArrayList for array and HashMap for map.

openapi-generator version

3.3.4, 4.0.0-beta3

OpenAPI declaration file content or url
Pet:
  type: object
  properties:
    owners:
      type: array
      items:
        $ref: "#/components/schemas/Owner"
Owner:
  type: object
  properties:
     name:
        type: string
  ....
  ....
Command line used for generation

openapi-generator generate -i petstore.yaml -g java --instantiation-types=array=HashSet --type-mappings=array=Set --import-mappings=HashSet=java.util.HashSet

Steps to reproduce

Generate code with the above-specified command

Generated code in Pet.java

public Pet addOwnersItem(Owner ownersItem) {
  if (this.owners == null) {
    this.owners = new ArrayList<Owner>();
  }
  this.owners.add(ownersItem);
  return this;
}

Expected code

public Pet addOwnersItem(Owner ownersItem) {
  if (this.owners == null) {
    this.owners = new HashSet<Owner>();
  }
  this.owners.add(ownersItem);
  return this;
}
Related issues/PRs

None

Suggest a fix

Suggest fix would be to correctly interpret instantiation-types and import-mappings options and adjust code-gen logic to generate appropriate runtime type.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the openapi-generator generate command and the supplied OpenAPI declaration, then inspect how the Java generator handles --instantiation-types, --type-mappings, and --import-mappings. Compare the generated Pet.java initializer with the expected HashSet<Owner> form; done means the configured runtime type is emitted instead of ArrayList.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.