mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Avoid IllegalStateException for unversioned request
Closes gh-35236
This commit is contained in:
+1
-2
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -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);
|
||||
|
||||
+1
-2
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user