Skip to content

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

Open

Description

@adamszeibert

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<>();

Activity

  1. Mattias-Sehlstedt commented on Jun 11, 2026

    @Mattias-Sehlstedt
    Contributor

    Could we elaborate on why you are using generateAliasAsModel=true and then expect the behavior to be private List<Item> itemArray = new ArrayList<>(); rather than private ItemArray itemArray = null or private ItemArray itemArray = new ItemArray();?

    The whole purpose of the setting is as far as I understand just that the alias should become a concrete Java class that can be used.

  2. adamszeibert commented on Jun 12, 2026

    @adamszeibert
    Author

    Could we elaborate on why you are using generateAliasAsModel=true and then expect the behavior to be private List<Item> itemArray = new ArrayList<>(); rather than private ItemArray itemArray = null or private ItemArray itemArray = new ItemArray();?

    The whole purpose of the setting is as far as I understand just that the alias should become a concrete Java class that can be used.

    Yes, private ItemArray itemArray = new ItemArray(); will be good since ItemArray is defined as ItemArray extends ArrayList<Item> so it's technically the same as private List<Item> itemArray = new ArrayList<>();

    Thanks for the quick fix and the PR @seonwooj0810

  3. seonwooj0810 commented on Sep 1, 2026

    @seonwooj0810
    Contributor

    The defect here stands regardless of which type one expects. Today the generated field is:

    private ItemArray itemArray = new ArrayList<>();

    which does not compile: AbstractJavaCodegen.toDefaultValue(...) dereferences the $ref and emits a collection default, while the declared type stays the alias model.

    #24008 fixes it by making the default instantiate the alias instead — new ItemArray() / new ItemMap(), the output @Mattias-Sehlstedt proposed and @adamszeibert confirmed, rather than the List<Item> shape in this issue's title. null is used when the property is nullable or containerDefaultToNull is set, and the generateAliasAsModel=false path is untouched: there the property is already inlined to the collection type, so the guard doesn't fire.

    It's a 16-line change in AbstractJavaCodegen plus a Spring test, and regenerating all 197 java-* / spring* sample configs produced no diff.

    @Mattias-Sehlstedt — if you'd rather the alias default be null than new ItemArray(), the other option you offered, that's a one-line change and I'm happy to make it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions