Skip to content

Commit 00543be

Browse files
committed
Allow explicitly sending no API version
Closes gh-35566
1 parent 891c3e3 commit 00543be

File tree

8 files changed

+73
-20
lines changed

8 files changed

+73
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public RequestBodySpec attributes(Consumer<Map<String, Object>> attributesConsum
248248
}
249249

250250
@Override
251-
public RequestBodySpec apiVersion(Object version) {
251+
public RequestBodySpec apiVersion(@Nullable Object version) {
252252
this.requestHeadersUriSpec.apiVersion(version);
253253
return this;
254254
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,14 +479,19 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
479479

480480
/**
481481
* Set an API version for the request. The version is inserted into the
482-
* request by the {@linkplain Builder#apiVersionInserter(ApiVersionInserter)
482+
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
483483
* configured} {@code ApiVersionInserter}.
484-
* @param version the API version of the request; this can be a String or
485-
* some Object that can be formatted by the inserter &mdash; for example,
486-
* through an {@link ApiVersionFormatter}
484+
* <p>If no version is set, the
485+
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
486+
* if configured.
487+
* <p>If {@code null} is passed, then an API version is not inserted
488+
* irrespective of default version settings.
489+
* @param version the API version for the request; this can be a String
490+
* or some Object that can be formatted the inserter, e.g. through an
491+
* {@link ApiVersionFormatter}.
487492
* @return this spec for further declaration of the request
488493
*/
489-
S apiVersion(Object version);
494+
S apiVersion(@Nullable Object version);
490495

491496
/**
492497
* Set the attribute with the given name to the given value.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ private static <T> Class<T> bodyClass(Type type) {
293293

294294
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
295295

296+
private static final Object NO_VERSION = new Object();
297+
296298
private final HttpMethod httpMethod;
297299

298300
private @Nullable URI uri;
@@ -430,8 +432,8 @@ public DefaultRequestBodyUriSpec ifNoneMatch(String... ifNoneMatches) {
430432
}
431433

432434
@Override
433-
public RequestBodySpec apiVersion(Object version) {
434-
this.apiVersion = version;
435+
public RequestBodySpec apiVersion(@Nullable Object version) {
436+
this.apiVersion = (version != null ? version : NO_VERSION);
435437
return this;
436438
}
437439

@@ -646,7 +648,15 @@ private URI initUri() {
646648
}
647649

648650
private @Nullable Object getApiVersionOrDefault() {
649-
return (this.apiVersion != null ? this.apiVersion : DefaultRestClient.this.defaultApiVersion);
651+
if (this.apiVersion == null) {
652+
return DefaultRestClient.this.defaultApiVersion;
653+
}
654+
else if (this.apiVersion == NO_VERSION) {
655+
return null;
656+
}
657+
else {
658+
return this.apiVersion;
659+
}
650660
}
651661

652662
private @Nullable String serializeCookies() {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,19 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
642642

643643
/**
644644
* Set an API version for the request. The version is inserted into the
645-
* request by the {@link Builder#apiVersionInserter(ApiVersionInserter)
645+
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
646646
* configured} {@code ApiVersionInserter}.
647-
* @param version the API version of the request; this can be a String or
648-
* some Object that can be formatted the inserter, e.g. through an
647+
* <p>If no version is set, the
648+
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
649+
* if configured.
650+
* <p>If {@code null} is passed, then an API version is not inserted
651+
* irrespective of default version settings.
652+
* @param version the API version for the request; this can be a String
653+
* or some Object that can be formatted the inserter, e.g. through an
649654
* {@link ApiVersionFormatter}.
650655
* @since 7.0
651656
*/
652-
S apiVersion(Object version);
657+
S apiVersion(@Nullable Object version);
653658

654659
/**
655660
* Set the attribute with the given name to the given value.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ void defaultVersion() {
108108
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isEqualTo("1.2"));
109109
}
110110

111+
@Test
112+
void noVersion() {
113+
ApiVersionInserter inserter = ApiVersionInserter.useHeader("API-Version");
114+
RestClient restClient = restClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
115+
restClient.get().uri("/path").apiVersion(null).retrieve().body(String.class);
116+
117+
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isNull());
118+
}
119+
111120
private void performRequest(ApiVersionInserter versionInserter) {
112121
restClientBuilder.apiVersionInserter(versionInserter).build()
113122
.post().uri("/path")

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ private static <T> Mono<T> releaseIfNotConsumed(ClientResponse response, Throwab
206206

207207
private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec {
208208

209+
private static final Object NO_VERSION = new Object();
210+
209211
private final HttpMethod httpMethod;
210212

211213
private @Nullable URI uri;
@@ -335,8 +337,8 @@ public DefaultRequestBodyUriSpec ifNoneMatch(String... ifNoneMatches) {
335337
}
336338

337339
@Override
338-
public DefaultRequestBodyUriSpec apiVersion(Object version) {
339-
this.apiVersion = version;
340+
public DefaultRequestBodyUriSpec apiVersion(@Nullable Object version) {
341+
this.apiVersion = (version != null ? version : NO_VERSION);
340342
return this;
341343
}
342344

@@ -503,7 +505,15 @@ private URI initUri() {
503505
}
504506

505507
private @Nullable Object getApiVersionOrDefault() {
506-
return (this.apiVersion != null ? this.apiVersion : DefaultWebClient.this.defaultApiVersion);
508+
if (this.apiVersion == null) {
509+
return DefaultWebClient.this.defaultApiVersion;
510+
}
511+
else if (this.apiVersion == NO_VERSION) {
512+
return null;
513+
}
514+
else {
515+
return this.apiVersion;
516+
}
507517
}
508518

509519
private void initHeaders(HttpHeaders out) {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,19 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
497497

498498
/**
499499
* Set an API version for the request. The version is inserted into the
500-
* request by the {@link Builder#apiVersionInserter(ApiVersionInserter)
500+
* request through the {@link Builder#apiVersionInserter(ApiVersionInserter)
501501
* configured} {@code ApiVersionInserter}.
502-
* @param version the API version of the request; this can be a String or
503-
* some Object that can be formatted the inserter, e.g. through an
502+
* <p>If no version is set, the
503+
* {@link Builder#defaultApiVersion(Object) defaultApiVersion} is used,
504+
* if configured.
505+
* <p>If {@code null} is passed, then an API version is not inserted
506+
* irrespective of default version settings.
507+
* @param version the API version for the request; this can be a String
508+
* or some Object that can be formatted the inserter, e.g. through an
504509
* {@link ApiVersionFormatter}.
505510
* @since 7.0
506511
*/
507-
S apiVersion(Object version);
512+
S apiVersion(@Nullable Object version);
508513

509514
/**
510515
* Set the attribute with the given name to the given value.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ void defaultVersion() {
9999
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isEqualTo("1.2"));
100100
}
101101

102+
@Test
103+
void noVersion() {
104+
ApiVersionInserter inserter = ApiVersionInserter.useHeader("API-Version");
105+
WebClient webClient = webClientBuilder.defaultApiVersion(1.2).apiVersionInserter(inserter).build();
106+
webClient.get().uri("/path").apiVersion(null).retrieve().bodyToMono(String.class).block();
107+
108+
expectRequest(request -> assertThat(request.getHeaders().get("API-Version")).isNull());
109+
}
110+
102111
private void performRequest(ApiVersionInserter versionInserter) {
103112
WebClient webClient = webClientBuilder.apiVersionInserter(versionInserter).build();
104113
webClient.get().uri("/path").apiVersion(1.2).retrieve().bodyToMono(String.class).block();

0 commit comments

Comments
 (0)