-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StackOverflowError for enum serialization since 2.18.2 #4906
Comments
Please provide the stackoverflowerror and trace. We don't need full trace but include a couple of cycles where the code is presumably running through the same set of calls. |
Oups... sorry @pjfanning , I had prepared it and put it in the description, but it seems I deleted it somehow...
|
Looks like this change may be the cause of the change. #4788 @JooHyukKim @cowtowncoder It does see to me calling objectMapper.convertValue in a toString is not a great idea even if it worked before. |
Ok, thanks. If it's not a great idea to call |
I guess I was asking the opinion of @JooHyukKim and @cowtowncoder. One enhancement that we could add to https://github.com/FasterXML/jackson-databind/blame/2.18/src/main/java/com/fasterxml/jackson/databind/util/EnumValues.java#L117 would be to check if the enum instance has a JsonProperty annotaton before checking its toString. @nvervelle you could also check the for the JsonProperty annotation in your toString call. I would expect some trial and error trying to find the best way to track down the annotation. |
Uhhh. This:
is a HORRIBLE, TERRIBLE, NO GOOD idea. Please, please do not do that. Calling Any code that does that is basically something that voids any warranties. So: while there is no intended change to break things, I think I will close this as "wont fix". Calling As to how to avoid duplication: I am not sure. If |
Ok, I'm replacing the code for the toString() method to retrieve the If anyone is interested, here's my utility class for that public final class EnumUtil {
/** Constructor. Private to avoid instantiation */
private EnumUtil() {}
public static <E extends Enum<E>> String getJsonValue(final E value) {
try {
final JsonProperty jsonProperty =
value.getDeclaringClass().getField(value.name()).getAnnotation(JsonProperty.class);
if (jsonProperty == null) {
return value.name();
}
return jsonProperty.value();
} catch (NoSuchFieldException e) {
return value.name();
}
}
} |
@nvervelle Great! Thank you for sharing. |
Search before asking
Describe the bug
When trying to upgrade my project with spring-boot 3.4.1 (which brings Jackson 2.18.2) instead of spring-boot 3.4.0, my project build fails in many places due to
StackOverflowError
when serializing values, forenum
.By looking at the stacktrace, it seems that it's because :
toString()
method of theenum
: so that thetoString()
method would return the same value as the one that will be used when serializing to JSON...enum
now calls thetoString()
method of theenum
=> which leads to recursive calls and the stack overflow...Is it voluntary that this constructs now fails with 2.18.2 even if it was working with 2.18.1 ?
If it's voluntary or will stay that way, how can we achieve the same behavior as before ? (the
toString()
method returning the same value as the serialized value specified with@JsonProperty
Version Information
2.18.2
Reproduction
Expected behavior
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: