Skip to content

Commit

Permalink
Expose RestClientBuilder when RestHighLevelClient is not available
Browse files Browse the repository at this point in the history
This commits exposes the RestClientBuilder as a bean even when the
RestHighLevelClient is not available. It allows users to create their
own RestClient beans using the Spring Boot configured RestClientBuilder
when they are not using the RestHighLevelClient.

Fixes gh-28655
  • Loading branch information
filiphr authored and bclozel committed Nov 15, 2021
1 parent 6e06e69 commit d6bead1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
Expand Up @@ -16,12 +16,10 @@

package org.springframework.boot.autoconfigure.elasticsearch;

import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClientBuilder;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientBuilderConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestClientSnifferConfiguration;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations.RestHighLevelClientConfiguration;
Expand All @@ -38,8 +36,7 @@
*/
@SuppressWarnings("deprecation")
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RestHighLevelClient.class)
@ConditionalOnMissingBean(RestClient.class)
@ConditionalOnClass(RestClientBuilder.class)
@EnableConfigurationProperties({ ElasticsearchProperties.class, ElasticsearchRestClientProperties.class,
DeprecatedElasticsearchRestClientProperties.class })
@Import({ RestClientBuilderConfiguration.class, RestHighLevelClientConfiguration.class,
Expand Down
Expand Up @@ -112,7 +112,8 @@ private HttpHost createHttpHost(URI uri) {
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(RestHighLevelClient.class)
@ConditionalOnClass(RestHighLevelClient.class)
@ConditionalOnMissingBean({ RestHighLevelClient.class, RestClient.class })
static class RestHighLevelClientConfiguration {

@Bean
Expand Down
Expand Up @@ -55,6 +55,7 @@
* @author Brian Clozel
* @author Vedran Pavic
* @author Evgeniy Cheban
* @author Filip Hrisafov
*/
class ElasticsearchRestClientAutoConfigurationTests {

Expand All @@ -64,14 +65,22 @@ class ElasticsearchRestClientAutoConfigurationTests {
@Test
void configureShouldOnlyCreateHighLevelRestClient() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(RestClient.class)
.hasSingleBean(RestHighLevelClient.class));
.hasSingleBean(RestClientBuilder.class).hasSingleBean(RestHighLevelClient.class));
}

@Test
void configureWithoutRestHighLevelClientShouldOnlyCreateRestClientBuilder() {
this.contextRunner.withClassLoader(new FilteredClassLoader(RestHighLevelClient.class))
.run((context) -> assertThat(context).doesNotHaveBean(RestClient.class)
.doesNotHaveBean(RestHighLevelClient.class).hasSingleBean(RestClientBuilder.class));
}

@Test
void configureWhenCustomRestClientShouldBackOff() {
this.contextRunner.withBean("customRestClient", RestClient.class, () -> mock(RestClient.class))
this.contextRunner.withUserConfiguration(CustomRestClientConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(RestHighLevelClient.class)
.hasSingleBean(RestClient.class).hasBean("customRestClient"));
.hasSingleBean(RestClientBuilder.class).hasSingleBean(RestClient.class)
.hasBean("customRestClient"));
}

@Test
Expand Down Expand Up @@ -304,6 +313,16 @@ RestHighLevelClient customRestHighLevelClient1(RestClientBuilder builder) {

}

@Configuration(proxyBeanMethods = false)
static class CustomRestClientConfiguration {

@Bean
RestClient customRestClient(RestClientBuilder builder) {
return builder.build();
}

}

@ParameterizedTest
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
Expand Down

0 comments on commit d6bead1

Please sign in to comment.