OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Generated java code with generateAliasAsModel.set(true) not compiling

Open
#15,072 1 comment 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

Description

given schema with

(...)
    ProviderDetails:
      type: object
      additionalProperties:
        type: string
      example:
        awsRegion: us-east-1
(...)        
    Database:
      type: object
      properties:
        providerDetails:
          $ref: '#/components/schemas/ProviderDetails'
(...)

openapi-generator used :
gradle plugin
id("org.openapi.generator") version "6.4.0"

generator : spring

Java code generated


@Schema(name = "ProviderDetails")
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class ProviderDetails extends HashMap<String, String> {
(...)
}

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public class Database {


  @JsonProperty("providerDetails")
  private ProviderDetails providerDetails = new HashMap<>();
  (...)
}

ProviderDetails providerDetails = new HashMap<>();
does not compile
I would expect either

private ProviderDetails providerDetails;
or
private ProviderDetails providerDetails = new ProviderDetails;

Is there a fix or workaround that can be used (updated mustache template) ?

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 schema, the Spring generator, and the Gradle plugin using generateAliasAsModel.set(true). Inspect the Spring generator templates responsible for Database and ProviderDetails initialization, then verify that the generated Java compiles and uses a valid ProviderDetails initialization or no initialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, devtools
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.