Skip to content

Commit 48506db

Browse files
committed
Avoid IllegalStateException for unversioned request
Closes gh-35236
1 parent c7fbf78 commit 48506db

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ else if (this.version != null && otherVersion != null) {
149149
public void handleMatch(ServerWebExchange exchange) {
150150
if (this.version != null && !this.baselineVersion) {
151151
Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
152-
Assert.state(version != null, "No API version attribute");
153-
if (!this.version.equals(version)) {
152+
if (version != null && !this.version.equals(version)) {
154153
throw new NotAcceptableApiVersionException(version.toString());
155154
}
156155
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ private void testCompare(String expected, String... versions) {
148148
assertThat(list.get(0)).isEqualTo(condition(expected));
149149
}
150150

151+
@Test // gh-35236
152+
void noRequestVersion() {
153+
MockServerWebExchange exchange = exchange();
154+
VersionRequestCondition condition = condition("1.1");
155+
156+
VersionRequestCondition match = condition.getMatchingCondition(exchange);
157+
assertThat(match).isSameAs(condition);
158+
159+
condition.handleMatch(exchange);
160+
}
161+
151162
private VersionRequestCondition condition(String v) {
152163
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
153164
return new VersionRequestCondition(v, this.strategy);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ else if (this.version != null && otherVersion != null) {
148148
public void handleMatch(HttpServletRequest request) {
149149
if (this.version != null && !this.baselineVersion) {
150150
Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
151-
Assert.state(version != null, "No API version attribute");
152-
if (!this.version.equals(version)) {
151+
if (version != null && !this.version.equals(version)) {
153152
throw new NotAcceptableApiVersionException(version.toString());
154153
}
155154
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ private void testCompare(String expected, String... versions) {
146146
assertThat(list.get(0)).isEqualTo(condition(expected));
147147
}
148148

149+
@Test // gh-35236
150+
void noRequestVersion() {
151+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
152+
VersionRequestCondition condition = condition("1.1");
153+
154+
VersionRequestCondition match = condition.getMatchingCondition(request);
155+
assertThat(match).isSameAs(condition);
156+
157+
condition.handleMatch(request);
158+
}
159+
149160
private VersionRequestCondition condition(String v) {
150161
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
151162
return new VersionRequestCondition(v, this.strategy);

0 commit comments

Comments
 (0)