[Java] Fix uncompilable default value for alias-as-model array/map properties (#23988)#24008
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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>), butAbstractJavaCodegen.toDefaultValue(...)dereferenced the$reffirst and produced a collection default such asnew ArrayList<>(). That value is not assignable to the alias type, so the generated field does not compile:Fix
AbstractJavaCodegen.toDefaultValue(CodegenProperty, Schema): when the property is a$refto 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 — ornullwhen the property is nullable /containerDefaultToNullis set — instead of the inlined collection:This matches the maintainer-clarified expectation in the issue thread. When
generateAliasAsModel=false, the property is inlined to the collection type (cp.isArray/cp.isMapistrue), so the guard does not fire and the existingnew ArrayList<>()behaviour is preserved.Test evidence
Added
SpringCodegenTest#shouldUseAliasModelDefaultValueForAliasAsModelProperties_issue23988(with spec3_0/spring/issue_23988.yaml) which generates withgenerateAliasAsModel=trueand asserts both the array- and map-alias fields instantiate the alias model.AbstractJavaCodegenTest(65 tests) passes — includingtoDefaultValueTestandgetTypeDeclarationTest, which cover the inlined-array/map default paths — so existing behaviour is unchanged.bin/configs/java-*andbin/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/ItemMaphave default no-arg constructors); confirmed no in-flight PR referenced #23988.PR checklist
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()), ornullwhen nullable orcontainerDefaultToNullis set.AbstractJavaCodegen.toDefaultValue(...)to detect$refto array/map aliases and returnnew <Alias>(); keeps collection defaults whengenerateAliasAsModel=false.Written for commit 2b405e1. Summary will update on new commits.