Skip to content

Commit 051ab05

Browse files
committed
Properly initialize URI/Matrix vars w/ urlDecode=false
Issue: SPR-16867
1 parent 9d36fd0 commit 051ab05

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

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

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -91,14 +91,14 @@ public class RequestMappingInfoHandlerMappingTests {
9191

9292

9393
@Before
94-
public void setup() throws Exception {
94+
public void setup() {
9595
this.handlerMapping = new TestRequestMappingInfoHandlerMapping();
9696
this.handlerMapping.registerHandler(new TestController());
9797
}
9898

9999

100100
@Test
101-
public void getHandlerDirectMatch() throws Exception {
101+
public void getHandlerDirectMatch() {
102102
Method expected = on(TestController.class).annot(getMapping("/foo").params()).resolveMethod();
103103
ServerWebExchange exchange = MockServerWebExchange.from(get("/foo"));
104104
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
@@ -107,7 +107,7 @@ public void getHandlerDirectMatch() throws Exception {
107107
}
108108

109109
@Test
110-
public void getHandlerGlobMatch() throws Exception {
110+
public void getHandlerGlobMatch() {
111111
Method expected = on(TestController.class).annot(requestMapping("/ba*").method(GET, HEAD)).resolveMethod();
112112
ServerWebExchange exchange = MockServerWebExchange.from(get("/bar"));
113113
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
@@ -116,7 +116,7 @@ public void getHandlerGlobMatch() throws Exception {
116116
}
117117

118118
@Test
119-
public void getHandlerEmptyPathMatch() throws Exception {
119+
public void getHandlerEmptyPathMatch() {
120120
Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod();
121121
ServerWebExchange exchange = MockServerWebExchange.from(get(""));
122122
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
@@ -128,7 +128,7 @@ public void getHandlerEmptyPathMatch() throws Exception {
128128
}
129129

130130
@Test
131-
public void getHandlerBestMatch() throws Exception {
131+
public void getHandlerBestMatch() {
132132
Method expected = on(TestController.class).annot(getMapping("/foo").params("p")).resolveMethod();
133133
ServerWebExchange exchange = MockServerWebExchange.from(get("/foo?p=anything"));
134134
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
@@ -137,7 +137,7 @@ public void getHandlerBestMatch() throws Exception {
137137
}
138138

139139
@Test
140-
public void getHandlerRequestMethodNotAllowed() throws Exception {
140+
public void getHandlerRequestMethodNotAllowed() {
141141
ServerWebExchange exchange = MockServerWebExchange.from(post("/bar"));
142142
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
143143

@@ -146,7 +146,7 @@ public void getHandlerRequestMethodNotAllowed() throws Exception {
146146
}
147147

148148
@Test // SPR-9603
149-
public void getHandlerRequestMethodMatchFalsePositive() throws Exception {
149+
public void getHandlerRequestMethodMatchFalsePositive() {
150150
ServerWebExchange exchange = MockServerWebExchange.from(get("/users").accept(MediaType.APPLICATION_XML));
151151
this.handlerMapping.registerHandler(new UserController());
152152
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
@@ -157,14 +157,14 @@ public void getHandlerRequestMethodMatchFalsePositive() throws Exception {
157157
}
158158

159159
@Test // SPR-8462
160-
public void getHandlerMediaTypeNotSupported() throws Exception {
160+
public void getHandlerMediaTypeNotSupported() {
161161
testHttpMediaTypeNotSupportedException("/person/1");
162162
testHttpMediaTypeNotSupportedException("/person/1/");
163163
testHttpMediaTypeNotSupportedException("/person/1.json");
164164
}
165165

166166
@Test
167-
public void getHandlerTestInvalidContentType() throws Exception {
167+
public void getHandlerTestInvalidContentType() {
168168
MockServerHttpRequest request = put("/person/1").header("content-type", "bogus").build();
169169
ServerWebExchange exchange = MockServerWebExchange.from(request);
170170
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
@@ -175,13 +175,13 @@ public void getHandlerTestInvalidContentType() throws Exception {
175175
}
176176

177177
@Test // SPR-8462
178-
public void getHandlerTestMediaTypeNotAcceptable() throws Exception {
178+
public void getHandlerTestMediaTypeNotAcceptable() {
179179
testMediaTypeNotAcceptable("/persons");
180180
testMediaTypeNotAcceptable("/persons/");
181181
}
182182

183183
@Test // SPR-12854
184-
public void getHandlerTestRequestParamMismatch() throws Exception {
184+
public void getHandlerTestRequestParamMismatch() {
185185
ServerWebExchange exchange = MockServerWebExchange.from(get("/params"));
186186
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
187187
assertError(mono, ServerWebInputException.class, ex -> {
@@ -191,7 +191,7 @@ public void getHandlerTestRequestParamMismatch() throws Exception {
191191
}
192192

193193
@Test
194-
public void getHandlerHttpOptions() throws Exception {
194+
public void getHandlerHttpOptions() {
195195
List<HttpMethod> allMethodExceptTrace = new ArrayList<>(Arrays.asList(HttpMethod.values()));
196196
allMethodExceptTrace.remove(HttpMethod.TRACE);
197197

@@ -202,7 +202,7 @@ public void getHandlerHttpOptions() throws Exception {
202202
}
203203

204204
@Test
205-
public void getHandlerProducibleMediaTypesAttribute() throws Exception {
205+
public void getHandlerProducibleMediaTypesAttribute() {
206206
ServerWebExchange exchange = MockServerWebExchange.from(get("/content").accept(MediaType.APPLICATION_XML));
207207
this.handlerMapping.getHandler(exchange).block();
208208

@@ -218,7 +218,7 @@ public void getHandlerProducibleMediaTypesAttribute() throws Exception {
218218

219219
@Test
220220
@SuppressWarnings("unchecked")
221-
public void handleMatchUriTemplateVariables() throws Exception {
221+
public void handleMatchUriTemplateVariables() {
222222
ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2"));
223223

224224
RequestMappingInfo key = paths("/{path1}/{path2}").build();
@@ -233,7 +233,7 @@ public void handleMatchUriTemplateVariables() throws Exception {
233233
}
234234

235235
@Test // SPR-9098
236-
public void handleMatchUriTemplateVariablesDecode() throws Exception {
236+
public void handleMatchUriTemplateVariablesDecode() {
237237
RequestMappingInfo key = paths("/{group}/{identifier}").build();
238238
URI url = URI.create("/group/a%2Fb");
239239
ServerWebExchange exchange = MockServerWebExchange.from(method(HttpMethod.GET, url));
@@ -250,7 +250,7 @@ public void handleMatchUriTemplateVariablesDecode() throws Exception {
250250
}
251251

252252
@Test
253-
public void handleMatchBestMatchingPatternAttribute() throws Exception {
253+
public void handleMatchBestMatchingPatternAttribute() {
254254
RequestMappingInfo key = paths("/{path1}/2", "/**").build();
255255
ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2"));
256256
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
@@ -263,7 +263,7 @@ public void handleMatchBestMatchingPatternAttribute() throws Exception {
263263
}
264264

265265
@Test
266-
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Exception {
266+
public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() {
267267
RequestMappingInfo key = paths().build();
268268
ServerWebExchange exchange = MockServerWebExchange.from(get("/1/2"));
269269
this.handlerMapping.handleMatch(key, handlerMethod, exchange);
@@ -273,7 +273,7 @@ public void handleMatchBestMatchingPatternAttributeNoPatternsDefined() throws Ex
273273
}
274274

275275
@Test
276-
public void handleMatchMatrixVariables() throws Exception {
276+
public void handleMatchMatrixVariables() {
277277
MultiValueMap<String, String> matrixVariables;
278278
Map<String, String> uriVariables;
279279

@@ -290,17 +290,17 @@ public void handleMatchMatrixVariables() throws Exception {
290290
}
291291

292292
@Test
293-
public void handleMatchMatrixVariablesDecoding() throws Exception {
294-
MockServerHttpRequest request = method(HttpMethod.GET, URI.create("/path;mvar=a%2fb")).build();
293+
public void handleMatchMatrixVariablesDecoding() {
294+
MockServerHttpRequest request = method(HttpMethod.GET, URI.create("/cars;mvar=a%2Fb")).build();
295295
ServerWebExchange exchange = MockServerWebExchange.from(request);
296-
handleMatch(exchange, "/{filter}");
296+
handleMatch(exchange, "/{cars}");
297297

298-
MultiValueMap<String, String> matrixVariables = getMatrixVariables(exchange, "filter");
298+
MultiValueMap<String, String> matrixVariables = getMatrixVariables(exchange, "cars");
299299
Map<String, String> uriVariables = getUriTemplateVariables(exchange);
300300

301301
assertNotNull(matrixVariables);
302302
assertEquals(Collections.singletonList("a/b"), matrixVariables.get("mvar"));
303-
assertEquals("path", uriVariables.get("filter"));
303+
assertEquals("cars", uriVariables.get("cars"));
304304
}
305305

306306

@@ -314,7 +314,7 @@ private <T> void assertError(Mono<Object> mono, final Class<T> exceptionClass, f
314314
.verify();
315315
}
316316

317-
private void testHttpMediaTypeNotSupportedException(String url) throws Exception {
317+
private void testHttpMediaTypeNotSupportedException(String url) {
318318
MockServerHttpRequest request = put(url).contentType(MediaType.APPLICATION_JSON).build();
319319
ServerWebExchange exchange = MockServerWebExchange.from(request);
320320
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
@@ -325,7 +325,7 @@ private void testHttpMediaTypeNotSupportedException(String url) throws Exception
325325
ex.getSupportedMediaTypes()));
326326
}
327327

328-
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) throws Exception {
328+
private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods) {
329329
ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.options(requestURI));
330330
HandlerMethod handlerMethod = (HandlerMethod) this.handlerMapping.getHandler(exchange).block();
331331

@@ -342,7 +342,7 @@ private void testHttpOptions(String requestURI, Set<HttpMethod> allowedMethods)
342342
assertEquals(allowedMethods, ((HttpHeaders) value).getAllow());
343343
}
344344

345-
private void testMediaTypeNotAcceptable(String url) throws Exception {
345+
private void testMediaTypeNotAcceptable(String url) {
346346
ServerWebExchange exchange = MockServerWebExchange.from(get(url).accept(MediaType.APPLICATION_JSON));
347347
Mono<Object> mono = this.handlerMapping.getHandler(exchange);
348348

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMapping.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,28 +113,27 @@ protected void handleMatch(RequestMappingInfo info, String lookupPath, HttpServl
113113

114114
String bestPattern;
115115
Map<String, String> uriVariables;
116-
Map<String, String> decodedUriVariables;
117116

118117
Set<String> patterns = info.getPatternsCondition().getPatterns();
119118
if (patterns.isEmpty()) {
120119
bestPattern = lookupPath;
121120
uriVariables = Collections.emptyMap();
122-
decodedUriVariables = Collections.emptyMap();
123121
}
124122
else {
125123
bestPattern = patterns.iterator().next();
126124
uriVariables = getPathMatcher().extractUriTemplateVariables(bestPattern, lookupPath);
127-
decodedUriVariables = getUrlPathHelper().decodePathVariables(request, uriVariables);
128125
}
129126

130127
request.setAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, bestPattern);
131-
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, decodedUriVariables);
132128

133129
if (isMatrixVariableContentAvailable()) {
134130
Map<String, MultiValueMap<String, String>> matrixVars = extractMatrixVariables(request, uriVariables);
135131
request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, matrixVars);
136132
}
137133

134+
Map<String, String> decodedUriVariables = getUrlPathHelper().decodePathVariables(request, uriVariables);
135+
request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, decodedUriVariables);
136+
138137
if (!info.getProducesCondition().getProducibleMediaTypes().isEmpty()) {
139138
Set<MediaType> mediaTypes = info.getProducesCondition().getProducibleMediaTypes();
140139
request.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, mediaTypes);

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/RequestMappingInfoHandlerMappingTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public void handleMatchMatrixVariables() {
345345
assertEquals("", uriVariables.get("params"));
346346
}
347347

348-
@Test
348+
@Test // SPR-10140, SPR-16867
349349
public void handleMatchMatrixVariablesDecoding() {
350350

351351
MockHttpServletRequest request;
@@ -357,14 +357,14 @@ public void handleMatchMatrixVariablesDecoding() {
357357
this.handlerMapping.setUrlPathHelper(urlPathHelper);
358358

359359
request = new MockHttpServletRequest();
360-
handleMatch(request, "/path{filter}", "/path;mvar=a%2fb");
360+
handleMatch(request, "/{cars}", "/cars;mvar=a%2Fb");
361361

362-
MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "filter");
362+
MultiValueMap<String, String> matrixVariables = getMatrixVariables(request, "cars");
363363
Map<String, String> uriVariables = getUriTemplateVariables(request);
364364

365365
assertNotNull(matrixVariables);
366366
assertEquals(Collections.singletonList("a/b"), matrixVariables.get("mvar"));
367-
assertEquals(";mvar=a/b", uriVariables.get("filter"));
367+
assertEquals("cars", uriVariables.get("cars"));
368368
}
369369

370370

0 commit comments

Comments
 (0)