Skip to content

Commit 96eac3b

Browse files
committed
ReactiveElasticsearchOperations indexName is encoded twice.
Original Pull Request #1666 Closes #1665 (cherry picked from commit 4829b07)
1 parent c47d284 commit 96eac3b

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/main/java/org/springframework/data/elasticsearch/client/reactive/DefaultWebClientProvider.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 the original author or authors.
2+
* Copyright 2018-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.util.Assert;
2828
import org.springframework.web.reactive.function.client.WebClient;
2929
import org.springframework.web.reactive.function.client.WebClient.Builder;
30+
import org.springframework.web.util.DefaultUriBuilderFactory;
3031

3132
/**
3233
* Default {@link WebClientProvider} that uses cached {@link WebClient} instances per {@code hostAndPort}.
@@ -156,7 +157,16 @@ protected WebClient createWebClientForSocketAddress(InetSocketAddress socketAddr
156157

157158
String baseUrl = String.format("%s://%s:%d%s", this.scheme, socketAddress.getHostString(), socketAddress.getPort(),
158159
pathPrefix == null ? "" : '/' + pathPrefix);
159-
WebClient webClient = builder.baseUrl(baseUrl).filter((request, next) -> next.exchange(request).doOnError(errorListener)).build();
160+
161+
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);
162+
// the template will already be encoded by the RequestConverters methods
163+
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
164+
builder.uriBuilderFactory(uriBuilderFactory); //
165+
166+
WebClient webClient = builder //
167+
.filter((request, next) -> next.exchange(request) //
168+
.doOnError(errorListener)) //
169+
.build(); //
160170
return webClientConfigurer.apply(webClient);
161171
}
162172
}

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateIntegrationTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.lang.Long;
3333
import java.lang.Object;
3434
import java.net.ConnectException;
35+
import java.time.LocalDate;
36+
import java.time.format.DateTimeFormatter;
3537
import java.util.ArrayList;
3638
import java.util.Arrays;
3739
import java.util.Collections;
@@ -1037,6 +1039,32 @@ void shouldReturnMonoOfSearchPage() {
10371039
assertThat(searchHits.getSearchHits().size()).isEqualTo(5);
10381040
}).verifyComplete();
10391041
}
1042+
1043+
@Test // #1665
1044+
@DisplayName("should be able to process date-math-index names")
1045+
void shouldBeAbleToProcessDateMathIndexNames() {
1046+
1047+
String indexName = "foo-" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM"));
1048+
String dateMathIndexName = "<foo-{now/M{yyyy.MM}}>";
1049+
1050+
template.indexOps(IndexCoordinates.of(dateMathIndexName)) //
1051+
.create() //
1052+
.as(StepVerifier::create) //
1053+
.expectNext(true) //
1054+
.verifyComplete(); //
1055+
1056+
template.indexOps(IndexCoordinates.of(indexName)) //
1057+
.exists() //
1058+
.as(StepVerifier::create) //
1059+
.expectNext(true) //
1060+
.verifyComplete(); //
1061+
1062+
template.indexOps(IndexCoordinates.of(dateMathIndexName)) //
1063+
.delete() //
1064+
.as(StepVerifier::create) //
1065+
.expectNext(true) //
1066+
.verifyComplete(); //
1067+
}
10401068
// endregion
10411069

10421070
// region Helper functions
@@ -1134,5 +1162,6 @@ static class VersionedEntity {
11341162
@Id private String id;
11351163
@Version private Long version;
11361164
}
1165+
11371166
// endregion
11381167
}

0 commit comments

Comments
 (0)