Repository navigation
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs. expected output?
- [Optional] Sponsorship to speed up the bug fix?
Description
For a property that references an enum schema via allOf + $ref and carries a sibling default, the Java generators (spring, java) no longer emit the field initializer for the default value.
This is a regression between 7.23.0 and 7.24.0. It appears to be a side effect of PR #23971 (fix for #23795, "Render object default for composed ($ref + default) schemas"): the composed-schema branch in AbstractJavaCodegen.toDefaultValue() was narrowed to only handle object defaults and now returns null for a composed schema that wraps an enum (or scalar).
The allOf + sibling-default construct is the standard OAS 3.0 idiom for attaching a default to a $ref (a bare default next to $ref is dropped by the parser), so this breaks a very common pattern.
openapi-generator version
- 7.24.0 — broken (regression)
- 7.23.0 — works as expected
- Regression introduced by PR [Java] Render object default for composed ($ref + default) schemas (#23795) #23971 (fix for [BUG][Java] adding default with a reference using allOf (implicitely) results in uncompileable code #23795), released in 7.24.0.
OpenAPI declaration file content or url
openapi: 3.0.3
info:
title: repro
version: 1.0.0
paths: {}
components:
schemas:
CurrencyCode:
type: string
enum: [EUR, USD]
description: Currency code
SourcePaymentAccount:
type: object
properties:
currency:
allOf:
- $ref: '#/components/schemas/CurrencyCode'
default: EURGeneration Details
generatorName: spring # also reproduces with generatorName: java
inputSpec: repro.yaml
(Reproduces with a default configuration; no special configOptions required.)
CLI:
openapi-generator-cli generate -g spring -i repro.yaml -o outSteps to reproduce
- Generate the spec above with
-g spring(or-g java) using 7.24.0. - Open the generated
SourcePaymentAccount.javaand look at thecurrencyfield.
Actual (7.24.0):
private @Nullable CurrencyCode currency;Expected (as in 7.23.0):
private CurrencyCode currency = CurrencyCode.EUR;Runtime impact: deserializing a payload without currency now yields null from getCurrency() instead of the declared default EUR.
Root cause (analysis)
In AbstractJavaCodegen.toDefaultValue() the composed-schema branch (as of 7.24.0 / PR #23971) is:
else if (ModelUtils.isComposedSchema(schema)) {
if (schema.getDefault() != null) {
Map<String, Schema> propertySchemas = getComposedSchemaProperties(schema);
if (!propertySchemas.isEmpty()) {
return toObjectDefaultValue(cp, schema.getDefault(), propertySchemas);
}
return null; // <-- enum/scalar-wrapping allOf: no properties -> default is dropped
}
return null;
}For an allOf wrapping an object, getComposedSchemaProperties() is non-empty and toObjectDefaultValue(...) renders the default. For an allOf wrapping an enum (or scalar), propertySchemas is empty, so the method returns null and the enum default is lost. Prior to 7.24.0 this path resolved the referenced enum and emitted CurrencyCode.EUR.
Suggested fix
In the composed-schema branch, when propertySchemas is empty, fall back to the pre-7.24.0 behavior instead of returning null: resolve the referenced schema (ModelUtils.getReferencedSchema(...) over the allOf member) and, if it is an enum/scalar, delegate to the existing enum/scalar default rendering (i.e. reuse the same logic that produces CurrencyCode.EUR for a directly-referenced enum-with-default). This keeps the new object-default behavior from #23795 intact while restoring enum/scalar defaults on composed schemas.
Related issues/PRs
- [BUG][Java] adding default with a reference using allOf (implicitely) results in uncompileable code #23795 — Render object default for composed (
$ref+ default) schemas (the change whose fix, PR [Java] Render object default for composed ($ref + default) schemas (#23795) #23971, introduced this regression) - [Java] Render object default for composed ($ref + default) schemas (#23795) #23971 — PR that closed [BUG][Java] adding default with a reference using allOf (implicitely) results in uncompileable code #23795 (shipped in 7.24.0)
- [BUG] default values for references enums are not generated correctly #14689, [BUG][Java] Enum default value generated as string instead of enum value #15144 — older, different enum-default problems (invalid/string output), not this regression
Activity
wing328 commentedon Jul 22, 2026
@seonwooj0810 can you please take a look when you've time?
williechieukam commentedon Jul 23, 2026
@wing328 & @seonwooj0810
When can we please expect a release for this fix.
Many thanks in advance
fix(java): render enum default on allOf + $ref composed property (Ope…