Skip to content

Commit 5f05340

Browse files
bramhaagsdeleuze
authored andcommitted
Support Jackson's DatatypeFeature in Jackson2ObjectMapperBuilder
Closes gh-31380
1 parent 63770fb commit 5f05340

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.fasterxml.jackson.databind.ObjectMapper;
4646
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
4747
import com.fasterxml.jackson.databind.SerializationFeature;
48+
import com.fasterxml.jackson.databind.cfg.DatatypeFeature;
4849
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
4950
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
5051
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
@@ -839,6 +840,9 @@ else if (feature instanceof SerializationFeature serializationFeature) {
839840
else if (feature instanceof DeserializationFeature deserializationFeature) {
840841
objectMapper.configure(deserializationFeature, enabled);
841842
}
843+
else if (feature instanceof DatatypeFeature datatypeFeature) {
844+
objectMapper.configure(datatypeFeature, enabled);
845+
}
842846
else if (feature instanceof MapperFeature mapperFeature) {
843847
objectMapper.configure(mapperFeature, enabled);
844848
}

spring-web/src/test/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilderTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import com.fasterxml.jackson.databind.SerializerProvider;
5858
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5959
import com.fasterxml.jackson.databind.cfg.DeserializerFactoryConfig;
60+
import com.fasterxml.jackson.databind.cfg.EnumFeature;
61+
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
6062
import com.fasterxml.jackson.databind.cfg.SerializerFactoryConfig;
6163
import com.fasterxml.jackson.databind.deser.BasicDeserializerFactory;
6264
import com.fasterxml.jackson.databind.deser.Deserializers;
@@ -455,10 +457,12 @@ void completeSetup() throws JsonMappingException {
455457
.annotationIntrospector(current -> AnnotationIntrospector.pair(current, introspector))
456458
.featuresToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
457459
DeserializationFeature.UNWRAP_ROOT_VALUE,
460+
EnumFeature.BOGUS_FEATURE,
458461
JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
459462
JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)
460463
.featuresToDisable(MapperFeature.AUTO_DETECT_GETTERS,
461464
MapperFeature.AUTO_DETECT_FIELDS,
465+
JsonNodeFeature.READ_NULL_PROPERTIES,
462466
JsonParser.Feature.AUTO_CLOSE_SOURCE,
463467
JsonGenerator.Feature.QUOTE_FIELD_NAMES)
464468
.serializationInclusion(JsonInclude.Include.NON_NULL);
@@ -487,13 +491,15 @@ void completeSetup() throws JsonMappingException {
487491

488492
assertThat(mapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)).isTrue();
489493
assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)).isTrue();
494+
assertThat(mapper.getSerializationConfig().isEnabled(EnumFeature.BOGUS_FEATURE)).isTrue();
490495
assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)).isTrue();
491496
assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)).isTrue();
492497

493498
assertThat(mapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)).isFalse();
494499
assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)).isFalse();
495500
assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)).isFalse();
496501
assertThat(mapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)).isFalse();
502+
assertThat(mapper.getDeserializationConfig().isEnabled(JsonNodeFeature.READ_NULL_PROPERTIES)).isFalse();
497503
assertThat(mapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)).isFalse();
498504
assertThat(mapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES)).isFalse();
499505
assertThat(mapper.getSerializationConfig().getSerializationInclusion()).isSameAs(JsonInclude.Include.NON_NULL);

0 commit comments

Comments
 (0)