Skip to content

Commit 2e07f9a

Browse files
committed
DefaultWebClient exposes full URI template as request attribute
Closes gh-30027
1 parent dd23b1d commit 2e07f9a

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -410,6 +410,11 @@ private URI createUri(UriComponents uric) {
410410
}
411411
return URI.create(uric.toString());
412412
}
413+
414+
@Override
415+
public String toUriString() {
416+
return this.uriComponentsBuilder.build().toUriString();
417+
}
413418
}
414419

415420
}

spring-web/src/main/java/org/springframework/web/util/UriBuilder.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -270,4 +270,13 @@ public interface UriBuilder {
270270
*/
271271
URI build(Map<String, ?> uriVariables);
272272

273+
/**
274+
* Return a String representation of the URI by concatenating all URI
275+
* component values into the fully formed URI String. Implementing classes
276+
* should perform simple String concatenation of current URI component
277+
* values to preserve URI template placeholders.
278+
* @since 6.1.2
279+
*/
280+
String toUriString();
281+
273282
}

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ public URI build(Map<String, ?> uriVariables) {
501501
* @see UriComponents#toUriString()
502502
*/
503503
public String toUriString() {
504-
return (this.uriVariables.isEmpty() ? build().encode().toUriString() :
504+
return (this.uriVariables.isEmpty() ?
505+
build().encode().toUriString() :
505506
buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString());
506507
}
507508

spring-web/src/test/java/org/springframework/web/util/DefaultUriBuilderFactoryTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -186,4 +186,12 @@ public void pathWithDuplicateSlashes() {
186186
assertThat(uri.toString()).isEqualTo("/foo/bar");
187187
}
188188

189+
@Test // gh-30027
190+
void uriTemplateString() {
191+
String baseUrl = "https://github.com/spring-projects/spring-boot/releases";
192+
String uriTemplate = "/tag/v{version}";
193+
String actual = new DefaultUriBuilderFactory(baseUrl).uriString(uriTemplate).toUriString();
194+
assertThat(actual).isEqualTo(baseUrl + uriTemplate);
195+
}
196+
189197
}

spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
226226

227227
@Override
228228
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
229-
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
230-
return uri(uriBuilderFactory.expand(uriTemplate, uriVariables));
229+
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
230+
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
231+
return uri(uriBuilder.build(uriVariables));
231232
}
232233

233234
@Override

spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientObservationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void recordsObservationForSuccessfulExchange() {
8282
ClientRequest clientRequest = verifyAndGetRequest();
8383

8484
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS")
85-
.hasLowCardinalityKeyValue("uri", "/resource/{id}");
85+
.hasLowCardinalityKeyValue("uri", "/base/resource/{id}");
8686
assertThat(clientRequest.headers()).containsEntry("foo", Collections.singletonList("bar"));
8787
}
8888

0 commit comments

Comments
 (0)