mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Add properties for configuring EnumFeature and JsonNodeFeature
Both `EnumFeature` and `JsonNodeFeature` implement `DataTypeFeature` which was recently added in Spring Framework. This commits introduces support to allow the configuration via properties. See spring-projects/spring-framework#31380 See gh-37885
This commit is contained in:
parent
19fd88b25b
commit
8edb4b9729
@ -213,6 +213,8 @@ public class JacksonAutoConfiguration {
|
|||||||
configureFeatures(builder, this.jacksonProperties.getMapper());
|
configureFeatures(builder, this.jacksonProperties.getMapper());
|
||||||
configureFeatures(builder, this.jacksonProperties.getParser());
|
configureFeatures(builder, this.jacksonProperties.getParser());
|
||||||
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
||||||
|
configureFeatures(builder, this.jacksonProperties.getEnumDatatype());
|
||||||
|
configureFeatures(builder, this.jacksonProperties.getJsonNodeDatatype());
|
||||||
configureDateFormat(builder);
|
configureDateFormat(builder);
|
||||||
configurePropertyNamingStrategy(builder);
|
configurePropertyNamingStrategy(builder);
|
||||||
configureModules(builder);
|
configureModules(builder);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -29,6 +29,8 @@ import com.fasterxml.jackson.core.JsonParser;
|
|||||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
import com.fasterxml.jackson.databind.MapperFeature;
|
import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.cfg.EnumFeature;
|
||||||
|
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
@ -38,6 +40,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Marcel Overdijk
|
* @author Marcel Overdijk
|
||||||
* @author Johannes Edmeier
|
* @author Johannes Edmeier
|
||||||
|
* @author Eddú Meléndez
|
||||||
* @since 1.2.0
|
* @since 1.2.0
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "spring.jackson")
|
@ConfigurationProperties(prefix = "spring.jackson")
|
||||||
@ -86,6 +89,16 @@ public class JacksonProperties {
|
|||||||
*/
|
*/
|
||||||
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
|
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson on/off features for enum types.
|
||||||
|
*/
|
||||||
|
private final Map<EnumFeature, Boolean> enumDatatype = new EnumMap<>(EnumFeature.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jackson on/off features for JsonNode types.
|
||||||
|
*/
|
||||||
|
private final Map<JsonNodeFeature, Boolean> jsonNodeDatatype = new EnumMap<>(JsonNodeFeature.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controls the inclusion of properties during serialization. Configured with one of
|
* Controls the inclusion of properties during serialization. Configured with one of
|
||||||
* the values in Jackson's JsonInclude.Include enumeration.
|
* the values in Jackson's JsonInclude.Include enumeration.
|
||||||
@ -154,6 +167,14 @@ public class JacksonProperties {
|
|||||||
return this.generator;
|
return this.generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<EnumFeature, Boolean> getEnumDatatype() {
|
||||||
|
return this.enumDatatype;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<JsonNodeFeature, Boolean> getJsonNodeDatatype() {
|
||||||
|
return this.jsonNodeDatatype;
|
||||||
|
}
|
||||||
|
|
||||||
public JsonInclude.Include getDefaultPropertyInclusion() {
|
public JsonInclude.Include getDefaultPropertyInclusion() {
|
||||||
return this.defaultPropertyInclusion;
|
return this.defaultPropertyInclusion;
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,8 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
|
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
|
||||||
import com.fasterxml.jackson.databind.cfg.ConstructorDetector.SingleArgConstructor;
|
import com.fasterxml.jackson.databind.cfg.ConstructorDetector.SingleArgConstructor;
|
||||||
|
import com.fasterxml.jackson.databind.cfg.EnumFeature;
|
||||||
|
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
|
||||||
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
|
||||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||||
@ -88,6 +90,7 @@ import static org.mockito.Mockito.mock;
|
|||||||
* @author Johannes Edmeier
|
* @author Johannes Edmeier
|
||||||
* @author Grzegorz Poznachowski
|
* @author Grzegorz Poznachowski
|
||||||
* @author Ralf Ueberfuhr
|
* @author Ralf Ueberfuhr
|
||||||
|
* @author Eddú Meléndez
|
||||||
*/
|
*/
|
||||||
class JacksonAutoConfigurationTests {
|
class JacksonAutoConfigurationTests {
|
||||||
|
|
||||||
@ -289,6 +292,27 @@ class JacksonAutoConfigurationTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void enableEnumFeature() {
|
||||||
|
this.contextRunner.withPropertyValues("spring.jackson.enum-data-type.write_enums_to_lowercase:true")
|
||||||
|
.run((context) -> {
|
||||||
|
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
||||||
|
assertThat(EnumFeature.WRITE_ENUMS_TO_LOWERCASE.enabledByDefault()).isFalse();
|
||||||
|
assertThat(mapper.getSerializationConfig().isEnabled(EnumFeature.WRITE_ENUMS_TO_LOWERCASE)).isTrue();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void disableJsonNodeFeature() {
|
||||||
|
this.contextRunner.withPropertyValues("spring.jackson.json-node-data-type.write_null_properties:false")
|
||||||
|
.run((context) -> {
|
||||||
|
ObjectMapper mapper = context.getBean(ObjectMapper.class);
|
||||||
|
assertThat(JsonNodeFeature.WRITE_NULL_PROPERTIES.enabledByDefault()).isTrue();
|
||||||
|
assertThat(mapper.getDeserializationConfig().isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES))
|
||||||
|
.isFalse();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
|
void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
|
||||||
this.contextRunner.withUserConfiguration(ModuleConfig.class).run((context) -> {
|
this.contextRunner.withUserConfiguration(ModuleConfig.class).run((context) -> {
|
||||||
|
Loading…
Reference in New Issue
Block a user