Browse Source

Fix URI override for HttpExchange

Closes gh-29624
pull/29632/head
rstoyanchev 2 years ago
parent
commit
a09f93768a
  1. 30
      spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java
  2. 1
      spring-web/src/main/java/org/springframework/web/service/invoker/UrlArgumentResolver.java
  3. 7
      spring-web/src/test/java/org/springframework/web/service/invoker/UrlArgumentResolverTests.java
  4. 23
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyTests.java

30
spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java

@ -115,9 +115,11 @@ public final class HttpRequestValues { @@ -115,9 +115,11 @@ public final class HttpRequestValues {
}
/**
* Return the full URL to use, if set.
* <p>This is mutually exclusive with {@link #getUriTemplate() uriTemplate}.
* One of the two has a value but not both.
* Return the URL to use.
* <p>Typically, this comes from a {@link URI} method argument, which provides
* the caller with the option to override the {@link #getUriTemplate()
* uriTemplate} from class and method {@code HttpExchange} annotations.
* annotation.
*/
@Nullable
public URI getUri() {
@ -125,9 +127,8 @@ public final class HttpRequestValues { @@ -125,9 +127,8 @@ public final class HttpRequestValues {
}
/**
* Return the URL template for the request, if set.
* <p>This is mutually exclusive with a {@linkplain #getUri() full URL}.
* One of the two has a value but not both.
* Return the URL template for the request. This comes from the values in
* class and method {@code HttpExchange} annotations.
*/
@Nullable
public String getUriTemplate() {
@ -248,38 +249,29 @@ public final class HttpRequestValues { @@ -248,38 +249,29 @@ public final class HttpRequestValues {
}
/**
* Set the request URL as a full URL.
* <p>This is mutually exclusive with, and resets any previously set
* {@linkplain #setUriTemplate(String) URI template} or
* {@linkplain #setUriVariable(String, String) URI variables}.
* Set the URL to use. When set, this overrides the
* {@linkplain #setUriTemplate(String) URI template} from the
* {@code HttpExchange} annotation.
*/
public Builder setUri(URI uri) {
this.uri = uri;
this.uriTemplate = null;
this.uriVars = null;
return this;
}
/**
* Set the request URL as a String template.
* <p>This is mutually exclusive with, and resets any previously set
* {@linkplain #setUri(URI) full URI}.
*/
public Builder setUriTemplate(String uriTemplate) {
this.uriTemplate = uriTemplate;
this.uri = null;
return this;
}
/**
* Add a URI variable name-value pair.
* <p>This is mutually exclusive with, and resets any previously set
* {@linkplain #setUri(URI) full URI}.
*/
public Builder setUriVariable(String name, String value) {
this.uriVars = (this.uriVars != null ? this.uriVars : new LinkedHashMap<>());
this.uriVars.put(name, value);
this.uri = null;
return this;
}
@ -399,7 +391,7 @@ public final class HttpRequestValues { @@ -399,7 +391,7 @@ public final class HttpRequestValues {
public HttpRequestValues build() {
URI uri = this.uri;
String uriTemplate = (this.uriTemplate != null || uri != null ? this.uriTemplate : "");
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
Object bodyValue = this.bodyValue;

1
spring-web/src/main/java/org/springframework/web/service/invoker/UrlArgumentResolver.java

@ -40,7 +40,6 @@ public class UrlArgumentResolver implements HttpServiceArgumentResolver { @@ -40,7 +40,6 @@ public class UrlArgumentResolver implements HttpServiceArgumentResolver {
if (argument != null) {
requestValues.setUri((URI) argument);
return true;
}
return true;

7
spring-web/src/test/java/org/springframework/web/service/invoker/UrlArgumentResolverTests.java

@ -21,6 +21,7 @@ import java.net.URI; @@ -21,6 +21,7 @@ import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.lang.Nullable;
import org.springframework.web.service.annotation.GetExchange;
import static org.assertj.core.api.Assertions.assertThat;
@ -51,7 +52,7 @@ public class UrlArgumentResolverTests { @@ -51,7 +52,7 @@ public class UrlArgumentResolverTests {
this.service.execute(dynamicUrl);
assertThat(getRequestValues().getUri()).isEqualTo(dynamicUrl);
assertThat(getRequestValues().getUriTemplate()).isNull();
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
}
@Test
@ -67,7 +68,9 @@ public class UrlArgumentResolverTests { @@ -67,7 +68,9 @@ public class UrlArgumentResolverTests {
@Test
void ignoreNull() {
this.service.execute(null);
assertThat(getRequestValues().getUri()).isNull();
assertThat(getRequestValues().getUriTemplate()).isEqualTo("/path");
}
private HttpRequestValues getRequestValues() {
@ -78,7 +81,7 @@ public class UrlArgumentResolverTests { @@ -78,7 +81,7 @@ public class UrlArgumentResolverTests {
private interface Service {
@GetExchange("/path")
void execute(URI uri);
void execute(@Nullable URI uri);
@GetExchange
void executeNotUri(String other);

23
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientHttpServiceProxyTests.java

@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.client.support; @@ -18,6 +18,7 @@ package org.springframework.web.reactive.function.client.support;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@ -31,6 +32,8 @@ import org.junit.jupiter.api.Test; @@ -31,6 +32,8 @@ import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.annotation.GetExchange;
@ -77,7 +80,7 @@ public class WebClientHttpServiceProxyTests { @@ -77,7 +80,7 @@ public class WebClientHttpServiceProxyTests {
}
@Test
void greetingWithRequestAttribute() throws Exception {
void greetingWithRequestAttribute() {
Map<String, Object> attributes = new HashMap<>();
@ -100,7 +103,19 @@ public class WebClientHttpServiceProxyTests { @@ -100,7 +103,19 @@ public class WebClientHttpServiceProxyTests {
assertThat(attributes).containsEntry("myAttribute", "myAttributeValue");
}
private TestHttpService initHttpService() throws Exception {
@Test // gh-29624
void uri() throws Exception {
String expectedBody = "hello";
prepareResponse(response -> response.setResponseCode(200).setBody(expectedBody));
URI dynamicUri = this.server.url("/greeting/123").uri();
String actualBody = initHttpService().getGreetingById(dynamicUri, "456");
assertThat(actualBody).isEqualTo(expectedBody);
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
}
private TestHttpService initHttpService() {
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
return initHttpService(webClient);
}
@ -127,7 +142,9 @@ public class WebClientHttpServiceProxyTests { @@ -127,7 +142,9 @@ public class WebClientHttpServiceProxyTests {
@GetExchange("/greeting")
Mono<String> getGreetingWithAttribute(@RequestAttribute String myAttribute);
}
@GetExchange("/greetings/{id}")
String getGreetingById(@Nullable URI uri, @PathVariable String id);
}
}

Loading…
Cancel
Save