Skip to content

Commit 183a946

Browse files
committed
Add dedicated ApiVersionResolver implementations
Closes gh-35747
1 parent 29a76a6 commit 183a946

File tree

8 files changed

+310
-3
lines changed

8 files changed

+310
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
/**
24+
* {@link ApiVersionResolver} that extract the version from a header.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 7.0
28+
*/
29+
public class HeaderApiVersionResolver implements ApiVersionResolver {
30+
31+
private final String headerName;
32+
33+
34+
public HeaderApiVersionResolver(String headerName) {
35+
this.headerName = headerName;
36+
}
37+
38+
39+
@Override
40+
public @Nullable String resolveVersion(HttpServletRequest request) {
41+
return request.getHeader(this.headerName);
42+
}
43+
44+
}
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 org.junit.jupiter.api.Test;
21+
22+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Unit tests for {@link HeaderApiVersionResolver}.
28+
* @author Rossen Stoyanchev
29+
*/
30+
public class HeaderApiVersionResolverTests {
31+
32+
private final String headerName = "Api-Version";
33+
34+
private final HeaderApiVersionResolver resolver = new HeaderApiVersionResolver(headerName);
35+
36+
37+
@Test
38+
void resolve() {
39+
String version = "1.2";
40+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
41+
request.addHeader(headerName, version);
42+
String actual = resolver.resolveVersion(request);
43+
assertThat(actual).isEqualTo(version);
44+
}
45+
46+
@Test
47+
void noHeader() {
48+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
49+
String version = resolver.resolveVersion(request);
50+
assertThat(version).isNull();
51+
}
52+
53+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.reactive.accept;
18+
19+
import org.jspecify.annotations.Nullable;
20+
21+
import org.springframework.web.server.ServerWebExchange;
22+
23+
/**
24+
* {@link ApiVersionResolver} that extract the version from a request header.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 7.0
28+
*/
29+
public class HeaderApiVersionResolver implements ApiVersionResolver {
30+
31+
private final String headerName;
32+
33+
34+
public HeaderApiVersionResolver(String headerName) {
35+
this.headerName = headerName;
36+
}
37+
38+
39+
@Override
40+
public @Nullable String resolveVersion(ServerWebExchange exchange) {
41+
return exchange.getRequest().getHeaders().getFirst(this.headerName);
42+
}
43+
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.reactive.accept;
18+
19+
import org.jspecify.annotations.Nullable;
20+
21+
import org.springframework.web.server.ServerWebExchange;
22+
23+
/**
24+
* {@link ApiVersionResolver} that extract the version from a query parameter.
25+
*
26+
* @author Rossen Stoyanchev
27+
* @since 7.0
28+
*/
29+
public class QueryApiVersionResolver implements ApiVersionResolver {
30+
31+
private final String queryParamName;
32+
33+
34+
public QueryApiVersionResolver(String queryParamName) {
35+
this.queryParamName = queryParamName;
36+
}
37+
38+
39+
@Override
40+
public @Nullable String resolveVersion(ServerWebExchange exchange) {
41+
return exchange.getRequest().getQueryParams().getFirst(this.queryParamName);
42+
}
43+
44+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
import org.springframework.web.reactive.accept.ApiVersionResolver;
3636
import org.springframework.web.reactive.accept.ApiVersionStrategy;
3737
import org.springframework.web.reactive.accept.DefaultApiVersionStrategy;
38+
import org.springframework.web.reactive.accept.HeaderApiVersionResolver;
3839
import org.springframework.web.reactive.accept.MediaTypeParamApiVersionResolver;
3940
import org.springframework.web.reactive.accept.PathApiVersionResolver;
41+
import org.springframework.web.reactive.accept.QueryApiVersionResolver;
4042
import org.springframework.web.reactive.accept.StandardApiVersionDeprecationHandler;
4143

4244
/**
@@ -69,7 +71,7 @@ public class ApiVersionConfigurer {
6971
* @param headerName the header name to check
7072
*/
7173
public ApiVersionConfigurer useRequestHeader(String headerName) {
72-
this.versionResolvers.add(exchange -> exchange.getRequest().getHeaders().getFirst(headerName));
74+
this.versionResolvers.add(new HeaderApiVersionResolver(headerName));
7375
return this;
7476
}
7577

@@ -78,7 +80,7 @@ public ApiVersionConfigurer useRequestHeader(String headerName) {
7880
* @param paramName the parameter name to check
7981
*/
8082
public ApiVersionConfigurer useQueryParam(String paramName) {
81-
this.versionResolvers.add(exchange -> exchange.getRequest().getQueryParams().getFirst(paramName));
83+
this.versionResolvers.add(new QueryApiVersionResolver(paramName));
8284
return this;
8385
}
8486

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.reactive.accept;
18+
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.web.server.ServerWebExchange;
23+
import org.springframework.web.testfixture.server.MockServerWebExchange;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get;
27+
28+
/**
29+
* Unit tests for {@link HeaderApiVersionResolver}.
30+
* @author Rossen Stoyanchev
31+
*/
32+
public class HeaderApiVersionResolverTests {
33+
34+
private final String headerName = "Api-Version";
35+
36+
private final HeaderApiVersionResolver resolver = new HeaderApiVersionResolver(headerName);
37+
38+
39+
@Test
40+
void resolve() {
41+
String version = "1.2";
42+
ServerWebExchange exchange = MockServerWebExchange.from(get("/").header(headerName, version));
43+
String actual = resolver.resolveVersion(exchange);
44+
assertThat(actual).isEqualTo(version);
45+
}
46+
47+
@Test
48+
void noHeader() {
49+
ServerWebExchange exchange = MockServerWebExchange.from(get("/"));
50+
String version = resolver.resolveVersion(exchange);
51+
assertThat(version).isNull();
52+
}
53+
54+
}
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.reactive.accept;
18+
19+
20+
import org.jspecify.annotations.Nullable;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.web.server.ServerWebExchange;
24+
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
25+
import org.springframework.web.testfixture.server.MockServerWebExchange;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Unit tests for {@link QueryApiVersionResolver}.
31+
* @author Rossen Stoyanchev
32+
*/
33+
public class QueryApiVersionResolverTests {
34+
35+
private final String queryParamName = "api-version";
36+
37+
private final QueryApiVersionResolver resolver = new QueryApiVersionResolver(queryParamName);
38+
39+
40+
@Test
41+
void resolve() {
42+
ServerWebExchange exchange = initExchange("q=foo&" + queryParamName + "=1.2");
43+
String version = resolver.resolveVersion(exchange);
44+
assertThat(version).isEqualTo("1.2");
45+
}
46+
47+
@Test
48+
void noQueryString() {
49+
ServerWebExchange exchange = initExchange(null);
50+
String version = resolver.resolveVersion(exchange);
51+
assertThat(version).isNull();
52+
}
53+
54+
@Test
55+
void noQueryParam() {
56+
ServerWebExchange exchange = initExchange("q=foo");
57+
String version = resolver.resolveVersion(exchange);
58+
assertThat(version).isNull();
59+
}
60+
61+
private static ServerWebExchange initExchange(@Nullable String queryString) {
62+
return MockServerWebExchange.from(MockServerHttpRequest.get("/path?" + queryString));
63+
}
64+
65+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.web.accept.ApiVersionResolver;
3434
import org.springframework.web.accept.ApiVersionStrategy;
3535
import org.springframework.web.accept.DefaultApiVersionStrategy;
36+
import org.springframework.web.accept.HeaderApiVersionResolver;
3637
import org.springframework.web.accept.InvalidApiVersionException;
3738
import org.springframework.web.accept.MediaTypeParamApiVersionResolver;
3839
import org.springframework.web.accept.PathApiVersionResolver;
@@ -70,7 +71,7 @@ public class ApiVersionConfigurer {
7071
* @param headerName the header name to check
7172
*/
7273
public ApiVersionConfigurer useRequestHeader(String headerName) {
73-
this.versionResolvers.add(request -> request.getHeader(headerName));
74+
this.versionResolvers.add(new HeaderApiVersionResolver(headerName));
7475
return this;
7576
}
7677

0 commit comments

Comments
 (0)