-
-
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
Possible 2.16.0 Enum-as-JSON-Object serialization regression #4564
Comments
We do need Mapper configuration and rest of the enum declaration, to find out how the Meanwhile refer to below failing reproduction. public class EnumAsFormatObject4564Test
extends BaseMapTest {
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Level {
LEVEL1("level1"),
LEVEL2("level2"),
LEVEL3("level3", Level.LEVEL1);
public String label;
public Level sublevel;
Level(String level2) {
this.label = level2;
}
Level(String level3, Level level) {
this.label = level3;
this.sublevel = level;
}
}
private final ObjectMapper MAPPER = newJsonMapper();
@Test
public void testEnumAsFormatObject() throws JsonProcessingException {
List<Level> levels = new ArrayList<>();
levels.add(Level.LEVEL1);
levels.add(Level.LEVEL2);
levels.add(Level.LEVEL3);
String JSON = MAPPER.writerFor(new TypeReference<List<Level>>() {
})
.writeValueAsString(levels);
// Fails, because we get [{"label":"level1"},{"label":"level2"},{"label":"level3"}]
assertEquals("[" +
"{\"label\":\"level1\"}," +
"{\"label\":\"level2\"}," +
"{\"label\":\"level3\",\"sublevel\":{\"label\":\"level1\"}}" +
"]", JSON);
}
} |
I do not have Mapper configuration, I am just using Spring which uses jackson when returning response in a controller from a List. But I did recreate a simple java maven project using this pom :
When I am using your code (except I am not extending BaseMapTest and I add the @JsonInclude(JsonInclude.Include.NON_NULL) annotation) :
The test is ok. But when I update njackson-databind to 2.16.0 or 2.17.1, the test fails, it returns : |
There are some changes in the EnumSerializer for Jackson 2.16 - eg #4039 |
This is regression created by #3832 which improved handling of Enum's via This check in Method jackson-databind/src/main/java/com/fasterxml/jackson/databind/ser/BasicSerializerFactory.java Line 1246 in fc67817
|
Filed a fix PR #4565 against 2.16 version. |
@JooHyukKim if you think this is safe enough against 2.16 that works (and 2.16 is still open until 2.18.0 release, as per https://github.com/FasterXML/jackson/wiki/Jackson-Releases). I will try to get PR reviewed and merged soon. |
Fixed in 2.16 (for potential 2.16.3 release) and onwards (2.17.2, 2.18.0). |
Thank you ! |
@cowtowncoder when can we expect a release with this fix ? |
@px100 I am planning to release 2.17.2 today if all goes well. |
Discussed in #4563
Originally posted by Mugiwara84 June 5, 2024
Hi,
I am using jackson with spring to serialize a java enumeration which looks like this :
Before updating to 2.16.0, I was getting this when serializing :
Since 2.16.0, I'm getting :
Is this the expected behaviour ?
Is there a different way to achieve the previous result with parameters or annotations ?
The text was updated successfully, but these errors were encountered: