Properly handle single-value map with @MatrixVariable

Issue: SPR-14294
This commit is contained in:
Rossen Stoyanchev 2016-05-22 09:53:42 -04:00
parent 400206a2fe
commit c2625f968c
3 changed files with 31 additions and 15 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -17,9 +17,11 @@
package org.springframework.web.servlet.mvc.method.annotation;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@ -68,23 +70,37 @@ public class MatrixVariableMapMethodArgumentResolver implements HandlerMethodArg
return Collections.emptyMap();
}
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
String pathVariable = parameter.getParameterAnnotation(MatrixVariable.class).pathVar();
if (!pathVariable.equals(ValueConstants.DEFAULT_NONE)) {
MultiValueMap<String, String> map = matrixVariables.get(pathVariable);
return (map != null) ? map : Collections.emptyMap();
MultiValueMap<String, String> mapForPathVariable = matrixVariables.get(pathVariable);
if (mapForPathVariable == null) {
return Collections.emptyMap();
}
map.putAll(mapForPathVariable);
}
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
for (MultiValueMap<String, String> vars : matrixVariables.values()) {
for (String name : vars.keySet()) {
for (String value : vars.get(name)) {
map.add(name, value);
else {
for (MultiValueMap<String, String> vars : matrixVariables.values()) {
for (String name : vars.keySet()) {
for (String value : vars.get(name)) {
map.add(name, value);
}
}
}
}
return map;
return (isSingleValueMap(parameter) ? map.toSingleValueMap() : map);
}
private boolean isSingleValueMap(MethodParameter parameter) {
if (!MultiValueMap.class.isAssignableFrom(parameter.getParameterType())) {
ResolvableType[] genericTypes = ResolvableType.forMethodParameter(parameter).getGenerics();
if (genericTypes.length == 2) {
return !List.class.isAssignableFrom(genericTypes[1].getRawClass());
}
}
return false;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -102,7 +102,7 @@ public class MatrixVariablesMapMethodArgumentResolverTests {
Map<String, String> map = (Map<String, String>) this.resolver.resolveArgument(
this.paramMap, this.mavContainer, this.webRequest, null);
assertEquals(Arrays.asList("red", "green", "blue"), map.get("colors"));
assertEquals("red", map.get("colors"));
@SuppressWarnings("unchecked")
MultiValueMap<String, String> multivalueMap = (MultiValueMap<String, String>) this.resolver.resolveArgument(
@ -131,7 +131,7 @@ public class MatrixVariablesMapMethodArgumentResolverTests {
Map<String, String> mapAll = (Map<String, String>) this.resolver.resolveArgument(
this.paramMap, this.mavContainer, this.webRequest, null);
assertEquals(Arrays.asList("red", "purple", "yellow", "orange"), mapAll.get("colors"));
assertEquals("red", mapAll.get("colors"));
}
@Test

View File

@ -1026,8 +1026,8 @@ All matrix variables may be obtained in a Map:
@RequestMapping(path = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
@MatrixVariable Map<String, String> matrixVars,
@MatrixVariable(pathVar="petId"") Map<String, String> petMatrixVars) {
@MatrixVariable MultiValueMap<String, String> matrixVars,
@MatrixVariable(pathVar="petId"") MultiValueMap<String, String> petMatrixVars) {
// matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
// petMatrixVars: ["q" : 11, "s" : 23]