From 986ffc2072d820e83e64a02cad7bdd2b85c4ccf8 Mon Sep 17 00:00:00 2001 From: sonallux Date: Thu, 21 Nov 2024 00:47:31 +0100 Subject: [PATCH] Use full URI for URI template keyvalue with RestClient Prior to this commit, `RestClient` would not use the full URI created by the uri handler as a template request attribute. This means that HTTP client observations would not contain the base URI in recorded observations as the uri template keyvalue. Closes gh-33928 --- .../web/client/DefaultRestClient.java | 9 ++++--- .../client/RestClientObservationTests.java | 24 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java index 0572b986c9d5..9e88dfdc91a0 100644 --- a/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java +++ b/spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java @@ -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 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 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))); } diff --git a/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java b/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java index 8181151af9fa..1fe64dab761c 100644 --- a/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/RestClientObservationTests.java @@ -83,6 +83,7 @@ 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); @@ -90,26 +91,37 @@ RestClient.Builder createBuilder() { @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 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