Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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,27 @@ 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