Skip to content

Commit d796087

Browse files
committed
Polish "Add properties for configuring EnumFeature and JsonNodeFeature"
See gh-37885
1 parent 8edb4b9 commit d796087

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ public void customize(Jackson2ObjectMapperBuilder builder) {
213213
configureFeatures(builder, this.jacksonProperties.getMapper());
214214
configureFeatures(builder, this.jacksonProperties.getParser());
215215
configureFeatures(builder, this.jacksonProperties.getGenerator());
216-
configureFeatures(builder, this.jacksonProperties.getEnumDatatype());
217-
configureFeatures(builder, this.jacksonProperties.getJsonNodeDatatype());
216+
configureFeatures(builder, this.jacksonProperties.getDatatype().getEnum());
217+
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode());
218218
configureDateFormat(builder);
219219
configurePropertyNamingStrategy(builder);
220220
configureModules(builder);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonProperties.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,6 @@ public class JacksonProperties {
8989
*/
9090
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
9191

92-
/**
93-
* Jackson on/off features for enum types.
94-
*/
95-
private final Map<EnumFeature, Boolean> enumDatatype = new EnumMap<>(EnumFeature.class);
96-
97-
/**
98-
* Jackson on/off features for JsonNode types.
99-
*/
100-
private final Map<JsonNodeFeature, Boolean> jsonNodeDatatype = new EnumMap<>(JsonNodeFeature.class);
101-
10292
/**
10393
* Controls the inclusion of properties during serialization. Configured with one of
10494
* the values in Jackson's JsonInclude.Include enumeration.
@@ -127,6 +117,8 @@ public class JacksonProperties {
127117
*/
128118
private Locale locale;
129119

120+
private final Datatype datatype = new Datatype();
121+
130122
public String getDateFormat() {
131123
return this.dateFormat;
132124
}
@@ -167,14 +159,6 @@ public Map<JsonGenerator.Feature, Boolean> getGenerator() {
167159
return this.generator;
168160
}
169161

170-
public Map<EnumFeature, Boolean> getEnumDatatype() {
171-
return this.enumDatatype;
172-
}
173-
174-
public Map<JsonNodeFeature, Boolean> getJsonNodeDatatype() {
175-
return this.jsonNodeDatatype;
176-
}
177-
178162
public JsonInclude.Include getDefaultPropertyInclusion() {
179163
return this.defaultPropertyInclusion;
180164
}
@@ -215,6 +199,10 @@ public void setLocale(Locale locale) {
215199
this.locale = locale;
216200
}
217201

202+
public Datatype getDatatype() {
203+
return this.datatype;
204+
}
205+
218206
public enum ConstructorDetectorStrategy {
219207

220208
/**
@@ -240,4 +228,26 @@ public enum ConstructorDetectorStrategy {
240228

241229
}
242230

231+
public static class Datatype {
232+
233+
/**
234+
* Jackson on/off features for enums.
235+
*/
236+
private final Map<EnumFeature, Boolean> enumFeatures = new EnumMap<>(EnumFeature.class);
237+
238+
/**
239+
* Jackson on/off features for JsonNodes.
240+
*/
241+
private final Map<JsonNodeFeature, Boolean> jsonNode = new EnumMap<>(JsonNodeFeature.class);
242+
243+
public Map<EnumFeature, Boolean> getEnum() {
244+
return this.enumFeatures;
245+
}
246+
247+
public Map<JsonNodeFeature, Boolean> getJsonNode() {
248+
return this.jsonNode;
249+
}
250+
251+
}
252+
243253
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,10 @@
15591559
"name": "spring.jackson.constructor-detector",
15601560
"defaultValue": "default"
15611561
},
1562+
{
1563+
"name": "spring.jackson.datatype.enum",
1564+
"description": "Jackson on/off features for enums."
1565+
},
15621566
{
15631567
"name": "spring.jackson.joda-date-time-format",
15641568
"type": "java.lang.String",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ void defaultObjectMapperBuilder() {
294294

295295
@Test
296296
void enableEnumFeature() {
297-
this.contextRunner.withPropertyValues("spring.jackson.enum-data-type.write_enums_to_lowercase:true")
297+
this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.write-enums-to-lowercase=true")
298298
.run((context) -> {
299299
ObjectMapper mapper = context.getBean(ObjectMapper.class);
300300
assertThat(EnumFeature.WRITE_ENUMS_TO_LOWERCASE.enabledByDefault()).isFalse();
@@ -304,7 +304,7 @@ void enableEnumFeature() {
304304

305305
@Test
306306
void disableJsonNodeFeature() {
307-
this.contextRunner.withPropertyValues("spring.jackson.json-node-data-type.write_null_properties:false")
307+
this.contextRunner.withPropertyValues("spring.jackson.datatype.jsonnode.write-null-properties:false")
308308
.run((context) -> {
309309
ObjectMapper mapper = context.getBean(ObjectMapper.class);
310310
assertThat(JsonNodeFeature.WRITE_NULL_PROPERTIES.enabledByDefault()).isTrue();

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto/spring-mvc.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ Spring Boot also has some features to make it easier to customize this behavior.
6464

6565
You can configure the `ObjectMapper` and `XmlMapper` instances by using the environment.
6666
Jackson provides an extensive suite of on/off features that can be used to configure various aspects of its processing.
67-
These features are described in six enums (in Jackson) that map onto properties in the environment:
67+
These features are described in several enums (in Jackson) that map onto properties in the environment:
6868

6969
|===
7070
| Enum | Property | Values
7171

72+
| `com.fasterxml.jackson.databind.cfg.EnumFeature`
73+
| `spring.jackson.datatype.enum.<feature_name>`
74+
| `true`, `false`
75+
76+
| `com.fasterxml.jackson.databind.cfg.JsonNodeFeature`
77+
| `spring.jackson.datatype.jsonnode.<feature_name>`
78+
| `true`, `false`
79+
7280
| `com.fasterxml.jackson.databind.DeserializationFeature`
7381
| `spring.jackson.deserialization.<feature_name>`
7482
| `true`, `false`

0 commit comments

Comments
 (0)