Commit fb3deab8 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '2.0.x'

parents 971adfcd e81abc3f
...@@ -54,6 +54,7 @@ public class CodecsAutoConfiguration { ...@@ -54,6 +54,7 @@ public class CodecsAutoConfiguration {
static class JacksonCodecConfiguration { static class JacksonCodecConfiguration {
@Bean @Bean
@Order(0)
@ConditionalOnBean(ObjectMapper.class) @ConditionalOnBean(ObjectMapper.class)
public CodecCustomizer jacksonCodecCustomizer(ObjectMapper objectMapper) { public CodecCustomizer jacksonCodecCustomizer(ObjectMapper objectMapper) {
return (configurer) -> { return (configurer) -> {
......
...@@ -16,13 +16,17 @@ ...@@ -16,13 +16,17 @@
package org.springframework.boot.autoconfigure.http.codec; package org.springframework.boot.autoconfigure.http.codec;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.http.HttpProperties; import org.springframework.boot.autoconfigure.http.HttpProperties;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.codec.CodecCustomizer; import org.springframework.boot.web.codec.CodecCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.codec.CodecConfigurer; import org.springframework.http.codec.CodecConfigurer;
import org.springframework.http.codec.support.DefaultClientCodecConfigurer; import org.springframework.http.codec.support.DefaultClientCodecConfigurer;
...@@ -34,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -34,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link CodecsAutoConfiguration}. * Tests for {@link CodecsAutoConfiguration}.
* *
* @author Madhura Bhave * @author Madhura Bhave
* @author Andy Wilkinson
*/ */
public class CodecsAutoConfigurationTests { public class CodecsAutoConfigurationTests {
...@@ -76,6 +81,30 @@ public class CodecsAutoConfigurationTests { ...@@ -76,6 +81,30 @@ public class CodecsAutoConfigurationTests {
}); });
} }
@Test
public void jacksonCodecCustomizerBacksOffWhenThereIsNoObjectMapper() {
this.contextRunner.run((context) -> assertThat(context)
.doesNotHaveBean("jacksonCodecCustomizer"));
}
@Test
public void jacksonCodecCustomizerIsAutoConfiguredWhenObjectMapperIsPresent() {
this.contextRunner.withUserConfiguration(ObjectMapperConfiguration.class)
.run((context) -> assertThat(context).hasBean("jacksonCodecCustomizer"));
}
@Test
public void userProvidedCustomizerCanOverrideJacksonCodecCustomizer() {
this.contextRunner.withUserConfiguration(ObjectMapperConfiguration.class,
CodecCustomizerConfiguration.class).run((context) -> {
List<CodecCustomizer> codecCustomizers = context
.getBean(CodecCustomizers.class).codecCustomizers;
assertThat(codecCustomizers).hasSize(3);
assertThat(codecCustomizers.get(2))
.isInstanceOf(TestCodecCustomizer.class);
});
}
static class TestAnnotationAwareOrderComparator static class TestAnnotationAwareOrderComparator
extends AnnotationAwareOrderComparator { extends AnnotationAwareOrderComparator {
...@@ -86,4 +115,47 @@ public class CodecsAutoConfigurationTests { ...@@ -86,4 +115,47 @@ public class CodecsAutoConfigurationTests {
} }
@Configuration
static class ObjectMapperConfiguration {
@Bean
ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
@Configuration
static class CodecCustomizerConfiguration {
@Bean
CodecCustomizer codecCustomizer() {
return new TestCodecCustomizer();
}
@Bean
CodecCustomizers codecCustomizers(List<CodecCustomizer> customizers) {
return new CodecCustomizers(customizers);
}
}
private static final class TestCodecCustomizer implements CodecCustomizer {
@Override
public void customize(CodecConfigurer configurer) {
}
}
private static final class CodecCustomizers {
private final List<CodecCustomizer> codecCustomizers;
private CodecCustomizers(List<CodecCustomizer> codecCustomizers) {
this.codecCustomizers = codecCustomizers;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment