Browse Source

Avoid IllegalStateException for unversioned request

Closes gh-35236
pull/35247/head
rstoyanchev 5 months ago
parent
commit
48506db996
  1. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/VersionRequestCondition.java
  2. 11
      spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/VersionRequestConditionTests.java
  3. 3
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/VersionRequestCondition.java
  4. 11
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/condition/VersionRequestConditionTests.java

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

@ -149,8 +149,7 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
public void handleMatch(ServerWebExchange exchange) { public void handleMatch(ServerWebExchange exchange) {
if (this.version != null && !this.baselineVersion) { if (this.version != null && !this.baselineVersion) {
Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE); Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
Assert.state(version != null, "No API version attribute"); if (version != null && !this.version.equals(version)) {
if (!this.version.equals(version)) {
throw new NotAcceptableApiVersionException(version.toString()); throw new NotAcceptableApiVersionException(version.toString());
} }
} }

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

@ -148,6 +148,17 @@ public class VersionRequestConditionTests {
assertThat(list.get(0)).isEqualTo(condition(expected)); 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) { private VersionRequestCondition condition(String v) {
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v); this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
return new VersionRequestCondition(v, this.strategy); return new VersionRequestCondition(v, this.strategy);

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

@ -148,8 +148,7 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
public void handleMatch(HttpServletRequest request) { public void handleMatch(HttpServletRequest request) {
if (this.version != null && !this.baselineVersion) { if (this.version != null && !this.baselineVersion) {
Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE); Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
Assert.state(version != null, "No API version attribute"); if (version != null && !this.version.equals(version)) {
if (!this.version.equals(version)) {
throw new NotAcceptableApiVersionException(version.toString()); throw new NotAcceptableApiVersionException(version.toString());
} }
} }

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

@ -146,6 +146,17 @@ public class VersionRequestConditionTests {
assertThat(list.get(0)).isEqualTo(condition(expected)); 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) { private VersionRequestCondition condition(String v) {
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v); this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
return new VersionRequestCondition(v, this.strategy); return new VersionRequestCondition(v, this.strategy);

Loading…
Cancel
Save