Skip to content

Commit 19db6b2

Browse files
committed
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
2 parents 19fd88b + d796087 commit 19db6b2

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +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.getDatatype().getEnum());
217+
configureFeatures(builder, this.jacksonProperties.getDatatype().getJsonNode());
216218
configureDateFormat(builder);
217219
configurePropertyNamingStrategy(builder);
218220
configureModules(builder);

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@
2929
import com.fasterxml.jackson.databind.DeserializationFeature;
3030
import com.fasterxml.jackson.databind.MapperFeature;
3131
import com.fasterxml.jackson.databind.SerializationFeature;
32+
import com.fasterxml.jackson.databind.cfg.EnumFeature;
33+
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
3234

3335
import org.springframework.boot.context.properties.ConfigurationProperties;
3436

@@ -38,6 +40,7 @@
3840
* @author Andy Wilkinson
3941
* @author Marcel Overdijk
4042
* @author Johannes Edmeier
43+
* @author Eddú Meléndez
4144
* @since 1.2.0
4245
*/
4346
@ConfigurationProperties(prefix = "spring.jackson")
@@ -114,6 +117,8 @@ public class JacksonProperties {
114117
*/
115118
private Locale locale;
116119

120+
private final Datatype datatype = new Datatype();
121+
117122
public String getDateFormat() {
118123
return this.dateFormat;
119124
}
@@ -194,6 +199,10 @@ public void setLocale(Locale locale) {
194199
this.locale = locale;
195200
}
196201

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

199208
/**
@@ -219,4 +228,26 @@ public enum ConstructorDetectorStrategy {
219228

220229
}
221230

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+
222253
}

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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import com.fasterxml.jackson.databind.SerializerProvider;
4646
import com.fasterxml.jackson.databind.cfg.ConstructorDetector;
4747
import com.fasterxml.jackson.databind.cfg.ConstructorDetector.SingleArgConstructor;
48+
import com.fasterxml.jackson.databind.cfg.EnumFeature;
49+
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
4850
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
4951
import com.fasterxml.jackson.databind.module.SimpleModule;
5052
import com.fasterxml.jackson.databind.util.StdDateFormat;
@@ -88,6 +90,7 @@
8890
* @author Johannes Edmeier
8991
* @author Grzegorz Poznachowski
9092
* @author Ralf Ueberfuhr
93+
* @author Eddú Meléndez
9194
*/
9295
class JacksonAutoConfigurationTests {
9396

@@ -289,6 +292,27 @@ void defaultObjectMapperBuilder() {
289292
});
290293
}
291294

295+
@Test
296+
void enableEnumFeature() {
297+
this.contextRunner.withPropertyValues("spring.jackson.datatype.enum.write-enums-to-lowercase=true")
298+
.run((context) -> {
299+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
300+
assertThat(EnumFeature.WRITE_ENUMS_TO_LOWERCASE.enabledByDefault()).isFalse();
301+
assertThat(mapper.getSerializationConfig().isEnabled(EnumFeature.WRITE_ENUMS_TO_LOWERCASE)).isTrue();
302+
});
303+
}
304+
305+
@Test
306+
void disableJsonNodeFeature() {
307+
this.contextRunner.withPropertyValues("spring.jackson.datatype.jsonnode.write-null-properties:false")
308+
.run((context) -> {
309+
ObjectMapper mapper = context.getBean(ObjectMapper.class);
310+
assertThat(JsonNodeFeature.WRITE_NULL_PROPERTIES.enabledByDefault()).isTrue();
311+
assertThat(mapper.getDeserializationConfig().isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES))
312+
.isFalse();
313+
});
314+
}
315+
292316
@Test
293317
void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
294318
this.contextRunner.withUserConfiguration(ModuleConfig.class).run((context) -> {

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)