Skip to content

Commit 08ccf46

Browse files
committed
Rename request param version strategy to query param
Closes gh-35263
1 parent 18ee8ad commit 08ccf46

File tree

7 files changed

+129
-10
lines changed

7 files changed

+129
-10
lines changed

framework-docs/modules/ROOT/pages/web/webflux-versioning.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ directly with it.
4646
[.small]#xref:web/webmvc-versioning.adoc#mvc-versioning-resolver[See equivalent in the Servlet stack]#
4747

4848
This strategy resolves the API version from a request. The WebFlux config provides built-in
49-
options to resolve from a header, a request parameter, or from the URL path.
50-
You can also use a custom `ApiVersionResolver`.
49+
options to resolve from a header, query parameter, media type parameter,
50+
or from the URL path. You can also use a custom `ApiVersionResolver`.
5151

5252

5353

framework-docs/modules/ROOT/pages/web/webmvc-versioning.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ directly with it.
4646
[.small]#xref:web/webflux-versioning.adoc#webflux-versioning-resolver[See equivalent in the Reactive stack]#
4747

4848
This strategy resolves the API version from a request. The MVC config provides built-in
49-
options to resolve from a header, from a request parameter, or from the URL path.
50-
You can also use a custom `ApiVersionResolver`.
49+
options to resolve from a header, query parameter, media type parameter,
50+
or from the URL path. You can also use a custom `ApiVersionResolver`.
5151

5252

5353

spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ApiVersionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void queryParam() {
5454
String param = "api-version";
5555

5656
Map<String, String> result = performRequest(
57-
configurer -> configurer.useRequestParam(param),
57+
configurer -> configurer.useQueryParam(param),
5858
ApiVersionInserter.useQueryParam(param));
5959

6060
assertThat(result.get("query")).isEqualTo(param + "=1.2");
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.accept;
18+
19+
20+
import jakarta.servlet.http.HttpServletRequest;
21+
import org.jspecify.annotations.Nullable;
22+
23+
import org.springframework.util.StringUtils;
24+
import org.springframework.web.util.UriComponents;
25+
import org.springframework.web.util.UriComponentsBuilder;
26+
27+
/**
28+
* {@link ApiVersionResolver} that extract the version from a query parameter.
29+
*
30+
* @author Rossen Stoyanchev
31+
* @since 7.0
32+
*/
33+
public class QueryApiVersionResolver implements ApiVersionResolver {
34+
35+
private final String queryParamName;
36+
37+
38+
public QueryApiVersionResolver(String queryParamName) {
39+
this.queryParamName = queryParamName;
40+
}
41+
42+
43+
@Override
44+
public @Nullable String resolveVersion(HttpServletRequest request) {
45+
String query = request.getQueryString();
46+
if (StringUtils.hasText(query)) {
47+
UriComponents uri = UriComponentsBuilder.fromUriString("?" + query).build();
48+
return uri.getQueryParams().getFirst(this.queryParamName);
49+
}
50+
return null;
51+
}
52+
53+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.accept;
18+
19+
20+
import org.jspecify.annotations.Nullable;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Unit tests for {@link QueryApiVersionResolver}.
29+
* @author Rossen Stoyanchev
30+
*/
31+
public class QueryApiVersionResolverTests {
32+
33+
private final String queryParamName = "api-version";
34+
35+
private final QueryApiVersionResolver resolver = new QueryApiVersionResolver(queryParamName);
36+
37+
38+
@Test
39+
void resolve() {
40+
MockHttpServletRequest request = initRequest("q=foo&" + queryParamName + "=1.2");
41+
String version = resolver.resolveVersion(request);
42+
assertThat(version).isEqualTo("1.2");
43+
}
44+
45+
@Test
46+
void noQueryString() {
47+
MockHttpServletRequest request = initRequest(null);
48+
String version = resolver.resolveVersion(request);
49+
assertThat(version).isNull();
50+
}
51+
52+
@Test
53+
void noQueryParam() {
54+
MockHttpServletRequest request = initRequest("q=foo");
55+
String version = resolver.resolveVersion(request);
56+
assertThat(version).isNull();
57+
}
58+
59+
private static MockHttpServletRequest initRequest(@Nullable String queryString) {
60+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
61+
request.setQueryString(queryString);
62+
return request;
63+
}
64+
65+
}

spring-webflux/src/main/java/org/springframework/web/reactive/config/ApiVersionConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public ApiVersionConfigurer useRequestHeader(String headerName) {
7171
}
7272

7373
/**
74-
* Add a resolver that extracts the API version from a request parameter.
74+
* Add a resolver that extracts the API version from a query string parameter.
7575
* @param paramName the parameter name to check
7676
*/
77-
public ApiVersionConfigurer useRequestParam(String paramName) {
77+
public ApiVersionConfigurer useQueryParam(String paramName) {
7878
this.versionResolvers.add(exchange -> exchange.getRequest().getQueryParams().getFirst(paramName));
7979
return this;
8080
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ApiVersionConfigurer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.web.accept.InvalidApiVersionException;
3636
import org.springframework.web.accept.MediaTypeParamApiVersionResolver;
3737
import org.springframework.web.accept.PathApiVersionResolver;
38+
import org.springframework.web.accept.QueryApiVersionResolver;
3839
import org.springframework.web.accept.SemanticApiVersionParser;
3940
import org.springframework.web.accept.StandardApiVersionDeprecationHandler;
4041

@@ -71,11 +72,11 @@ public ApiVersionConfigurer useRequestHeader(String headerName) {
7172
}
7273

7374
/**
74-
* Add resolver to extract the version from a request parameter.
75+
* Add resolver to extract the version from a query string parameter.
7576
* @param paramName the parameter name to check
7677
*/
77-
public ApiVersionConfigurer useRequestParam(String paramName) {
78-
this.versionResolvers.add(request -> request.getParameter(paramName));
78+
public ApiVersionConfigurer useQueryParam(String paramName) {
79+
this.versionResolvers.add(new QueryApiVersionResolver(paramName));
7980
return this;
8081
}
8182

0 commit comments

Comments
 (0)