Avoid IllegalStateException for unversioned request

Closes gh-35236
This commit is contained in:
rstoyanchev
2025-07-28 08:47:57 +01:00
parent c7fbf7809f
commit 48506db996
4 changed files with 24 additions and 4 deletions
@@ -149,8 +149,7 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
public void handleMatch(ServerWebExchange exchange) {
if (this.version != null && !this.baselineVersion) {
Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
Assert.state(version != null, "No API version attribute");
if (!this.version.equals(version)) {
if (version != null && !this.version.equals(version)) {
throw new NotAcceptableApiVersionException(version.toString());
}
}
@@ -148,6 +148,17 @@ public class VersionRequestConditionTests {
assertThat(list.get(0)).isEqualTo(condition(expected));
}
@Test // gh-35236
void noRequestVersion() {
MockServerWebExchange exchange = exchange();
VersionRequestCondition condition = condition("1.1");
VersionRequestCondition match = condition.getMatchingCondition(exchange);
assertThat(match).isSameAs(condition);
condition.handleMatch(exchange);
}
private VersionRequestCondition condition(String v) {
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
return new VersionRequestCondition(v, this.strategy);
@@ -148,8 +148,7 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
public void handleMatch(HttpServletRequest request) {
if (this.version != null && !this.baselineVersion) {
Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
Assert.state(version != null, "No API version attribute");
if (!this.version.equals(version)) {
if (version != null && !this.version.equals(version)) {
throw new NotAcceptableApiVersionException(version.toString());
}
}
@@ -146,6 +146,17 @@ public class VersionRequestConditionTests {
assertThat(list.get(0)).isEqualTo(condition(expected));
}
@Test // gh-35236
void noRequestVersion() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");
VersionRequestCondition condition = condition("1.1");
VersionRequestCondition match = condition.getMatchingCondition(request);
assertThat(match).isSameAs(condition);
condition.handleMatch(request);
}
private VersionRequestCondition condition(String v) {
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
return new VersionRequestCondition(v, this.strategy);