Allow explicitly sending no API version

Closes gh-35566
This commit is contained in:
rstoyanchev
2025-10-08 12:24:37 +01:00
parent 891c3e3a1f
commit 00543becf8
8 changed files with 73 additions and 20 deletions
@@ -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;
}
@@ -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.
@@ -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 {
}
@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 {
}
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() {
@@ -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.
@@ -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")
@@ -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 {
}
@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 {
}
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) {
@@ -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.
@@ -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();