-
Notifications
You must be signed in to change notification settings - Fork 38k
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
Jackson2ObjectMapperBuilder's modulesToInstall function does not eventually override the default configuration #22576
Comments
|
@wilkinsona , thanks for the response. I will put it as a separate repository as soon as possible. |
I have created a demo app with 2 object mapper configs. https://github.com/elue/object-mapper-demo.git |
Thanks for the sample. It can be made quite a bit more minimal, removing Spring Boot's involvement entirely: package com.github.example;
import static junit.framework.TestCase.assertNotNull;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class Jackson2ObjectMapperBuilderModuleOverrideTests {
private static final String DATA = "{\"offsetDateTime\": \"2020-01-01T00:00:00\"}";
@Test
public void testObjectMapperForZoneOffset() throws IOException {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
builder.modules(javaTimeModule);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
ObjectMapper objectMapper = builder.build();
DemoPojo demoPojo = objectMapper.readValue(DATA, DemoPojo.class);
assertNotNull(demoPojo.getOffsetDateTime());
}
@Test
public void testFailingObjectMapperForZoneOffset() throws IOException {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
builder.modulesToInstall(javaTimeModule);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
ObjectMapper objectMapper = builder.build();
DemoPojo demoPojo = objectMapper.readValue(DATA, DemoPojo.class);
assertNotNull(demoPojo.getOffsetDateTime());
}
}
class OffsetDateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
private static final String CURRENT_ZONE_OFFSET = OffsetDateTime.now().getOffset().toString();
@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
final String value = jsonParser.getValueAsString();
if (StringUtils.isEmpty(value)) {
return null;
}
try {
return OffsetDateTime.parse(value);
} catch (DateTimeParseException exception) {
return OffsetDateTime.parse(value + CURRENT_ZONE_OFFSET);
}
}
}
@JsonDeserialize
class DemoPojo {
private OffsetDateTime offsetDateTime;
public OffsetDateTime getOffsetDateTime() {
return offsetDateTime;
}
public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
this.offsetDateTime = offsetDateTime;
}
}
|
Thanks for the feedback. Would you raise an issue for the spring framework then? |
I've asked @philwebb to move this issue over to spring-projects/spring-framework. |
This sounds worth backporting to 5.0.13 and 4.3.23 as well. |
Spring Boot version: 2.1.2.RELEASE
The Jackson2ObjectMapperBuilder's modulesToInstall documentation says
But when we have this config:
It is expect that the custom DateTimeDeserializer will get picked up. But still the default one's are used and leading to json parse errors.
The text was updated successfully, but these errors were encountered: