Bug Report Checklist
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
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: EUR
Generation 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 out
Steps to reproduce
- Generate the spec above with
-g spring (or -g java) using 7.24.0.
- Open the generated
SourcePaymentAccount.java and look at the currency field.
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 Report Checklist
Description
For a property that references an enum schema via
allOf+$refand carries a siblingdefault, 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 inAbstractJavaCodegen.toDefaultValue()was narrowed to only handle object defaults and now returnsnullfor a composed schema that wraps an enum (or scalar).The
allOf+ sibling-defaultconstruct is the standard OAS 3.0 idiom for attaching adefaultto a$ref(a baredefaultnext to$refis dropped by the parser), so this breaks a very common pattern.openapi-generator version
OpenAPI declaration file content or url
Generation Details
(Reproduces with a default configuration; no special
configOptionsrequired.)CLI:
Steps to reproduce
-g spring(or-g java) using 7.24.0.SourcePaymentAccount.javaand look at thecurrencyfield.Actual (7.24.0):
Expected (as in 7.23.0):
Runtime impact: deserializing a payload without
currencynow yieldsnullfromgetCurrency()instead of the declared defaultEUR.Root cause (analysis)
In
AbstractJavaCodegen.toDefaultValue()the composed-schema branch (as of 7.24.0 / PR #23971) is:For an
allOfwrapping an object,getComposedSchemaProperties()is non-empty andtoObjectDefaultValue(...)renders the default. For anallOfwrapping an enum (or scalar),propertySchemasis empty, so the method returnsnulland the enum default is lost. Prior to 7.24.0 this path resolved the referenced enum and emittedCurrencyCode.EUR.Suggested fix
In the composed-schema branch, when
propertySchemasis empty, fall back to the pre-7.24.0 behavior instead of returningnull: resolve the referenced schema (ModelUtils.getReferencedSchema(...)over theallOfmember) and, if it is an enum/scalar, delegate to the existing enum/scalar default rendering (i.e. reuse the same logic that producesCurrencyCode.EURfor 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
$ref+ default) schemas (the change whose fix, PR [Java] Render object default for composed ($ref + default) schemas (#23795) #23971, introduced this regression)