Skip to content

[Java] Fix uncompilable default value for alias-as-model array/map properties (#23988)#24008

Open
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-23988-alias-array-default-value
Open

[Java] Fix uncompilable default value for alias-as-model array/map properties (#23988)#24008
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-23988-alias-array-default-value

Conversation

@seonwooj0810

@seonwooj0810 seonwooj0810 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #23988

What

When generateAliasAsModel=true, a property that directly $refs an array or map alias is typed as the generated alias model (e.g. ItemArray extends ArrayList<Item>), but AbstractJavaCodegen.toDefaultValue(...) dereferenced the $ref first and produced a collection default such as new ArrayList<>(). That value is not assignable to the alias type, so the generated field does not compile:

private ItemArray itemArray = new ArrayList<>(); // ItemArray extends ArrayList<Item> -> does not compile

Fix

AbstractJavaCodegen.toDefaultValue(CodegenProperty, Schema): when the property is a $ref to an array/map alias (so it is neither an inline array nor map itself) and the referenced schema is an array/map (i.e. it is generated as an alias model), instantiate the alias model — or null when the property is nullable / containerDefaultToNull is set — instead of the inlined collection:

private ItemArray itemArray = new ItemArray();
private ItemMap itemMap = new ItemMap();

This matches the maintainer-clarified expectation in the issue thread. When generateAliasAsModel=false, the property is inlined to the collection type (cp.isArray/cp.isMap is true), so the guard does not fire and the existing new ArrayList<>() behaviour is preserved.

Test evidence

Added SpringCodegenTest#shouldUseAliasModelDefaultValueForAliasAsModelProperties_issue23988 (with spec 3_0/spring/issue_23988.yaml) which generates with generateAliasAsModel=true and asserts both the array- and map-alias fields instantiate the alias model.

  • New test passes.
  • Full AbstractJavaCodegenTest (65 tests) passes — including toDefaultValueTest and getTypeDeclarationTest, which cover the inlined-array/map default paths — so existing behaviour is unchanged.
  • Regenerated all bin/configs/java-* and bin/configs/spring* samples (197 generators): no diff, so no committed mature sample changes.

Verification done: reproduced the uncompilable output on master with the issue's spec via the CLI; confirmed the corrected output compiles (ItemArray/ItemMap have default no-arg constructors); confirmed no in-flight PR referenced #23988.

PR checklist

  • Read the contribution guidelines.
  • Ran the build and sample regeneration for the affected generators (Java/Spring); no sample changes resulted.
  • @mention technical committee — deferring to maintainers.

Summary by cubic

Fixes uncompilable defaults for properties that $ref array/map aliases when generateAliasAsModel=true. Defaults now instantiate the alias model (e.g., new ItemArray()/new ItemMap()), or null when nullable or containerDefaultToNull is set.

  • Bug Fixes
    • Updated AbstractJavaCodegen.toDefaultValue(...) to detect $ref to array/map aliases and return new <Alias>(); keeps collection defaults when generateAliasAsModel=false.
    • Added a Spring test and spec to assert alias model defaults for array and map properties.
    • No sample diffs; existing tests pass.

Written for commit 2b405e1. Summary will update on new commits.

Review in cubic

…operties

When generateAliasAsModel is enabled, a property that $refs an array/map
alias is typed as the generated alias model (e.g. "ItemArray extends
ArrayList<Item>"), but toDefaultValue dereferenced the $ref and produced a
collection default such as "new ArrayList<>()", which is not assignable to
the alias type and does not compile. Use the alias model's own default
("new ItemArray()") instead.

Fixes OpenAPITools#23988

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: seonwoo_jung <79202163+seonwooj0810@users.noreply.github.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

Re-trigger cubic

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant