mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Correctly apply required API version validation
Closes gh-35386
This commit is contained in:
@@ -70,17 +70,20 @@ public interface ApiVersionStrategy {
|
||||
*/
|
||||
default @Nullable Comparable<?> resolveParseAndValidateVersion(HttpServletRequest request) {
|
||||
String value = resolveVersion(request);
|
||||
Comparable<?> version;
|
||||
if (value == null) {
|
||||
return getDefaultVersion();
|
||||
version = getDefaultVersion();
|
||||
}
|
||||
try {
|
||||
Comparable<?> version = parseVersion(value);
|
||||
validateVersion(version, request);
|
||||
return version;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidApiVersionException(value, null, ex);
|
||||
else {
|
||||
try {
|
||||
version = parseVersion(value);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidApiVersionException(value, null, ex);
|
||||
}
|
||||
}
|
||||
validateVersion(version, request);
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+5
-4
@@ -35,8 +35,6 @@ public class DefaultApiVersionStrategiesTests {
|
||||
|
||||
private static final SemanticApiVersionParser parser = new SemanticApiVersionParser();
|
||||
|
||||
private final MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
|
||||
@Test
|
||||
void defaultVersionIsParsed() {
|
||||
@@ -113,8 +111,11 @@ public class DefaultApiVersionStrategiesTests {
|
||||
}
|
||||
|
||||
private void validateVersion(@Nullable String version, DefaultApiVersionStrategy strategy) {
|
||||
Comparable<?> parsedVersion = (version != null ? parser.parseVersion(version) : null);
|
||||
strategy.validateVersion(parsedVersion, request);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
if (version != null) {
|
||||
request.setParameter("api-version", version);
|
||||
}
|
||||
strategy.resolveParseAndValidateVersion(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-8
@@ -72,17 +72,20 @@ public interface ApiVersionStrategy {
|
||||
*/
|
||||
default @Nullable Comparable<?> resolveParseAndValidateVersion(ServerWebExchange exchange) {
|
||||
String value = resolveVersion(exchange);
|
||||
Comparable<?> version;
|
||||
if (value == null) {
|
||||
return getDefaultVersion();
|
||||
version = getDefaultVersion();
|
||||
}
|
||||
try {
|
||||
Comparable<?> version = parseVersion(value);
|
||||
validateVersion(version, exchange);
|
||||
return version;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidApiVersionException(value, null, ex);
|
||||
else {
|
||||
try {
|
||||
version = parseVersion(value);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new InvalidApiVersionException(value, null, ex);
|
||||
}
|
||||
}
|
||||
validateVersion(version, exchange);
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+5
-4
@@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.web.accept.InvalidApiVersionException;
|
||||
import org.springframework.web.accept.MissingApiVersionException;
|
||||
import org.springframework.web.accept.SemanticApiVersionParser;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.web.testfixture.server.MockServerWebExchange;
|
||||
|
||||
@@ -40,8 +39,6 @@ public class DefaultApiVersionStrategiesTests {
|
||||
|
||||
private static final SemanticApiVersionParser parser = new SemanticApiVersionParser();
|
||||
|
||||
private final ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"));
|
||||
|
||||
|
||||
@Test
|
||||
void defaultVersionIsParsed() {
|
||||
@@ -114,8 +111,12 @@ public class DefaultApiVersionStrategiesTests {
|
||||
}
|
||||
|
||||
private void validateVersion(@Nullable String version, DefaultApiVersionStrategy strategy) {
|
||||
MockServerHttpRequest.BaseBuilder<?> requestBuilder = MockServerHttpRequest.get("/");
|
||||
if (version != null) {
|
||||
requestBuilder.queryParam("api-version", version);
|
||||
}
|
||||
Comparable<?> parsedVersion = (version != null ? parser.parseVersion(version) : null);
|
||||
strategy.validateVersion(parsedVersion, exchange);
|
||||
strategy.resolveParseAndValidateVersion(MockServerWebExchange.builder(requestBuilder).build());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user