Add dedicated ApiVersionResolver implementations

Closes gh-35747
This commit is contained in:
rstoyanchev 2025-11-03 09:19:38 +00:00
parent 29a76a6c70
commit 183a9466c5
8 changed files with 310 additions and 3 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.accept;
import jakarta.servlet.http.HttpServletRequest;
import org.jspecify.annotations.Nullable;
/**
* {@link ApiVersionResolver} that extract the version from a header.
*
* @author Rossen Stoyanchev
* @since 7.0
*/
public class HeaderApiVersionResolver implements ApiVersionResolver {
private final String headerName;
public HeaderApiVersionResolver(String headerName) {
this.headerName = headerName;
}
@Override
public @Nullable String resolveVersion(HttpServletRequest request) {
return request.getHeader(this.headerName);
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.accept;
import org.junit.jupiter.api.Test;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link HeaderApiVersionResolver}.
* @author Rossen Stoyanchev
*/
public class HeaderApiVersionResolverTests {
private final String headerName = "Api-Version";
private final HeaderApiVersionResolver resolver = new HeaderApiVersionResolver(headerName);
@Test
void resolve() {
String version = "1.2";
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
request.addHeader(headerName, version);
String actual = resolver.resolveVersion(request);
assertThat(actual).isEqualTo(version);
}
@Test
void noHeader() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
String version = resolver.resolveVersion(request);
assertThat(version).isNull();
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.accept;
import org.jspecify.annotations.Nullable;
import org.springframework.web.server.ServerWebExchange;
/**
* {@link ApiVersionResolver} that extract the version from a request header.
*
* @author Rossen Stoyanchev
* @since 7.0
*/
public class HeaderApiVersionResolver implements ApiVersionResolver {
private final String headerName;
public HeaderApiVersionResolver(String headerName) {
this.headerName = headerName;
}
@Override
public @Nullable String resolveVersion(ServerWebExchange exchange) {
return exchange.getRequest().getHeaders().getFirst(this.headerName);
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.accept;
import org.jspecify.annotations.Nullable;
import org.springframework.web.server.ServerWebExchange;
/**
* {@link ApiVersionResolver} that extract the version from a query parameter.
*
* @author Rossen Stoyanchev
* @since 7.0
*/
public class QueryApiVersionResolver implements ApiVersionResolver {
private final String queryParamName;
public QueryApiVersionResolver(String queryParamName) {
this.queryParamName = queryParamName;
}
@Override
public @Nullable String resolveVersion(ServerWebExchange exchange) {
return exchange.getRequest().getQueryParams().getFirst(this.queryParamName);
}
}

View File

@ -35,8 +35,10 @@ import org.springframework.web.reactive.accept.ApiVersionDeprecationHandler;
import org.springframework.web.reactive.accept.ApiVersionResolver;
import org.springframework.web.reactive.accept.ApiVersionStrategy;
import org.springframework.web.reactive.accept.DefaultApiVersionStrategy;
import org.springframework.web.reactive.accept.HeaderApiVersionResolver;
import org.springframework.web.reactive.accept.MediaTypeParamApiVersionResolver;
import org.springframework.web.reactive.accept.PathApiVersionResolver;
import org.springframework.web.reactive.accept.QueryApiVersionResolver;
import org.springframework.web.reactive.accept.StandardApiVersionDeprecationHandler;
/**
@ -69,7 +71,7 @@ public class ApiVersionConfigurer {
* @param headerName the header name to check
*/
public ApiVersionConfigurer useRequestHeader(String headerName) {
this.versionResolvers.add(exchange -> exchange.getRequest().getHeaders().getFirst(headerName));
this.versionResolvers.add(new HeaderApiVersionResolver(headerName));
return this;
}
@ -78,7 +80,7 @@ public class ApiVersionConfigurer {
* @param paramName the parameter name to check
*/
public ApiVersionConfigurer useQueryParam(String paramName) {
this.versionResolvers.add(exchange -> exchange.getRequest().getQueryParams().getFirst(paramName));
this.versionResolvers.add(new QueryApiVersionResolver(paramName));
return this;
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.accept;
import org.junit.jupiter.api.Test;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest.get;
/**
* Unit tests for {@link HeaderApiVersionResolver}.
* @author Rossen Stoyanchev
*/
public class HeaderApiVersionResolverTests {
private final String headerName = "Api-Version";
private final HeaderApiVersionResolver resolver = new HeaderApiVersionResolver(headerName);
@Test
void resolve() {
String version = "1.2";
ServerWebExchange exchange = MockServerWebExchange.from(get("/").header(headerName, version));
String actual = resolver.resolveVersion(exchange);
assertThat(actual).isEqualTo(version);
}
@Test
void noHeader() {
ServerWebExchange exchange = MockServerWebExchange.from(get("/"));
String version = resolver.resolveVersion(exchange);
assertThat(version).isNull();
}
}

View File

@ -0,0 +1,65 @@
/*
* Copyright 2002-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.accept;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.testfixture.server.MockServerWebExchange;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link QueryApiVersionResolver}.
* @author Rossen Stoyanchev
*/
public class QueryApiVersionResolverTests {
private final String queryParamName = "api-version";
private final QueryApiVersionResolver resolver = new QueryApiVersionResolver(queryParamName);
@Test
void resolve() {
ServerWebExchange exchange = initExchange("q=foo&" + queryParamName + "=1.2");
String version = resolver.resolveVersion(exchange);
assertThat(version).isEqualTo("1.2");
}
@Test
void noQueryString() {
ServerWebExchange exchange = initExchange(null);
String version = resolver.resolveVersion(exchange);
assertThat(version).isNull();
}
@Test
void noQueryParam() {
ServerWebExchange exchange = initExchange("q=foo");
String version = resolver.resolveVersion(exchange);
assertThat(version).isNull();
}
private static ServerWebExchange initExchange(@Nullable String queryString) {
return MockServerWebExchange.from(MockServerHttpRequest.get("/path?" + queryString));
}
}

View File

@ -33,6 +33,7 @@ import org.springframework.web.accept.ApiVersionParser;
import org.springframework.web.accept.ApiVersionResolver;
import org.springframework.web.accept.ApiVersionStrategy;
import org.springframework.web.accept.DefaultApiVersionStrategy;
import org.springframework.web.accept.HeaderApiVersionResolver;
import org.springframework.web.accept.InvalidApiVersionException;
import org.springframework.web.accept.MediaTypeParamApiVersionResolver;
import org.springframework.web.accept.PathApiVersionResolver;
@ -70,7 +71,7 @@ public class ApiVersionConfigurer {
* @param headerName the header name to check
*/
public ApiVersionConfigurer useRequestHeader(String headerName) {
this.versionResolvers.add(request -> request.getHeader(headerName));
this.versionResolvers.add(new HeaderApiVersionResolver(headerName));
return this;
}