Merge pull request #36109 from Pengfei-Lu

* gh-36109:
  Correct conditions on auto-configured JacksonJsonpMapper

Closes gh-36109
pull/37018/head
Andy Wilkinson 1 year ago
commit 7eddacf406

@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations.ElasticsearchClientConfiguration; import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations.ElasticsearchClientConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchClientConfigurations.ElasticsearchTransportConfiguration; 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.boot.autoconfigure.jsonb.JsonbAutoConfiguration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -33,8 +32,7 @@ import org.springframework.context.annotation.Import;
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 3.0.0 * @since 3.0.0
*/ */
@AutoConfiguration(after = { JacksonAutoConfiguration.class, JsonbAutoConfiguration.class, @AutoConfiguration(after = { JsonbAutoConfiguration.class, ElasticsearchRestClientAutoConfiguration.class })
ElasticsearchRestClientAutoConfiguration.class })
@ConditionalOnClass(ElasticsearchClient.class) @ConditionalOnClass(ElasticsearchClient.class)
@Import({ ElasticsearchTransportConfiguration.class, ElasticsearchClientConfiguration.class }) @Import({ ElasticsearchTransportConfiguration.class, ElasticsearchClientConfiguration.class })
public class ElasticsearchClientAutoConfiguration { public class ElasticsearchClientAutoConfiguration {

@ -31,6 +31,7 @@ import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -44,7 +45,7 @@ import org.springframework.context.annotation.Import;
class ElasticsearchClientConfigurations { class ElasticsearchClientConfigurations {
@ConditionalOnMissingBean(JsonpMapper.class) @ConditionalOnMissingBean(JsonpMapper.class)
@ConditionalOnBean(ObjectMapper.class) @ConditionalOnClass(ObjectMapper.class)
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class JacksonJsonpMapperConfiguration { static class JacksonJsonpMapperConfiguration {

@ -30,6 +30,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration; 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.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -64,14 +65,16 @@ class ElasticsearchClientAutoConfigurationTests {
@Test @Test
void withoutJsonbOrJacksonShouldDefineSimpleMapper() { void withoutJsonbOrJacksonShouldDefineSimpleMapper() {
this.contextRunner.withUserConfiguration(RestClientConfiguration.class) this.contextRunner.withClassLoader(new FilteredClassLoader(ObjectMapper.class))
.withUserConfiguration(RestClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class) .run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class)
.hasSingleBean(SimpleJsonpMapper.class)); .hasSingleBean(SimpleJsonpMapper.class));
} }
@Test @Test
void withJsonbShouldDefineJsonbMapper() { void withJsonbShouldDefineJsonbMapper() {
this.contextRunner.withConfiguration(AutoConfigurations.of(JsonbAutoConfiguration.class)) this.contextRunner.withClassLoader(new FilteredClassLoader(ObjectMapper.class))
.withConfiguration(AutoConfigurations.of(JsonbAutoConfiguration.class))
.withUserConfiguration(RestClientConfiguration.class) .withUserConfiguration(RestClientConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class) .run((context) -> assertThat(context).hasSingleBean(JsonpMapper.class)
.hasSingleBean(JsonbJsonpMapper.class)); .hasSingleBean(JsonbJsonpMapper.class));

Loading…
Cancel
Save