List all unsatisfied request param groups

Issue: SPR-12854
This commit is contained in:
Rossen Stoyanchev
2015-04-06 23:33:56 -04:00
parent 0b8554f94a
commit b6449baaa6
3 changed files with 79 additions and 21 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -22,6 +22,7 @@ import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
@@ -205,7 +206,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
Set<MediaType> consumableMediaTypes;
Set<MediaType> producibleMediaTypes;
Set<String> paramConditions;
List<String[]> paramConditions;
if (patternAndMethodMatches.isEmpty()) {
consumableMediaTypes = getConsumableMediaTypes(request, patternMatches);
@@ -234,8 +235,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
throw new HttpMediaTypeNotAcceptableException(new ArrayList<MediaType>(producibleMediaTypes));
}
else if (!CollectionUtils.isEmpty(paramConditions)) {
String[] params = paramConditions.toArray(new String[paramConditions.size()]);
throw new UnsatisfiedServletRequestParameterException(params, request.getParameterMap());
throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap());
}
else {
return null;
@@ -262,18 +262,21 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
return result;
}
private Set<String> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) {
private List<String[]> getRequestParams(HttpServletRequest request, Set<RequestMappingInfo> partialMatches) {
List<String[]> result = new ArrayList<String[]>();
for (RequestMappingInfo partialMatch : partialMatches) {
ParamsRequestCondition condition = partialMatch.getParamsCondition();
if (!CollectionUtils.isEmpty(condition.getExpressions()) && (condition.getMatchingCondition(request) == null)) {
Set<String> expressions = new HashSet<String>();
for (NameValueExpression<String> expr : condition.getExpressions()) {
expressions.add(expr.toString());
Set<NameValueExpression<String>> expressions = condition.getExpressions();
if (!CollectionUtils.isEmpty(expressions) && condition.getMatchingCondition(request) == null) {
int i = 0;
String[] array = new String[expressions.size()];
for (NameValueExpression<String> expression : expressions) {
array[i++] = expression.toString();
}
return expressions;
result.add(array);
}
}
return null;
return result;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,12 +16,17 @@
package org.springframework.web.servlet.mvc.method;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.junit.Before;
@@ -54,8 +59,6 @@ import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.util.UrlPathHelper;
import static org.junit.Assert.*;
/**
* Test fixture with {@link RequestMappingInfoHandlerMapping}.
*
@@ -211,6 +214,8 @@ public class RequestMappingInfoHandlerMappingTests {
}
}
// SPR-12854
@Test
public void testUnsatisfiedServletRequestParameterException() throws Exception {
try {
@@ -219,8 +224,10 @@ public class RequestMappingInfoHandlerMappingTests {
fail("UnsatisfiedServletRequestParameterException expected");
}
catch (UnsatisfiedServletRequestParameterException ex) {
assertArrayEquals("Invalid request parameter conditions",
new String[] { "foo=bar" }, ex.getParamConditions());
List<String[]> groups = ex.getParamConditionGroups();
assertEquals(2, groups.size());
assertThat(Arrays.asList("foo=bar", "bar=baz"),
containsInAnyOrder(groups.get(0)[0], groups.get(1)[0]));
}
}
@@ -408,6 +415,7 @@ public class RequestMappingInfoHandlerMappingTests {
}
@SuppressWarnings("unused")
@Controller
private static class TestController {
@@ -441,6 +449,11 @@ public class RequestMappingInfoHandlerMappingTests {
return "";
}
@RequestMapping(value = "/params", params="bar=baz")
public String param2() {
return "";
}
@RequestMapping(value = "/content", produces="application/xml")
public String xmlContent() {
return "";
@@ -452,6 +465,7 @@ public class RequestMappingInfoHandlerMappingTests {
}
}
@SuppressWarnings("unused")
@Controller
private static class UserController {