OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] the client generation in java instantiate Set as ArrayList

Open
#14,631 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

When generating a java client the Collections are generated as Sets but assigned to a new ArrayList if null.

For example, I have an ActivityRequest class with a list of languages. The generated client will be :

public class ActivityRequest {

  private Set<String> languages = new LinkedHashSet<>();


  public ActivityRequest addLanguagesItem(String languagesItem) {
    if (this.languages == null) {
      this.languages = new ArrayList<>();
    }
    this.languages.add(languagesItem);
    return this;
  }

}

And to add more to the issue, the ArrayList type is not even imported.

Therefore, when trying to compile the client there are failures.

Expected

I would have expected the same type as its initialization.

 public ActivityRequest addLanguagesItem(String languagesItem) {
    if (this.languages == null) {
      this.languages = new LinkedHashSet<>();
    }
    this.languages.add(languagesItem);
    return this;
  }
openapi-generator version

This is appearing in 6.3.0.
The same json input was working previously.

OpenAPI declaration file content or url

example ActivityRequest entity

Generation Details

command used to generate : openapi-generator-cli generate -i openapi.json -g java -o clients/java --additional-properties=groupId=com.company,modelPackage=com.company.client.model,invokerPackage=com.company.client,apiPackage=com.company.client.api,artifactId=api-client,artifactVersion=0.0.1-SNAPHOT,snapshotVersion=true,library=native,serializationLibrary=jackson

Suggest a fix

The problem might be related to the following PR : https://github.com/OpenAPITools/openapi-generator/pull/14130

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 provided ActivityRequest example and the openapi-generator-cli command using generator version 6.3.0. Compare the Java collection field initialization with the generated addLanguagesItem method, and review PR #14130 for related context. Done means the generated client uses a consistent collection type and compiles without the missing import failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.