[BUG][JAVA] Render enum constants in object property defaults (fixes #24298)

Merged
SE
3mo ago
+77-1

Fixes #24298

Problem

When a schema defines a default for an object property whose nested properties include an enum, the Java generator rendered the enum value as a raw quoted String, producing code that does not compile:

// Actual (does not compile)
private OutputFormat format = new OutputFormat().order("SIMILARITY").limit(10);

AbstractJavaCodegen#toObjectDefaultValue builds the fluent-builder expression for object defaults (added in #23795) but had no branch for enum-typed properties, so an enum fell through to the isStringSchema branch and got quoted.

Fix

toObjectDefaultValue now detects enum-typed properties (inline or $ref) and emits the enum constant, mirroring the existing array-of-enum default handling (cp.items.datatypeWithEnum + "." + toEnumVarName(...)):

// Expected (compiles)
private OutputFormat format = new OutputFormat().order(OutputFormat.OrderEnum.SIMILARITY).limit(10);

For an inline enum the constant is qualified with the containing object type (OutputFormat.OrderEnum.…); for a $ref to a named enum the top-level type is used directly.

Tests

  • AbstractJavaCodegenTest#toDefaultValueForObjectWithEnumPropertyDefaultTest — unit test asserting the rendered fluent expression.
  • JavaClientCodegenTest#testObjectDefaultWithEnumProperty_issue24298 — end-to-end generation from bugs/issue_24298.yaml (the spec from the issue) asserting the generated Formatter.java field uses the enum constant.

Both fail before the change and pass after. The full AbstractJavaCodegenTest (67 tests) passes.

Samples

No committed sample exercises object-property defaults (no generated model field uses a new X().... fluent initializer), so ./bin/generate-samples.sh produces no diff for this change. Verified with the module test suites above.

Verification done: (1) confirmed no in-flight PR (gh pr list --search); (3) reproduced the bug from the issue spec, located the root cause in toObjectDefaultValue, and confirmed the fix with unit + end-to-end tests on JDK21.

AI disclosure

This change was prepared with the assistance of an AI coding tool; the fix, tests and analysis were reviewed for correctness before submission.


Summary by cubic

Fixes #24298 by rendering enum constants in object property defaults in the Java generator so generated code compiles. Enum defaults are now emitted as constants instead of quoted strings.

  • Bug Fixes
    • In AbstractJavaCodegen#toObjectDefaultValue, detect enum-typed properties (inline or $ref via ModelUtils.getReferencedSchema) and render Type.EnumName.CONSTANT (e.g., OutputFormat.OrderEnum.SIMILARITY), mirroring array-of-enum handling.
    • Added unit and E2E tests with bugs/issue_24298.yaml to assert Formatter.java initializes the enum with the constant.