Skip to content

Commit

Permalink
DefaultWebClient exposes full URI template as request attribute
Browse files Browse the repository at this point in the history
Closes gh-30027
  • Loading branch information
rstoyanchev committed Dec 7, 2023
1 parent dd23b1d commit 2e07f9a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -410,6 +410,11 @@ private URI createUri(UriComponents uric) {
}
return URI.create(uric.toString());
}

@Override
public String toUriString() {
return this.uriComponentsBuilder.build().toUriString();
}
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ public URI build(Map<String, ?> uriVariables) {
* @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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -186,4 +186,12 @@ public void pathWithDuplicateSlashes() {
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void recordsObservationForSuccessfulExchange() {
ClientRequest clientRequest = verifyAndGetRequest();

assertThatHttpObservation().hasLowCardinalityKeyValue("outcome", "SUCCESS")
.hasLowCardinalityKeyValue("uri", "/resource/{id}");
.hasLowCardinalityKeyValue("uri", "/base/resource/{id}");
assertThat(clientRequest.headers()).containsEntry("foo", Collections.singletonList("bar"));
}

Expand Down

0 comments on commit 2e07f9a

Please sign in to comment.