Skip to content

Commit 0eec1dc

Browse files
committed
Consistently validate API version
Closes gh-35082
1 parent a0f9872 commit 0eec1dc

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/VersionRequestCondition.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ public VersionRequestCondition combine(VersionRequestCondition other) {
105105

106106
@Override
107107
public @Nullable VersionRequestCondition getMatchingCondition(ServerWebExchange exchange) {
108-
if (this.version == null) {
109-
return this;
110-
}
111-
112108
Comparable<?> requestVersion = exchange.getAttribute(VERSION_ATTRIBUTE_NAME);
113109
if (requestVersion == null) {
114110
String value = this.versionStrategy.resolveVersion(exchange);
@@ -118,7 +114,7 @@ public VersionRequestCondition combine(VersionRequestCondition other) {
118114
exchange.getAttributes().put(VERSION_ATTRIBUTE_NAME, (requestVersion));
119115
}
120116

121-
if (requestVersion == NO_VERSION_ATTRIBUTE) {
117+
if (this.version == null || requestVersion == NO_VERSION_ATTRIBUTE) {
122118
return this;
123119
}
124120

spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/VersionRequestConditionTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
2525

26+
import org.springframework.web.accept.InvalidApiVersionException;
27+
import org.springframework.web.accept.MissingApiVersionException;
2628
import org.springframework.web.accept.NotAcceptableApiVersionException;
2729
import org.springframework.web.accept.SemanticApiVersionParser;
2830
import org.springframework.web.reactive.accept.DefaultApiVersionStrategy;
@@ -90,6 +92,21 @@ void baselineVersionMatch() {
9092
testMatch("v1.3", condition, true, false);
9193
}
9294

95+
@Test
96+
void notVersionedMatch() {
97+
VersionRequestCondition condition = new VersionRequestCondition(null, this.strategy);
98+
this.strategy.addSupportedVersion("1.1", "1.3");
99+
100+
testMatch("v1.1", condition, true, false);
101+
testMatch("v1.3", condition, true, false);
102+
103+
assertThatThrownBy(() -> condition.getMatchingCondition(exchangeWithVersion("1.2")))
104+
.isInstanceOf(InvalidApiVersionException.class);
105+
106+
assertThatThrownBy(() -> condition.getMatchingCondition(MockServerWebExchange.from(MockServerHttpRequest.get("/"))))
107+
.isInstanceOf(MissingApiVersionException.class);
108+
}
109+
93110
private void testMatch(
94111
String requestVersion, VersionRequestCondition condition, boolean matches, boolean notAcceptable) {
95112

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/VersionRequestCondition.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.util.StringUtils;
2828
import org.springframework.web.accept.ApiVersionStrategy;
2929
import org.springframework.web.accept.InvalidApiVersionException;
30+
import org.springframework.web.accept.MissingApiVersionException;
3031
import org.springframework.web.accept.NotAcceptableApiVersionException;
3132
import org.springframework.web.bind.annotation.RequestMapping;
3233

@@ -104,10 +105,6 @@ public VersionRequestCondition combine(VersionRequestCondition other) {
104105

105106
@Override
106107
public @Nullable VersionRequestCondition getMatchingCondition(HttpServletRequest request) {
107-
if (this.version == null) {
108-
return this;
109-
}
110-
111108
Comparable<?> requestVersion = (Comparable<?>) request.getAttribute(VERSION_ATTRIBUTE_NAME);
112109
if (requestVersion == null) {
113110
String value = this.versionStrategy.resolveVersion(request);
@@ -117,7 +114,7 @@ public VersionRequestCondition combine(VersionRequestCondition other) {
117114
request.setAttribute(VERSION_ATTRIBUTE_NAME, (requestVersion));
118115
}
119116

120-
if (requestVersion == NO_VERSION_ATTRIBUTE) {
117+
if (this.version == null || requestVersion == NO_VERSION_ATTRIBUTE) {
121118
return this;
122119
}
123120

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/VersionRequestConditionTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.web.accept.DefaultApiVersionStrategy;
27+
import org.springframework.web.accept.InvalidApiVersionException;
28+
import org.springframework.web.accept.MissingApiVersionException;
2729
import org.springframework.web.accept.NotAcceptableApiVersionException;
2830
import org.springframework.web.accept.SemanticApiVersionParser;
2931
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
@@ -88,6 +90,21 @@ void baselineVersionMatch() {
8890
testMatch("v1.3", condition, true, false);
8991
}
9092

93+
@Test
94+
void notVersionedMatch() {
95+
VersionRequestCondition condition = new VersionRequestCondition(null, this.strategy);
96+
this.strategy.addSupportedVersion("1.1", "1.3");
97+
98+
testMatch("v1.1", condition, true, false);
99+
testMatch("v1.3", condition, true, false);
100+
101+
assertThatThrownBy(() -> condition.getMatchingCondition(requestWithVersion("1.2")))
102+
.isInstanceOf(InvalidApiVersionException.class);
103+
104+
assertThatThrownBy(() -> condition.getMatchingCondition(new MockHttpServletRequest("GET", "/path")))
105+
.isInstanceOf(MissingApiVersionException.class);
106+
}
107+
91108
private void testMatch(
92109
String requestVersion, VersionRequestCondition condition, boolean matches, boolean notAcceptable) {
93110

0 commit comments

Comments
 (0)