Skip to content

Commit fc1f65f

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

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
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/ReactiveElasticsearchTemplateTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.lang.Long;
3232
import java.lang.Object;
3333
import java.net.ConnectException;
34+
import java.time.LocalDate;
35+
import java.time.format.DateTimeFormatter;
3436
import java.util.Arrays;
3537
import java.util.Collections;
3638
import java.util.LinkedHashMap;
@@ -49,7 +51,6 @@
4951
import org.junit.jupiter.api.AfterEach;
5052
import org.junit.jupiter.api.BeforeEach;
5153
import org.junit.jupiter.api.Test;
52-
5354
import org.springframework.dao.DataAccessResourceFailureException;
5455
import org.springframework.dao.OptimisticLockingFailureException;
5556
import org.springframework.data.annotation.Id;
@@ -524,7 +525,8 @@ public void aggregateShouldReturnAggregations() {
524525

525526
@Test // DATAES-567, DATAES-767
526527
public void aggregateShouldErrorWhenIndexDoesNotExist() {
527-
template.aggregate(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class,
528+
template
529+
.aggregate(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class,
528530
IndexCoordinates.of("no-such-index")) //
529531
.as(StepVerifier::create) //
530532
.expectError(ElasticsearchStatusException.class);
@@ -981,6 +983,28 @@ public Person(String name, int age) {
981983

982984
// --> JUST some helpers
983985

986+
@Test // #1665
987+
void shouldBeAbleToProcessDateMathIndexNames() {
988+
989+
String indexName = "foo-" + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM"));
990+
String dateMathIndexName = "<foo-{now/M{yyyy.MM}}>";
991+
992+
SampleEntity entity = randomEntity("foo");
993+
994+
template.save(entity, IndexCoordinates.of(dateMathIndexName)) //
995+
.as(StepVerifier::create) //
996+
.expectNext(entity) //
997+
.verifyComplete(); //
998+
999+
template.get(entity.getId(), SampleEntity.class, IndexCoordinates.of(indexName)) //
1000+
.as(StepVerifier::create) //
1001+
.expectNext(entity) //
1002+
.verifyComplete(); //
1003+
1004+
}
1005+
// endregion
1006+
1007+
// region Helper functions
9841008
private SampleEntity randomEntity(String message) {
9851009

9861010
return SampleEntity.builder() //

0 commit comments

Comments
 (0)