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

SE
23d ago
+131-0

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 Spring tests and specs asserting alias model defaults for array and map properties, including a known limitation: explicit alias defaults fall back to an empty alias instance when generateAliasAsModel=true.
    • No sample diffs; existing tests pass.