Skip to content

Commit b1a9243

Browse files
committed
JacksonAutoConfiguration should retain modules registered in Jackson2ObjectMapperBuilderCustomizer with higher precedence, rather than overwriting them
1 parent bacd2c2 commit b1a9243

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 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.
@@ -306,7 +306,7 @@ private Field findPropertyNamingStrategyField(String fieldName) {
306306
}
307307

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

312312
private void configureLocale(Jackson2ObjectMapperBuilder builder) {

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

+25
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,18 @@ void moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder() {
324326
});
325327
}
326328

329+
@Test
330+
void customModulesRegisteredByBuilderCustomizerWithHighestPrecedenceShouldBeRetained() {
331+
this.contextRunner.withUserConfiguration(ModuleConfig.class, ObjectMapperModuleBuilderCustomizerConfig.class)
332+
.run((context) -> {
333+
ObjectMapper objectMapper = context.getBean(Jackson2ObjectMapperBuilder.class).build();
334+
assertThat(objectMapper.getRegisteredModuleIds()).contains("customizer-module");
335+
assertThat(context.getBean(CustomModule.class).getOwners()).contains(objectMapper);
336+
assertThat(((DefaultSerializerProvider) objectMapper.getSerializerProviderInstance())
337+
.hasSerializerFor(Baz.class, null)).isTrue();
338+
});
339+
}
340+
327341
@Test
328342
void defaultSerializationInclusion() {
329343
this.contextRunner.run((context) -> {
@@ -592,6 +606,17 @@ Jackson2ObjectMapperBuilderCustomizer customDateFormat() {
592606

593607
}
594608

609+
@Configuration(proxyBeanMethods = false)
610+
static class ObjectMapperModuleBuilderCustomizerConfig {
611+
612+
@Bean
613+
@Order(Ordered.HIGHEST_PRECEDENCE)
614+
Jackson2ObjectMapperBuilderCustomizer customModuleCustomizer() {
615+
return (builder) -> builder.modulesToInstall(new SimpleModule("customizer-module"));
616+
}
617+
618+
}
619+
595620
@Configuration(proxyBeanMethods = false)
596621
static class ObjectMapperBuilderConsumerConfig {
597622

0 commit comments

Comments
 (0)