Skip to content

Commit 2238121

Browse files
committed
Prefer mapping without version for unversioned request
Closes gh-35237
1 parent 48506db commit 2238121

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ else if (this.version != null && otherVersion != null) {
128128
return (-1 * compareVersions(this.version, otherVersion));
129129
}
130130
else {
131-
return (this.version != null ? -1 : 1);
131+
// Prefer mapping without version for unversioned request
132+
Comparable<?> version = exchange.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
133+
return (version != null ? (this.version != null ? -1 : 1) : (this.version != null ? 1 : -1));
132134
}
133135
}
134136

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.stream.Stream;
2122

2223
import org.jspecify.annotations.Nullable;
2324
import org.junit.jupiter.api.BeforeEach;
@@ -159,6 +160,15 @@ void noRequestVersion() {
159160
condition.handleMatch(exchange);
160161
}
161162

163+
@Test
164+
void compareWithoutRequestVersion() {
165+
VersionRequestCondition condition = Stream.of(condition("1.1"), condition("1.2"), emptyCondition())
166+
.min((c1, c2) -> c1.compareTo(c2, exchange()))
167+
.get();
168+
169+
assertThat(condition).isEqualTo(emptyCondition());
170+
}
171+
162172
private VersionRequestCondition condition(String v) {
163173
this.strategy.addSupportedVersion(v.endsWith("+") ? v.substring(0, v.length() - 1) : v);
164174
return new VersionRequestCondition(v, this.strategy);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ else if (this.version != null && otherVersion != null) {
127127
return (-1 * compareVersions(this.version, otherVersion));
128128
}
129129
else {
130-
return (this.version != null ? -1 : 1);
130+
// Prefer mapping without version for unversioned request
131+
Comparable<?> version = (Comparable<?>) request.getAttribute(HandlerMapping.API_VERSION_ATTRIBUTE);
132+
return (version != null ? (this.version != null ? -1 : 1) : (this.version != null ? 1 : -1));
131133
}
132134
}
133135

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.stream.Stream;
2122

2223
import org.jspecify.annotations.Nullable;
2324
import org.junit.jupiter.api.BeforeEach;
@@ -146,6 +147,15 @@ private void testCompare(String expected, String... versions) {
146147
assertThat(list.get(0)).isEqualTo(condition(expected));
147148
}
148149

150+
@Test
151+
void compareWithoutRequestVersion() {
152+
VersionRequestCondition condition = Stream.of(condition("1.1"), condition("1.2"), emptyCondition())
153+
.min((c1, c2) -> c1.compareTo(c2, new MockHttpServletRequest()))
154+
.get();
155+
156+
assertThat(condition).isEqualTo(emptyCondition());
157+
}
158+
149159
@Test // gh-35236
150160
void noRequestVersion() {
151161
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/path");

0 commit comments

Comments
 (0)