Skip to content

Commit

Permalink
Add support for EnumFeature and JsonNodeFeature
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eddumelendez committed Oct 14, 2023
1 parent 085e12a commit 2e301f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Expand Up @@ -213,6 +213,8 @@ public void customize(Jackson2ObjectMapperBuilder builder) {
configureFeatures(builder, this.jacksonProperties.getMapper());
configureFeatures(builder, this.jacksonProperties.getParser());
configureFeatures(builder, this.jacksonProperties.getGenerator());
configureFeatures(builder, this.jacksonProperties.getEnumDatatype());
configureFeatures(builder, this.jacksonProperties.getJsonNodeDatatype());
configureDateFormat(builder);
configurePropertyNamingStrategy(builder);
configureModules(builder);
Expand Down
@@ -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.
Expand Down Expand Up @@ -29,6 +29,8 @@
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;

Expand All @@ -38,6 +40,7 @@
* @author Andy Wilkinson
* @author Marcel Overdijk
* @author Johannes Edmeier
* @author Eddú Meléndez
* @since 1.2.0
*/
@ConfigurationProperties(prefix = "spring.jackson")
Expand Down Expand Up @@ -86,6 +89,16 @@ public class JacksonProperties {
*/
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
* the values in Jackson's JsonInclude.Include enumeration.
Expand Down Expand Up @@ -154,6 +167,14 @@ public Map<JsonGenerator.Feature, Boolean> getGenerator() {
return this.generator;
}

public Map<EnumFeature, Boolean> getEnumDatatype() {
return this.enumDatatype;
}

public Map<JsonNodeFeature, Boolean> getJsonNodeDatatype() {
return this.jsonNodeDatatype;
}

public JsonInclude.Include getDefaultPropertyInclusion() {
return this.defaultPropertyInclusion;
}
Expand Down
Expand Up @@ -45,6 +45,8 @@
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;
Expand Down Expand Up @@ -88,6 +90,7 @@
* @author Johannes Edmeier
* @author Grzegorz Poznachowski
* @author Ralf Ueberfuhr
* @author Eddú Meléndez
*/
class JacksonAutoConfigurationTests {

Expand Down Expand Up @@ -289,6 +292,24 @@ void defaultObjectMapperBuilder() {
});
}

@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
void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
this.contextRunner.withUserConfiguration(ModuleConfig.class).run((context) -> {
Expand Down

0 comments on commit 2e301f3

Please sign in to comment.