OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][MAVEN] Array aliases generate uncompilable Java code with generateAliasAsModel=true

Open
#23,988 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?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I have a schema called Item, I have array of Item properties in many schemas so I made an ItemArray alias to make my property definition shorter. I have generateAliasAsModel=true because I have other schema aliases. The generated Java code is
private ItemArray itemArray = new ArrayList<>();

which obviously does not compile. The correct code in this case should be
private List<Item> itemArray = new ArrayList<>();

openapi-generator version

7.23.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Test
  version: 0.0.1
paths: {}

components:
  schemas:
    Item:
      type: object
      properties:
        property:
          type: string
    ItemArray:
      type: array
      items:
        $ref: '#/components/schemas/Item'
    MyObject:
      type: object
      properties:
        id:
          type: string
          format: uuid
        itemArray:
          $ref: '#/components/schemas/ItemArray'
Generation Details

config from pom.xml

<generateSupportingFiles>false</generateSupportingFiles>
<generatorName>spring</generatorName>
<strictSpec>true</strictSpec>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateAliasAsModel>true</generateAliasAsModel>
<configOptions>
    <legacyDiscriminatorBehavior>true</legacyDiscriminatorBehavior>
    <library>spring-boot</library>
    <oas3>true</oas3>
    <useSpringController>true</useSpringController>
    <useSpringBoot3>true</useSpringBoot3>
    <useSpringfox>false</useSpringfox>
    <interfaceOnly>true</interfaceOnly>
    <skipDefaultInterface>true</skipDefaultInterface>
    <useBeanValidation>true</useBeanValidation>
    <useEnumCaseInsensitive>false</useEnumCaseInsensitive>
    <useClassLevelBeanValidation>false</useClassLevelBeanValidation>
    <serializableModel>true</serializableModel>
    <useTags>true</useTags>
    <javaNative>21</javaNative>
    <useOptional>false</useOptional>
    <hideGenerationTimestamp>true</hideGenerationTimestamp>
    <dateLibrary>java8</dateLibrary>
    <bigDecimalAsString>true</bigDecimalAsString>
    <generateApiUtil>false</generateApiUtil>
    <openApiNormalizer>RULE_1=true</openApiNormalizer>
    <apiPackage>com.test.api</apiPackage>
    <modelPackage>com.test.model</modelPackage>
</configOptions>

Steps to reproduce

The generated Java code is always wrong.

Related issues/PRs
Suggest a fix

The generated Java code should be
private List<Item> itemArray = new ArrayList<>();
instead of
private ItemArray itemArray = new ArrayList<>();

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

Start with the OpenAPI declaration and the pom.xml generator configuration, reproducing the issue with the spring generator and generateAliasAsModel=true. Trace Java model generation for the ItemArray alias and add a regression test showing that the generated field uses List and compiles instead of ItemArray.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.