Repository navigation
Open
Labels
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<>();
Activity
Mattias-Sehlstedt commentedon Jun 11, 2026
Could we elaborate on why you are using
generateAliasAsModel=trueand then expect the behavior to beprivate List<Item> itemArray = new ArrayList<>();rather thanprivate ItemArray itemArray = nullorprivate 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.
adamszeibert commentedon Jun 12, 2026
Yes,
private ItemArray itemArray = new ItemArray();will be good since ItemArray is defined asItemArray extends ArrayList<Item>so it's technically the same asprivate List<Item> itemArray = new ArrayList<>();Thanks for the quick fix and the PR @seonwooj0810
seonwooj0810 commentedon Sep 1, 2026
The defect here stands regardless of which type one expects. Today the generated field is:
which does not compile:
AbstractJavaCodegen.toDefaultValue(...)dereferences the$refand 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 theList<Item>shape in this issue's title.nullis used when the property is nullable orcontainerDefaultToNullis set, and thegenerateAliasAsModel=falsepath 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
AbstractJavaCodegenplus a Spring test, and regenerating all 197java-*/spring*sample configs produced no diff.@Mattias-Sehlstedt — if you'd rather the alias default be
nullthannew ItemArray(), the other option you offered, that's a one-line change and I'm happy to make it.