DefaultWebClient exposes full URI template as request attribute

Closes gh-30027
This commit is contained in:
Rossen Stoyanchev 2023-12-06 10:16:16 +00:00 committed by rstoyanchev
parent dd23b1d156
commit 2e07f9ab33
6 changed files with 31 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -410,6 +410,11 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
}
return URI.create(uric.toString());
}
@Override
public String toUriString() {
return this.uriComponentsBuilder.build().toUriString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -270,4 +270,13 @@ public interface UriBuilder {
*/
URI build(Map<String, ?> uriVariables);
/**
* Return a String representation of the URI by concatenating all URI
* component values into the fully formed URI String. Implementing classes
* should perform simple String concatenation of current URI component
* values to preserve URI template placeholders.
* @since 6.1.2
*/
String toUriString();
}

View File

@ -501,7 +501,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
* @see UriComponents#toUriString()
*/
public String toUriString() {
return (this.uriVariables.isEmpty() ? build().encode().toUriString() :
return (this.uriVariables.isEmpty() ?
build().encode().toUriString() :
buildInternal(EncodingHint.ENCODE_TEMPLATE).toUriString());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -186,4 +186,12 @@ public class DefaultUriBuilderFactoryTests {
assertThat(uri.toString()).isEqualTo("/foo/bar");
}
@Test // gh-30027
void uriTemplateString() {
String baseUrl = "https://github.com/spring-projects/spring-boot/releases";
String uriTemplate = "/tag/v{version}";
String actual = new DefaultUriBuilderFactory(baseUrl).uriString(uriTemplate).toUriString();
assertThat(actual).isEqualTo(baseUrl + uriTemplate);
}
}

View File

@ -226,8 +226,9 @@ final class DefaultWebClient implements WebClient {
@Override
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
return uri(uriBuilderFactory.expand(uriTemplate, uriVariables));
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(uriBuilder.build(uriVariables));
}
@Override

View File

@ -82,7 +82,7 @@ class WebClientObservationTests {
ClientRequest clientRequest = verifyAndGetRequest();
assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS")
.hasLowCardinalityKeyValue("uri", "/resource/{id}");
.hasLowCardinalityKeyValue("uri", "/base/resource/{id}");
assertThat(clientRequest.headers()).containsEntry("foo", Collections.singletonList("bar"));
}