Skip to content

Commit 1cb9f2c

Browse files
committed
Explicit HEAD sorted higher than implicit one
Same fix as #7cdcc1 but for WebFlux. Issue: SPR-14182
1 parent 72fbbb2 commit 1cb9f2c

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,8 +39,8 @@
3939
*/
4040
public final class RequestMethodsRequestCondition extends AbstractRequestCondition<RequestMethodsRequestCondition> {
4141

42-
private static final RequestMethodsRequestCondition HEAD_CONDITION =
43-
new RequestMethodsRequestCondition(RequestMethod.HEAD);
42+
private static final RequestMethodsRequestCondition GET_CONDITION =
43+
new RequestMethodsRequestCondition(RequestMethod.GET);
4444

4545

4646
private final Set<RequestMethod> methods;
@@ -139,7 +139,7 @@ private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod h
139139
}
140140
}
141141
if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) {
142-
return HEAD_CONDITION;
142+
return GET_CONDITION;
143143
}
144144
}
145145
return null;
@@ -158,7 +158,18 @@ private RequestMethodsRequestCondition matchRequestMethod(@Nullable HttpMethod h
158158
*/
159159
@Override
160160
public int compareTo(RequestMethodsRequestCondition other, ServerWebExchange exchange) {
161-
return (other.methods.size() - this.methods.size());
161+
if (other.methods.size() != this.methods.size()) {
162+
return other.methods.size() - this.methods.size();
163+
}
164+
else if (this.methods.size() == 1) {
165+
if (this.methods.contains(RequestMethod.HEAD) && other.methods.contains(RequestMethod.GET)) {
166+
return -1;
167+
}
168+
else if (this.methods.contains(RequestMethod.GET) && other.methods.contains(RequestMethod.HEAD)) {
169+
return 1;
170+
}
171+
}
172+
return 0;
162173
}
163174

164175
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@ public void getMatchingCondition() throws Exception {
5151
@Test
5252
public void getMatchingConditionWithHttpHead() throws Exception {
5353
testMatch(new RequestMethodsRequestCondition(HEAD), HEAD);
54-
testMatch(new RequestMethodsRequestCondition(GET), HEAD);
54+
testMatch(new RequestMethodsRequestCondition(GET), GET);
5555
testNoMatch(new RequestMethodsRequestCondition(POST), HEAD);
5656
}
5757

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@
3131
import org.springframework.http.ResponseEntity;
3232
import org.springframework.http.server.reactive.ServerHttpRequest;
3333
import org.springframework.web.bind.annotation.GetMapping;
34+
import org.springframework.web.bind.annotation.RequestMapping;
35+
import org.springframework.web.bind.annotation.RequestMethod;
3436
import org.springframework.web.bind.annotation.RestController;
3537
import org.springframework.web.reactive.config.EnableWebFlux;
3638
import org.springframework.web.server.adapter.ForwardedHeaderTransformer;
@@ -100,10 +102,16 @@ static class WebConfig {
100102
private static class TestRestController {
101103

102104
@GetMapping("/text")
103-
public String text() {
105+
public String textGet() {
104106
return "Foo";
105107
}
106108

109+
// SPR-17593: explicit HEAD should not clash with implicit mapping via GET
110+
@RequestMapping(path = "/text", method = RequestMethod.HEAD)
111+
public String textHead() {
112+
return textGet();
113+
}
114+
107115
@GetMapping("/uri")
108116
public String uri(ServerHttpRequest request) {
109117
return request.getURI().toString();

0 commit comments

Comments
 (0)