Skip to content

fix(java): render enum default on allOf + $ref composed property (#24384)#24385

Open
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24384-composed-enum-default
Open

fix(java): render enum default on allOf + $ref composed property (#24384)#24385
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24384-composed-enum-default

Conversation

@seonwooj0810

@seonwooj0810 seonwooj0810 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #24384

Root cause

A property that references an enum via allOf + $ref with a sibling default (the standard OAS 3.0 idiom for attaching a default to 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 via getComposedSchemaProperties() and, when that set is empty, returned null. 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 to super.toDefaultValue(...).

Runtime impact: deserializing a payload without the property now yields null from 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 through toObjectDefaultValue(...), so the #23795 fix is preserved.

For the reporter's spec the spring/java generators again emit private CurrencyCode currency = CurrencyCode.EUR; instead of private @Nullable CurrencyCode currency;.

Test

Added AbstractJavaCodegenTest#toDefaultValueForComposedEnumWithDefaultTest, a sibling to the existing toDefaultValueForComposedObjectWithDefaultTest. It builds an allOf → enum $ref composed schema with a sibling default and asserts the rendered default is preserved. The test fails on current master (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 + $ref properties were dropped. Restores proper field initialization so missing values use the declared enum default instead of null.

  • Bug Fixes
    • In AbstractJavaCodegen.toDefaultValue, when a composed schema has no object properties, fall back to super.toDefaultValue(...) to preserve non-object defaults (enums/scalars).
    • Keeps object default rendering intact; only affects non-object compositions.
    • Adds a unit test for allOf + $ref enum with a sibling default to prevent regressions.

Written for commit ccded63. Summary will update on new commits.

Review in cubic

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

@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 2 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] Enum default on allOf + $ref property is no longer generated (regression in 7.24.0)

1 participant