Harmonize Elasticsearch converter auto-configuration

This commit harmonizes our auto-configuration so that default converters
are configured the same way ElasticsearchConfigurationSupport does.

Closes gh-25087
pull/27078/head
Stephane Nicoll 4 years ago
parent 7286c33f53
commit d96fcf320c

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.data.elasticsearch; package org.springframework.boot.autoconfigure.data.elasticsearch;
import java.util.Collections;
import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
@ -31,6 +33,7 @@ import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
@ -43,6 +46,7 @@ import org.springframework.web.reactive.function.client.WebClient;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Scott Frederick * @author Scott Frederick
* @author Stephane Nicoll
*/ */
abstract class ElasticsearchDataConfiguration { abstract class ElasticsearchDataConfiguration {
@ -51,14 +55,26 @@ abstract class ElasticsearchDataConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
ElasticsearchConverter elasticsearchConverter(SimpleElasticsearchMappingContext mappingContext) { ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new MappingElasticsearchConverter(mappingContext); return new ElasticsearchCustomConversions(Collections.emptyList());
}
@Bean
@ConditionalOnMissingBean
SimpleElasticsearchMappingContext mappingContext(
ElasticsearchCustomConversions elasticsearchCustomConversions) {
SimpleElasticsearchMappingContext mappingContext = new SimpleElasticsearchMappingContext();
mappingContext.setSimpleTypeHolder(elasticsearchCustomConversions.getSimpleTypeHolder());
return mappingContext;
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
SimpleElasticsearchMappingContext mappingContext() { ElasticsearchConverter elasticsearchConverter(SimpleElasticsearchMappingContext mappingContext,
return new SimpleElasticsearchMappingContext(); ElasticsearchCustomConversions elasticsearchCustomConversions) {
MappingElasticsearchConverter converter = new MappingElasticsearchConverter(mappingContext);
converter.setConversions(elasticsearchCustomConversions);
return converter;
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,10 @@
package org.springframework.boot.autoconfigure.data.elasticsearch; package org.springframework.boot.autoconfigure.data.elasticsearch;
import java.math.BigDecimal;
import java.util.Collections;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -25,9 +29,13 @@ import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestCli
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;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter; import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -40,10 +48,11 @@ import static org.mockito.Mockito.mock;
* @author Brian Clozel * @author Brian Clozel
* @author Peter-Josef Meisch * @author Peter-Josef Meisch
* @author Scott Frederick * @author Scott Frederick
* @author Stephane Nicoll
*/ */
class ElasticsearchDataAutoConfigurationTests { class ElasticsearchDataAutoConfigurationTests {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(ElasticsearchRestClientAutoConfiguration.class,
ReactiveElasticsearchRestClientAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class)); ReactiveElasticsearchRestClientAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class));
@ -61,7 +70,26 @@ class ElasticsearchDataAutoConfigurationTests {
void defaultRestBeansRegistered() { void defaultRestBeansRegistered() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestTemplate.class) this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ElasticsearchRestTemplate.class)
.hasSingleBean(ReactiveElasticsearchTemplate.class).hasSingleBean(ElasticsearchConverter.class) .hasSingleBean(ReactiveElasticsearchTemplate.class).hasSingleBean(ElasticsearchConverter.class)
.hasSingleBean(ElasticsearchConverter.class)); .hasSingleBean(ElasticsearchConverter.class).hasSingleBean(ElasticsearchCustomConversions.class));
}
@Test
void defaultConversionsRegisterBigDecimalAsSimpleType() {
this.contextRunner.run((context) -> {
SimpleElasticsearchMappingContext mappingContext = context.getBean(SimpleElasticsearchMappingContext.class);
assertThat(mappingContext)
.extracting("simpleTypeHolder", InstanceOfAssertFactories.type(SimpleTypeHolder.class)).satisfies(
(simpleTypeHolder) -> assertThat(simpleTypeHolder.isSimpleType(BigDecimal.class)).isTrue());
});
}
@Test
void customConversionsShouldBeUsed() {
this.contextRunner.withUserConfiguration(CustomElasticsearchCustomConversions.class).run((context) -> {
assertThat(context).hasSingleBean(ElasticsearchCustomConversions.class).hasBean("testCustomConversions");
assertThat(context.getBean(ElasticsearchConverter.class).getConversionService()
.canConvert(ElasticsearchRestTemplate.class, Boolean.class)).isTrue();
});
} }
@Test @Test
@ -77,6 +105,16 @@ class ElasticsearchDataAutoConfigurationTests {
.contains("reactiveElasticsearchTemplate")); .contains("reactiveElasticsearchTemplate"));
} }
@Configuration(proxyBeanMethods = false)
static class CustomElasticsearchCustomConversions {
@Bean
ElasticsearchCustomConversions testCustomConversions() {
return new ElasticsearchCustomConversions(Collections.singletonList(new MyConverter()));
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class CustomRestTemplate { static class CustomRestTemplate {
@ -97,4 +135,13 @@ class ElasticsearchDataAutoConfigurationTests {
} }
static class MyConverter implements Converter<ElasticsearchRestTemplate, Boolean> {
@Override
public Boolean convert(ElasticsearchRestTemplate source) {
return null;
}
}
} }

Loading…
Cancel
Save