From 8697d9ddd5179bce06f9f36e71e2e0b2b5fb8655 Mon Sep 17 00:00:00 2001 From: Pengfei-Lu <459360594@qq.com> Date: Fri, 30 Jun 2023 09:11:05 +0800 Subject: [PATCH] Correct conditions on auto-configured JacksonJsonpMapper See gh-36109 Signed-off-by: Pengfei-Lu <459360594@qq.com> --- .../ElasticsearchClientAutoConfiguration.java | 4 +--- .../elasticsearch/ElasticsearchClientConfigurations.java | 3 ++- .../ElasticsearchClientAutoConfigurationTests.java | 7 +++++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfiguration.java index 66f17d4c5a..64b86ab0e6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfiguration.java @@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations.ElasticsearchClientConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations.ElasticsearchTransportConfiguration; -import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; import org.springframework.context.annotation.Import; @@ -33,8 +32,7 @@ import org.springframework.context.annotation.Import; * @author Andy Wilkinson * @since 3.0.0 */ -@AutoConfiguration(after = { JacksonAutoConfiguration.class, JsonbAutoConfiguration.class, - ElasticsearchRestClientAutoConfiguration.class }) +@AutoConfiguration(after = { JsonbAutoConfiguration.class, ElasticsearchRestClientAutoConfiguration.class }) @ConditionalOnClass(ElasticsearchClient.class) @Import({ ElasticsearchTransportConfiguration.class, ElasticsearchClientConfiguration.class }) public class ElasticsearchClientAutoConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientConfigurations.java index 008ea9dd27..63ec135c01 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientConfigurations.java @@ -31,6 +31,7 @@ import org.elasticsearch.client.RestClient; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -44,7 +45,7 @@ import org.springframework.context.annotation.Import; class ElasticsearchClientConfigurations { @ConditionalOnMissingBean(JsonpMapper.class) - @ConditionalOnBean(ObjectMapper.class) + @ConditionalOnClass(ObjectMapper.class) @Configuration(proxyBeanMethods = false) static class JacksonJsonpMapperConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfigurationTests.java index c290db791f..3dbaaa8010 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchClientAutoConfigurationTests.java @@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; +import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -64,14 +65,16 @@ class ElasticsearchClientAutoConfigurationTests { @Test void withoutJsonbOrJacksonShouldDefineSimpleMapper() { - this.contextRunner.withUserConfiguration(RestClientConfiguration.class) + this.contextRunner.withClassLoader(new FilteredClassLoader(ObjectMapper.class)) + .withUserConfiguration(RestClientConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class) .hasSingleBean(SimpleJsonpMapper.class)); } @Test void withJsonbShouldDefineJsonbMapper() { - this.contextRunner.withConfiguration(AutoConfigurations.of(JsonbAutoConfiguration.class)) + this.contextRunner.withClassLoader(new FilteredClassLoader(ObjectMapper.class)) + .withConfiguration(AutoConfigurations.of(JsonbAutoConfiguration.class)) .withUserConfiguration(RestClientConfiguration.class) .run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class) .hasSingleBean(JsonbJsonpMapper.class));