From 1cb9f2c7b2a25daae014349ba3df7823b3584171 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 2 Jan 2019 09:23:05 -0500 Subject: [PATCH] Explicit HEAD sorted higher than implicit one Same fix as #7cdcc1 but for WebFlux. Issue: SPR-14182 --- .../RequestMethodsRequestCondition.java | 21 ++++++++++++++----- .../RequestMethodsRequestConditionTests.java | 4 ++-- .../RequestMappingIntegrationTests.java | 12 +++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java index 0131d8393a4..69fd7593cc6 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,8 +39,8 @@ import org.springframework.web.server.ServerWebExchange; */ public final class RequestMethodsRequestCondition extends AbstractRequestCondition { - private static final RequestMethodsRequestCondition HEAD_CONDITION = - new RequestMethodsRequestCondition(RequestMethod.HEAD); + private static final RequestMethodsRequestCondition GET_CONDITION = + new RequestMethodsRequestCondition(RequestMethod.GET); private final Set methods; @@ -139,7 +139,7 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi } } if (httpMethod == HttpMethod.HEAD && getMethods().contains(RequestMethod.GET)) { - return HEAD_CONDITION; + return GET_CONDITION; } } return null; @@ -158,7 +158,18 @@ public final class RequestMethodsRequestCondition extends AbstractRequestConditi */ @Override public int compareTo(RequestMethodsRequestCondition other, ServerWebExchange exchange) { - return (other.methods.size() - this.methods.size()); + if (other.methods.size() != this.methods.size()) { + return other.methods.size() - this.methods.size(); + } + else if (this.methods.size() == 1) { + if (this.methods.contains(RequestMethod.HEAD) && other.methods.contains(RequestMethod.GET)) { + return -1; + } + else if (this.methods.contains(RequestMethod.GET) && other.methods.contains(RequestMethod.HEAD)) { + return 1; + } + } + return 0; } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java index 2cacbb5d2cd..7a29d32e6b7 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestConditionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,7 +51,7 @@ public class RequestMethodsRequestConditionTests { @Test public void getMatchingConditionWithHttpHead() throws Exception { testMatch(new RequestMethodsRequestCondition(HEAD), HEAD); - testMatch(new RequestMethodsRequestCondition(GET), HEAD); + testMatch(new RequestMethodsRequestCondition(GET), GET); testNoMatch(new RequestMethodsRequestCondition(POST), HEAD); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java index 58ae4d73612..72f45030d78 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,8 @@ import org.springframework.http.RequestEntity; import org.springframework.http.ResponseEntity; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.config.EnableWebFlux; import org.springframework.web.server.adapter.ForwardedHeaderTransformer; @@ -100,10 +102,16 @@ public class RequestMappingIntegrationTests extends AbstractRequestMappingIntegr private static class TestRestController { @GetMapping("/text") - public String text() { + public String textGet() { return "Foo"; } + // SPR-17593: explicit HEAD should not clash with implicit mapping via GET + @RequestMapping(path = "/text", method = RequestMethod.HEAD) + public String textHead() { + return textGet(); + } + @GetMapping("/uri") public String uri(ServerHttpRequest request) { return request.getURI().toString();