fix: exclude bridge methods from @JsonValue enum detection (#5127)#5128
Open
olsavmic wants to merge 1 commit intoswagger-api:masterfrom
Open
fix: exclude bridge methods from @JsonValue enum detection (#5127)#5128olsavmic wants to merge 1 commit intoswagger-api:masterfrom
olsavmic wants to merge 1 commit intoswagger-api:masterfrom
Conversation
…pi#5127) When an enum implements a generic interface (e.g. PersistableEnum<String>), the compiler generates a bridge method with @jsonvalue copied from the real method. ReflectionUtils.getAnnotatedMethods() returned both, and findFirst() in _createSchemaForEnum() picked whichever getDeclaredMethods() returned first — which is non-deterministic per JVM spec. If the bridge method (returning Object) was picked, PrimitiveType.fromType(Object.class) returned null, causing the enum schema to lose its "type":"string" in OpenAPI 3.1 mode. Filter bridge methods in collectAnnotatedDeclaredMethods() to ensure only the real annotated method is found.
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.
Summary
Fixes #5127 —
@JsonValueon an enum implementing a generic interface (e.g.PersistableEnum<String>) causes non-deterministic schema generation in OpenAPI 3.1 mode.Root cause
When an enum implements a generic interface like
PersistableEnum<T>, the Java compiler generates a bridge methodObject getValue()alongside the realString getValue(). The compiler copies@JsonValueto the bridge method.ReflectionUtils.getAnnotatedMethods()usesgetDeclaredMethods()without filtering bridge methods, so both methods are returned. InModelResolver._createSchemaForEnum(),findFirst()picks whichevergetDeclaredMethods()returns first — which is unspecified per JVM spec.String getValue()wins →PrimitiveType.fromType(String.class)→ schema withtype: "string"✓Object getValue()(bridge) wins →PrimitiveType.fromType(Object.class)→null→ falls through to defaultThis causes non-deterministic output: the same code produces different schemas across JVM invocations (e.g. Maven Surefire vs IntelliJ test runner).
Fix
One-line change in
ReflectionUtils.collectAnnotatedDeclaredMethods()to skip bridge methods:Tests added
ReflectionUtilsTest.getAnnotatedMethods_excludesBridgeMethodsFromGenericInterface— verifies only the non-bridge method is returnedEnumPropertyTest.testJsonValueWithBridgeMethodFromGenericInterface— end-to-end: enum with generic interface gets correcttype: "string"and enum valuesPersistableEnum<T>interface andJacksonValueBridgeMethodEnumenum