Skip to content

[BUG][Java] Enum default on allOf + $ref property is no longer generated (regression in 7.24.0) #24384

Description

@williechieukam

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

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

  1. Generate the spec above with -g spring (or -g java) using 7.24.0.
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions