Browse Source

Prefer mapping without version for unversioned request

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

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

@ -128,7 +128,9 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers @@ -128,7 +128,9 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
return (-1 * compareVersions(this.version, otherVersion));
}
else {
return (this.version != null ? -1 : 1);
// Prefer mapping without version for unversioned request
Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
return (version != null ? (this.version != null ? -1 : 1) : (this.version != null ? 1 : -1));
}
}

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

@ -18,6 +18,7 @@ package org.springframework.web.reactive.result.condition; @@ -18,6 +18,7 @@ package org.springframework.web.reactive.result.condition;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
@ -159,6 +160,15 @@ public class VersionRequestConditionTests { @@ -159,6 +160,15 @@ public class VersionRequestConditionTests {
condition.handleMatch(exchange);
}
@Test
void compareWithoutRequestVersion() {
VersionRequestCondition condition = Stream.of(condition("1.1"), condition("1.2"), emptyCondition())
.min((c1, c2) -> c1.compareTo(c2, exchange()))
.get();
assertThat(condition).isEqualTo(emptyCondition());
}
private VersionRequestCondition condition(String v) {
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
return new VersionRequestCondition(v, this.strategy);

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

@ -127,7 +127,9 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers @@ -127,7 +127,9 @@ public final class VersionRequestCondition extends AbstractRequestCondition<Vers
return (-1 * compareVersions(this.version, otherVersion));
}
else {
return (this.version != null ? -1 : 1);
// Prefer mapping without version for unversioned request
Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
return (version != null ? (this.version != null ? -1 : 1) : (this.version != null ? 1 : -1));
}
}

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

@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.condition; @@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.condition;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
@ -146,6 +147,15 @@ public class VersionRequestConditionTests { @@ -146,6 +147,15 @@ public class VersionRequestConditionTests {
assertThat(list.get(0)).isEqualTo(condition(expected));
}
@Test
void compareWithoutRequestVersion() {
VersionRequestCondition condition = Stream.of(condition("1.1"), condition("1.2"), emptyCondition())
.min((c1, c2) -> c1.compareTo(c2, new MockHttpServletRequest()))
.get();
assertThat(condition).isEqualTo(emptyCondition());
}
@Test // gh-35236
void noRequestVersion() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");

Loading…
Cancel
Save