Skip to content

Commit 993fbb3

Browse files
nosansnicoll
authored andcommitted
Retain existing modules in JacksonAutoConfiguration
Previously, the default Jackson2ObjectMapperBuilderCustomizer implementation did set the list of modules to use. This had the effect of removing any modules that were registered programmatically by a customizer with higher precedence. This commit uses the variant of modulesToInstall that retain any existing modules. It also adds a note in the documentation as this behavior can be easily missed. See gh-42836
1 parent ff6c7c7 commit 993fbb3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ private Field findPropertyNamingStrategyField(String fieldName) {
307307
}
308308

309309
private void configureModules(Jackson2ObjectMapperBuilder builder) {
310-
builder.modulesToInstall(this.modules.toArray(new Module[0]));
310+
builder.modulesToInstall((modules) -> modules.addAll(this.modules));
311311
}
312312

313313
private void configureLocale(Jackson2ObjectMapperBuilder builder) {

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

+23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
import org.springframework.context.annotation.Configuration;
7474
import org.springframework.context.annotation.Import;
7575
import org.springframework.context.annotation.Primary;
76+
import org.springframework.core.Ordered;
77+
import org.springframework.core.annotation.Order;
7678
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
7779

7880
import static org.assertj.core.api.Assertions.assertThat;
@@ -324,6 +326,16 @@ void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
324326
});
325327
}
326328

329+
@Test
330+
void customModulesRegisteredByBuilderCustomizerWithHighestPrecedenceShouldBeRetained() {
331+
this.contextRunner.withUserConfiguration(ModuleConfig.class, CustomModuleBuilderCustomizerConfig.class)
332+
.run((context) -> {
333+
ObjectMapper objectMapper = context.getBean(Jackson2ObjectMapperBuilder.class).build();
334+
assertThat(context.getBean(CustomModule.class).getOwners()).contains(objectMapper);
335+
assertThat(objectMapper.getRegisteredModuleIds()).contains("customizer-module");
336+
});
337+
}
338+
327339
@Test
328340
void defaultSerializationInclusion() {
329341
this.contextRunner.run((context) -> {
@@ -592,6 +604,17 @@ Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
592604

593605
}
594606

607+
@Configuration(proxyBeanMethods = false)
608+
static class CustomModuleBuilderCustomizerConfig {
609+
610+
@Bean
611+
@Order(Ordered.HIGHEST_PRECEDENCE)
612+
Jackson2ObjectMapperBuilderCustomizer customModuleCustomizer() {
613+
return (builder) -> builder.modulesToInstall(new SimpleModule("customizer-module"));
614+
}
615+
616+
}
617+
595618
@Configuration(proxyBeanMethods = false)
596619
static class ObjectMapperBuilderConsumerConfig {
597620

0 commit comments

Comments
 (0)