fix(java): render enum default on allOf + $ref composed property (#24384)#24385
Open
seonwooj0810 wants to merge 1 commit into
Open
fix(java): render enum default on allOf + $ref composed property (#24384)#24385seonwooj0810 wants to merge 1 commit into
seonwooj0810 wants to merge 1 commit into
Conversation
A property using the standard OAS 3.0 idiom of a $ref to an enum plus a sibling default (parsed as a composed allOf schema) no longer emitted the field initializer, so deserializing a payload without the property yielded null instead of the declared default. AbstractJavaCodegen.toDefaultValue's composed-schema branch (added in OpenAPITools#23971 to render object defaults) returned null whenever getComposedSchemaProperties resolved no object properties, which is the case for a composition wrapping an enum or scalar. Restore the pre-OpenAPITools#23971 behavior for that case by deferring to super.toDefaultValue, which emits the raw default for later enum var-name conversion. Fixes OpenAPITools#24384
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 #24384
Root cause
A property that references an enum via
allOf+$refwith a siblingdefault(the standard OAS 3.0 idiom for attaching adefaultto a$ref) is parsed as a composed schema.AbstractJavaCodegen.toDefaultValue()'s composed-schema branch — added in #23971 to render object defaults — resolves the composition's effective properties viagetComposedSchemaProperties()and, when that set is empty, returnednull. For a composition wrapping an enum (or any non-object) there are no object properties, so the default was dropped. This is a regression between 7.23.0 and 7.24.0: previously this branch fell through tosuper.toDefaultValue(...).Runtime impact: deserializing a payload without the property now yields
nullfrom the getter instead of the declared enum default.Change
In the composed branch, when no object properties can be resolved, defer to
super.toDefaultValue(schema)(the pre-#23971 behavior) so the raw default is emitted for later enum var-name conversion. Object compositions are unaffected and still go throughtoObjectDefaultValue(...), so the #23795 fix is preserved.For the reporter's spec the
spring/javagenerators again emitprivate CurrencyCode currency = CurrencyCode.EUR;instead ofprivate @Nullable CurrencyCode currency;.Test
Added
AbstractJavaCodegenTest#toDefaultValueForComposedEnumWithDefaultTest, a sibling to the existingtoDefaultValueForComposedObjectWithDefaultTest. It builds anallOf→ enum$refcomposed schema with a siblingdefaultand asserts the rendered default is preserved. The test fails on currentmaster(expected [EUR] but found [null]) and passes with this change.Verification done:
./mvnw -pl modules/openapi-generator -am test -Dtest=AbstractJavaCodegenTest#...— both the new enum test and the existing object test pass (2 run, 0 failures); confirmed the new test fails without the source change. No templates/samples touched, so no sample regeneration is required.Summary by cubic
Fixes a regression in Java generators where enum defaults on
allOf+$refproperties were dropped. Restores proper field initialization so missing values use the declared enum default instead of null.AbstractJavaCodegen.toDefaultValue, when a composed schema has no object properties, fall back tosuper.toDefaultValue(...)to preserve non-object defaults (enums/scalars).allOf+$refenum with a siblingdefaultto prevent regressions.Written for commit ccded63. Summary will update on new commits.