Repository navigation
[Java] Fix uncompilable default value for alias-as-model array/map properties (#23988) - #24008
seonwooj0810 wants to merge 3 commits into
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) <[email protected]>
Signed-off-by: seonwoo_jung <[email protected]>
|
Flagging this one for review whenever there's bandwidth — happy to regenerate samples again if main has moved since. |
Signed-off-by: seonwoo_jung <[email protected]>
|
Merged current master in 95f32e4 to resolve the conflict in Verified on JDK 21:
|
Sekator778
left a comment
There was a problem hiding this comment.
Reproduced #23988 on master, and with this Spring and okhttp-gson compile. LGTM.
|
Thanks for reproducing and reviewing, @Sekator778! |
|
I tested an array alias with default: [hello]. With generateAliasAsModel=false, the generated field is initialized with ["hello"]; with generateAliasAsModel=true, it is initialized with an empty ItemArray. Is this difference expected, or should explicit alias defaults be handled separately? It may be useful to add a test documenting the intended behavior. |
|
Not intended, it is a limitation of this fix: the generated alias model only has a no-arg constructor, so with |
There was a problem hiding this comment.
2 issues found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java:1458">
P2: This `$ref` branch also matches object models that have `properties` plus `additionalProperties`, because `isMapSchema` is true for them. Those are real model classes, not aliases, so they now get `new Foo()` where the existing map branch returned `null`. Exclude schemas with properties here so only alias-style maps take this path.</violation>
</file>
<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java:7176">
P2: This test asserts that the spec default `[hello]` on `ItemArray` is silently dropped when `generateAliasAsModel=true`. Pinning that output in a test means any future fix for the default will fail this test. Either leave the assertion out, or assert that the limitation is surfaced (for example, a warning) if the codegen emits one.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| // "new ArrayList<>()", which is not assignable to the alias type and does not compile. | ||
| // Use the alias model's own default instead. | ||
| // See https://github.com/OpenAPITools/openapi-generator/issues/23988 | ||
| if (originalSchema.get$ref() != null && !cp.isArray && !cp.isMap) { |
There was a problem hiding this comment.
P2: This $ref branch also matches object models that have properties plus additionalProperties, because isMapSchema is true for them. Those are real model classes, not aliases, so they now get new Foo() where the existing map branch returned null. Exclude schemas with properties here so only alias-style maps take this path.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, line 1458:
<comment>This `$ref` branch also matches object models that have `properties` plus `additionalProperties`, because `isMapSchema` is true for them. Those are real model classes, not aliases, so they now get `new Foo()` where the existing map branch returned `null`. Exclude schemas with properties here so only alias-style maps take this path.</comment>
<file context>
@@ -1449,6 +1449,21 @@ public String toDefaultValue(CodegenProperty cp, Schema schema) {
+ // "new ArrayList<>()", which is not assignable to the alias type and does not compile.
+ // Use the alias model's own default instead.
+ // See https://github.com/OpenAPITools/openapi-generator/issues/23988
+ if (originalSchema.get$ref() != null && !cp.isArray && !cp.isMap) {
+ if (ModelUtils.isArraySchema(resolvedSchema)
+ || (ModelUtils.isMapSchema(resolvedSchema) && !ModelUtils.isComposedSchema(resolvedSchema))) {
</file context>
| // generateAliasAsModel=true: known limitation. The generated alias model only has a no-arg | ||
| // constructor, so the explicit default values cannot be expressed in a field initializer and | ||
| // the field falls back to an empty alias instance (previously model generation failed here). | ||
| JavaFileAssert.assertThat(generateIssue23988DefaultModel(true).get("MyObject.java")) |
There was a problem hiding this comment.
P2: This test asserts that the spec default [hello] on ItemArray is silently dropped when generateAliasAsModel=true. Pinning that output in a test means any future fix for the default will fail this test. Either leave the assertion out, or assert that the limitation is surfaced (for example, a warning) if the codegen emits one.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java, line 7176:
<comment>This test asserts that the spec default `[hello]` on `ItemArray` is silently dropped when `generateAliasAsModel=true`. Pinning that output in a test means any future fix for the default will fail this test. Either leave the assertion out, or assert that the limitation is surfaced (for example, a warning) if the codegen emits one.</comment>
<file context>
@@ -7131,6 +7131,77 @@ public void shouldAddNullableImportForArrayTypeModels() throws IOException {
+ // generateAliasAsModel=true: known limitation. The generated alias model only has a no-arg
+ // constructor, so the explicit default values cannot be expressed in a field initializer and
+ // the field falls back to an empty alias instance (previously model generation failed here).
+ JavaFileAssert.assertThat(generateIssue23988DefaultModel(true).get("MyObject.java"))
+ .fileContains("private ItemArray itemArray = new ItemArray();");
+ }
</file context>
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
$refarray/map aliases whengenerateAliasAsModel=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.generateAliasAsModel=true.Written for commit 86ccb52. Summary will update on new commits.