Browse Source

Allow explicitly sending no API version

Closes gh-35566
pull/35588/head
rstoyanchev 3 months ago
parent
commit
00543becf8
  1. 2
      spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java
  2. 15
      spring-test/src/main/java/org/springframework/test/web/servlet/client/RestTestClient.java
  3. 16
      spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java
  4. 13
      spring-web/src/main/java/org/springframework/web/client/RestClient.java
  5. 9
      spring-web/src/test/java/org/springframework/web/client/RestClientVersionTests.java
  6. 16
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java
  7. 13
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java
  8. 9
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientVersionTests.java

2
spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java

@ -248,7 +248,7 @@ class DefaultRestTestClient implements RestTestClient { @@ -248,7 +248,7 @@ class DefaultRestTestClient implements RestTestClient {
}
@Override
public RequestBodySpec apiVersion(Object version) {
public RequestBodySpec apiVersion(@Nullable Object version) {
this.requestHeadersUriSpec.apiVersion(version);
return this;
}

15
spring-test/src/main/java/org/springframework/test/web/servlet/client/RestTestClient.java

@ -479,14 +479,19 @@ public interface RestTestClient { @@ -479,14 +479,19 @@ public interface RestTestClient {
/**
* Set an API version for the request. The version is inserted into the
* request by the {@linkplain Builder#apiVersionInserter(ApiVersionInserter)
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
* configured} {@code ApiVersionInserter}.
* @param version the API version of the request; this can be a String or
* some Object that can be formatted by the inserter — for example,
* through an {@link ApiVersionFormatter}
* <p>If no version is set, the
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
* if configured.
* <p>If {@code null} is passed, then an API version is not inserted
* irrespective of default version settings.
* @param version the API version for the request; this can be a String
* or some Object that can be formatted the inserter, e.g. through an
* {@link ApiVersionFormatter}.
* @return this spec for further declaration of the request
*/
S apiVersion(Object version);
S apiVersion(@Nullable Object version);
/**
* Set the attribute with the given name to the given value.

16
spring-web/src/main/java/org/springframework/web/client/DefaultRestClient.java

@ -293,6 +293,8 @@ final class DefaultRestClient implements RestClient { @@ -293,6 +293,8 @@ final class DefaultRestClient implements RestClient {
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
private static final Object NO_VERSION = new Object();
private final HttpMethod httpMethod;
private @Nullable URI uri;
@ -430,8 +432,8 @@ final class DefaultRestClient implements RestClient { @@ -430,8 +432,8 @@ final class DefaultRestClient implements RestClient {
}
@Override
public RequestBodySpec apiVersion(Object version) {
this.apiVersion = version;
public RequestBodySpec apiVersion(@Nullable Object version) {
this.apiVersion = (version != null ? version : NO_VERSION);
return this;
}
@ -646,7 +648,15 @@ final class DefaultRestClient implements RestClient { @@ -646,7 +648,15 @@ final class DefaultRestClient implements RestClient {
}
private @Nullable Object getApiVersionOrDefault() {
return (this.apiVersion != null ? this.apiVersion : DefaultRestClient.this.defaultApiVersion);
if (this.apiVersion == null) {
return DefaultRestClient.this.defaultApiVersion;
}
else if (this.apiVersion == NO_VERSION) {
return null;
}
else {
return this.apiVersion;
}
}
private @Nullable String serializeCookies() {

13
spring-web/src/main/java/org/springframework/web/client/RestClient.java

@ -642,14 +642,19 @@ public interface RestClient { @@ -642,14 +642,19 @@ public interface RestClient {
/**
* Set an API version for the request. The version is inserted into the
* request by the {@link Builder#apiVersionInserter(ApiVersionInserter)
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
* configured} {@code ApiVersionInserter}.
* @param version the API version of the request; this can be a String or
* some Object that can be formatted the inserter, e.g. through an
* <p>If no version is set, the
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
* if configured.
* <p>If {@code null} is passed, then an API version is not inserted
* irrespective of default version settings.
* @param version the API version for the request; this can be a String
* or some Object that can be formatted the inserter, e.g. through an
* {@link ApiVersionFormatter}.
* @since 7.0
*/
S apiVersion(Object version);
S apiVersion(@Nullable Object version);
/**
* Set the attribute with the given name to the given value.

9
spring-web/src/test/java/org/springframework/web/client/RestClientVersionTests.java

@ -108,6 +108,15 @@ public class RestClientVersionTests { @@ -108,6 +108,15 @@ public class RestClientVersionTests {
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isEqualTo("1.2"));
}
@Test
void noVersion() {
ApiVersionInserter inserter = ApiVersionInserter.useHeader("API-Version");
RestClient restClient = restClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
restClient.get().uri("/path").apiVersion(null).retrieve().body(String.class);
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isNull());
}
private void performRequest(ApiVersionInserter versionInserter) {
restClientBuilder.apiVersionInserter(versionInserter).build()
.post().uri("/path")

16
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -206,6 +206,8 @@ final class DefaultWebClient implements WebClient { @@ -206,6 +206,8 @@ final class DefaultWebClient implements WebClient {
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
private static final Object NO_VERSION = new Object();
private final HttpMethod httpMethod;
private @Nullable URI uri;
@ -335,8 +337,8 @@ final class DefaultWebClient implements WebClient { @@ -335,8 +337,8 @@ final class DefaultWebClient implements WebClient {
}
@Override
public DefaultRequestBodyUriSpec apiVersion(Object version) {
this.apiVersion = version;
public DefaultRequestBodyUriSpec apiVersion(@Nullable Object version) {
this.apiVersion = (version != null ? version : NO_VERSION);
return this;
}
@ -503,7 +505,15 @@ final class DefaultWebClient implements WebClient { @@ -503,7 +505,15 @@ final class DefaultWebClient implements WebClient {
}
private @Nullable Object getApiVersionOrDefault() {
return (this.apiVersion != null ? this.apiVersion : DefaultWebClient.this.defaultApiVersion);
if (this.apiVersion == null) {
return DefaultWebClient.this.defaultApiVersion;
}
else if (this.apiVersion == NO_VERSION) {
return null;
}
else {
return this.apiVersion;
}
}
private void initHeaders(HttpHeaders out) {

13
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClient.java

@ -497,14 +497,19 @@ public interface WebClient { @@ -497,14 +497,19 @@ public interface WebClient {
/**
* Set an API version for the request. The version is inserted into the
* request by the {@link Builder#apiVersionInserter(ApiVersionInserter)
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
* configured} {@code ApiVersionInserter}.
* @param version the API version of the request; this can be a String or
* some Object that can be formatted the inserter, e.g. through an
* <p>If no version is set, the
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
* if configured.
* <p>If {@code null} is passed, then an API version is not inserted
* irrespective of default version settings.
* @param version the API version for the request; this can be a String
* or some Object that can be formatted the inserter, e.g. through an
* {@link ApiVersionFormatter}.
* @since 7.0
*/
S apiVersion(Object version);
S apiVersion(@Nullable Object version);
/**
* Set the attribute with the given name to the given value.

9
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientVersionTests.java

@ -99,6 +99,15 @@ public class WebClientVersionTests { @@ -99,6 +99,15 @@ public class WebClientVersionTests {
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isEqualTo("1.2"));
}
@Test
void noVersion() {
ApiVersionInserter inserter = ApiVersionInserter.useHeader("API-Version");
WebClient webClient = webClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
webClient.get().uri("/path").apiVersion(null).retrieve().bodyToMono(String.class).block();
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isNull());
}
private void performRequest(ApiVersionInserter versionInserter) {
WebClient webClient = webClientBuilder.apiVersionInserter(versionInserter).build();
webClient.get().uri("/path").apiVersion(1.2).retrieve().bodyToMono(String.class).block();

Loading…
Cancel
Save