Skip to content

RestClient does not expose full URI template as attribute #33928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,19 +313,22 @@ public DefaultRequestBodyUriSpec(HttpMethod httpMethod) {

@Override
public RequestBodySpec uri(String uriTemplate, Object... uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(DefaultRestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}

@Override
public RequestBodySpec uri(String uriTemplate, Map<String, ?> uriVariables) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(DefaultRestClient.this.uriBuilderFactory.expand(uriTemplate, uriVariables));
}

@Override
public RequestBodySpec uri(String uriTemplate, Function<UriBuilder, URI> uriFunction) {
attribute(URI_TEMPLATE_ATTRIBUTE, uriTemplate);
UriBuilder uriBuilder = uriBuilderFactory.uriString(uriTemplate);
attribute(URI_TEMPLATE_ATTRIBUTE, uriBuilder.toUriString());
return uri(uriFunction.apply(DefaultRestClient.this.uriBuilderFactory.uriString(uriTemplate)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,45 @@ void setupEach() {

RestClient.Builder createBuilder() {
return RestClient.builder()
.baseUrl("https://example.com/base")
.messageConverters(converters -> converters.add(0, this.converter))
.requestFactory(this.requestFactory)
.observationRegistry(this.observationRegistry);
}

@Test
void shouldContributeTemplateWhenUriVariables() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

client.get().uri("https://example.com/hotels/{hotel}/bookings/{booking}", "42", "21")
client.get().uri("/hotels/{hotel}/bookings/{booking}", "42", "21")
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/hotels/{hotel}/bookings/{booking}");
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
void shouldContributeTemplateWhenMap() throws Exception {
mockSentRequest(GET, "https://example.com/hotels/42/bookings/21");
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

Map<String, String> vars = Map.of("hotel", "42", "booking", "21");

client.get().uri("https://example.com/hotels/{hotel}/bookings/{booking}", vars)
client.get().uri("/hotels/{hotel}/bookings/{booking}", vars)
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/hotels/{hotel}/bookings/{booking}");
assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
void shouldContributeTemplateWhenFunction() throws Exception {
mockSentRequest(GET, "https://example.com/base/hotels/42/bookings/21");
mockResponseStatus(HttpStatus.OK);

client.get().uri("/hotels/{hotel}/bookings/{booking}", builder -> builder.build("42", "21"))
.retrieve().toBodilessEntity();

assertThatHttpObservation().hasLowCardinalityKeyValue("uri", "/base/hotels/{hotel}/bookings/{booking}");
}

@Test
Expand Down