Skip to content

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

Open
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24298-object-default-enum-property
Open

[BUG][JAVA] Render enum constants in object property defaults (fixes #24298)#24419
seonwooj0810 wants to merge 1 commit into
OpenAPITools:masterfrom
seonwooj0810:fix/issue-24298-object-default-enum-property

Conversation

@seonwooj0810

@seonwooj0810 seonwooj0810 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

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) and render Type.EnumName.CONSTANT (e.g., OutputFormat.OrderEnum.SIMILARITY), mirroring array-of-enum handling.
    • Added unit and E2E tests using bugs/issue_24298.yaml to assert Formatter.java initializes with the enum constant.

Written for commit 1c97451. Summary will update on new commits.

Review in cubic

An object default that contains an enum property was rendered with the
raw value quoted as a String (e.g. `.order("SIMILARITY")`), which does
not compile. toObjectDefaultValue now resolves enum-typed properties and
emits the enum constant (`.order(OutputFormat.OrderEnum.SIMILARITY)`),
mirroring the existing array-of-enum default handling.

Fixes OpenAPITools#24298

@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 4 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] Object defaults with enum properties generate invalid fluent builder expressions

1 participant