Merge pull request #37885 from eddumelendez
* gh-37885: Polish "Add properties for configuring EnumFeature and JsonNodeFeature" Add properties for configuring EnumFeature and JsonNodeFeature Closes gh-37885
This commit is contained in:
@@ -213,6 +213,8 @@ public class JacksonAutoConfiguration {
|
||||
configureFeatures(builder, this.jacksonProperties.getMapper());
|
||||
configureFeatures(builder, this.jacksonProperties.getParser());
|
||||
configureFeatures(builder, this.jacksonProperties.getGenerator());
|
||||
configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum());
|
||||
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode());
|
||||
configureDateFormat(builder);
|
||||
configurePropertyNamingStrategy(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");
|
||||
* 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.MapperFeature;
|
||||
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;
|
||||
|
||||
@@ -38,6 +40,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
* @author Andy Wilkinson
|
||||
* @author Marcel Overdijk
|
||||
* @author Johannes Edmeier
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.jackson")
|
||||
@@ -114,6 +117,8 @@ public class JacksonProperties {
|
||||
*/
|
||||
private Locale locale;
|
||||
|
||||
private final Datatype datatype = new Datatype();
|
||||
|
||||
public String getDateFormat() {
|
||||
return this.dateFormat;
|
||||
}
|
||||
@@ -194,6 +199,10 @@ public class JacksonProperties {
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public Datatype getDatatype() {
|
||||
return this.datatype;
|
||||
}
|
||||
|
||||
public enum ConstructorDetectorStrategy {
|
||||
|
||||
/**
|
||||
@@ -219,4 +228,26 @@ public class JacksonProperties {
|
||||
|
||||
}
|
||||
|
||||
public static class Datatype {
|
||||
|
||||
/**
|
||||
* Jackson on/off features for enums.
|
||||
*/
|
||||
private final Map<EnumFeature, Boolean> enumFeatures = new EnumMap<>(EnumFeature.class);
|
||||
|
||||
/**
|
||||
* Jackson on/off features for JsonNodes.
|
||||
*/
|
||||
private final Map<JsonNodeFeature, Boolean> jsonNode = new EnumMap<>(JsonNodeFeature.class);
|
||||
|
||||
public Map<EnumFeature, Boolean> getEnum() {
|
||||
return this.enumFeatures;
|
||||
}
|
||||
|
||||
public Map<JsonNodeFeature, Boolean> getJsonNode() {
|
||||
return this.jsonNode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1559,6 +1559,10 @@
|
||||
"name": "spring.jackson.constructor-detector",
|
||||
"defaultValue": "default"
|
||||
},
|
||||
{
|
||||
"name": "spring.jackson.datatype.enum",
|
||||
"description": "Jackson on/off features for enums."
|
||||
},
|
||||
{
|
||||
"name": "spring.jackson.joda-date-time-format",
|
||||
"type": "java.lang.String",
|
||||
|
||||
@@ -45,6 +45,8 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
|
||||
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.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||
@@ -88,6 +90,7 @@ import static org.mockito.Mockito.mock;
|
||||
* @author Johannes Edmeier
|
||||
* @author Grzegorz Poznachowski
|
||||
* @author Ralf Ueberfuhr
|
||||
* @author Eddú Meléndez
|
||||
*/
|
||||
class JacksonAutoConfigurationTests {
|
||||
|
||||
@@ -289,6 +292,27 @@ class JacksonAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableEnumFeature() {
|
||||
this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.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.datatype.jsonnode.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
|
||||
void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
|
||||
this.contextRunner.withUserConfiguration(ModuleConfig.class).run((context) -> {
|
||||
|
||||
@@ -64,11 +64,19 @@ Spring Boot also has some features to make it easier to customize this behavior.
|
||||
|
||||
You can configure the `ObjectMapper` and `XmlMapper` instances by using the environment.
|
||||
Jackson provides an extensive suite of on/off features that can be used to configure various aspects of its processing.
|
||||
These features are described in six enums (in Jackson) that map onto properties in the environment:
|
||||
These features are described in several enums (in Jackson) that map onto properties in the environment:
|
||||
|
||||
|===
|
||||
| Enum | Property | Values
|
||||
|
||||
| `com.fasterxml.jackson.databind.cfg.EnumFeature`
|
||||
| `spring.jackson.datatype.enum.<feature_name>`
|
||||
| `true`, `false`
|
||||
|
||||
| `com.fasterxml.jackson.databind.cfg.JsonNodeFeature`
|
||||
| `spring.jackson.datatype.jsonnode.<feature_name>`
|
||||
| `true`, `false`
|
||||
|
||||
| `com.fasterxml.jackson.databind.DeserializationFeature`
|
||||
| `spring.jackson.deserialization.<feature_name>`
|
||||
| `true`, `false`
|
||||
|
||||
Reference in New Issue
Block a user