[BUG][JAVA] Render enum constants in object property defaults (fixes #24298)#24419
Open
seonwooj0810 wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #24298
Problem
When a schema defines a
defaultfor an object property whose nested properties include an enum, the Java generator rendered the enum value as a raw quotedString, producing code that does not compile:AbstractJavaCodegen#toObjectDefaultValuebuilds the fluent-builder expression for object defaults (added in #23795) but had no branch for enum-typed properties, so an enum fell through to theisStringSchemabranch and got quoted.Fix
toObjectDefaultValuenow detects enum-typed properties (inline or$ref) and emits the enum constant, mirroring the existing array-of-enum default handling (cp.items.datatypeWithEnum + "." + toEnumVarName(...)):For an inline enum the constant is qualified with the containing object type (
OutputFormat.OrderEnum.…); for a$refto 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 frombugs/issue_24298.yaml(the spec from the issue) asserting the generatedFormatter.javafield 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.shproduces 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 intoObjectDefaultValue, 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.
AbstractJavaCodegen#toObjectDefaultValue, detect enum-typed properties (inline or$ref) and renderType.EnumName.CONSTANT(e.g.,OutputFormat.OrderEnum.SIMILARITY), mirroring array-of-enum handling.bugs/issue_24298.yamlto assertFormatter.javainitializes with the enum constant.Written for commit 1c97451. Summary will update on new commits.