Skip to content

Commit e81abc3

Browse files
committed
Order jacksonCodecCustomizer so user-provided customizer can go after it
Closes gh-15167
1 parent e6dd112 commit e81abc3

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/http/codec/CodecsAutoConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -26,6 +26,7 @@
2626
import org.springframework.boot.web.codec.CodecCustomizer;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.core.annotation.Order;
2930
import org.springframework.http.codec.CodecConfigurer;
3031
import org.springframework.http.codec.json.Jackson2JsonDecoder;
3132
import org.springframework.http.codec.json.Jackson2JsonEncoder;
@@ -51,6 +52,7 @@ public class CodecsAutoConfiguration {
5152
static class JacksonCodecConfiguration {
5253

5354
@Bean
55+
@Order(0)
5456
@ConditionalOnBean(ObjectMapper.class)
5557
public CodecCustomizer jacksonCodecCustomizer(ObjectMapper objectMapper) {
5658
return (configurer) -> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.http.codec;
18+
19+
import java.util.List;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.boot.web.codec.CodecCustomizer;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.http.codec.CodecConfigurer;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link CodecsAutoConfiguration}.
35+
*
36+
* @author Andy Wilkinson
37+
*/
38+
public class CodecsAutoConfigurationTests {
39+
40+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
41+
.withConfiguration(AutoConfigurations.of(CodecsAutoConfiguration.class));
42+
43+
@Test
44+
public void jacksonCodecCustomizerBacksOffWhenThereIsNoObjectMapper() {
45+
this.contextRunner.run(
46+
(context) -> assertThat(context).doesNotHaveBean(CodecCustomizer.class));
47+
}
48+
49+
@Test
50+
public void jacksonCodecCustomizerIsAutoConfiguredWhenObjectMapperIsPresent() {
51+
this.contextRunner.withUserConfiguration(ObjectMapperConfiguration.class).run(
52+
(context) -> assertThat(context).hasSingleBean(CodecCustomizer.class));
53+
}
54+
55+
@Test
56+
public void userProvidedCustomizerCanOverrideJacksonCodecCustomizer() {
57+
this.contextRunner.withUserConfiguration(ObjectMapperConfiguration.class,
58+
CodecCustomizerConfiguration.class).run((context) -> {
59+
List<CodecCustomizer> codecCustomizers = context
60+
.getBean(CodecCustomizers.class).codecCustomizers;
61+
assertThat(codecCustomizers).hasSize(2);
62+
assertThat(codecCustomizers.get(1))
63+
.isInstanceOf(TestCodecCustomizer.class);
64+
});
65+
}
66+
67+
@Configuration
68+
static class ObjectMapperConfiguration {
69+
70+
@Bean
71+
ObjectMapper objectMapper() {
72+
return new ObjectMapper();
73+
}
74+
75+
}
76+
77+
@Configuration
78+
static class CodecCustomizerConfiguration {
79+
80+
@Bean
81+
CodecCustomizer codecCustomizer() {
82+
return new TestCodecCustomizer();
83+
}
84+
85+
@Bean
86+
CodecCustomizers codecCustomizers(List<CodecCustomizer> customizers) {
87+
return new CodecCustomizers(customizers);
88+
}
89+
90+
}
91+
92+
private static final class TestCodecCustomizer implements CodecCustomizer {
93+
94+
@Override
95+
public void customize(CodecConfigurer configurer) {
96+
}
97+
98+
}
99+
100+
private static final class CodecCustomizers {
101+
102+
private final List<CodecCustomizer> codecCustomizers;
103+
104+
private CodecCustomizers(List<CodecCustomizer> codecCustomizers) {
105+
this.codecCustomizers = codecCustomizers;
106+
}
107+
108+
}
109+
110+
}

0 commit comments

Comments
 (0)