Skip to content

Commit 8edb4b9

Browse files
eddumelendezwilkinsona
authored andcommitted
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
1 parent 19fd88b commit 8edb4b9

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
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.getEnumDatatype());
217+
configureFeatures(builder, this.jacksonProperties.getJsonNodeDatatype());
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: 22 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")
@@ -86,6 +89,16 @@ public class JacksonProperties {
8689
*/
8790
private final Map<JsonGenerator.Feature, Boolean> generator = new EnumMap<>(JsonGenerator.Feature.class);
8891

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+
89102
/**
90103
* Controls the inclusion of properties during serialization. Configured with one of
91104
* the values in Jackson's JsonInclude.Include enumeration.
@@ -154,6 +167,14 @@ public Map<JsonGenerator.Feature, Boolean> getGenerator() {
154167
return this.generator;
155168
}
156169

170+
public Map<EnumFeature, Boolean> getEnumDatatype() {
171+
return this.enumDatatype;
172+
}
173+
174+
public Map<JsonNodeFeature, Boolean> getJsonNodeDatatype() {
175+
return this.jsonNodeDatatype;
176+
}
177+
157178
public JsonInclude.Include getDefaultPropertyInclusion() {
158179
return this.defaultPropertyInclusion;
159180
}

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.enum-data-type.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.json-node-data-type.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) -> {

0 commit comments

Comments
 (0)