Browse Source

UriComponents support for array query params

Issue: SPR-9712
pull/959/head
Rossen Stoyanchev 9 years ago
parent
commit
f5aa011722
  1. 21
      spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java
  2. 12
      spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

21
spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

@ -1,5 +1,5 @@ @@ -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.
@ -336,6 +336,7 @@ final class HierarchicalUriComponents extends UriComponents { @@ -336,6 +336,7 @@ final class HierarchicalUriComponents extends UriComponents {
private MultiValueMap<String, String> expandQueryParams(UriTemplateVariables variables) {
int size = this.queryParams.size();
MultiValueMap<String, String> result = new LinkedMultiValueMap<String, String>(size);
variables = new QueryUriTemplateVariables(variables);
for (Map.Entry<String, List<String>> entry : this.queryParams.entrySet()) {
String name = expandUriComponent(entry.getKey(), variables);
List<String> values = new ArrayList<String>(entry.getValue().size());
@ -882,4 +883,22 @@ final class HierarchicalUriComponents extends UriComponents { @@ -882,4 +883,22 @@ final class HierarchicalUriComponents extends UriComponents {
}
};
private static class QueryUriTemplateVariables implements UriTemplateVariables {
private final UriTemplateVariables delegate;
public QueryUriTemplateVariables(UriTemplateVariables delegate) {
this.delegate = delegate;
}
@Override
public Object getValue(String name) {
Object value = this.delegate.getValue(name);
if (ObjectUtils.isArray(value)) {
value = StringUtils.arrayToCommaDelimitedString(ObjectUtils.toObjectArray(value));
}
return value;
}
}
}

12
spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

@ -1,5 +1,5 @@ @@ -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.
@ -30,6 +30,7 @@ import static org.junit.Assert.*; @@ -30,6 +30,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Rossen Stoyanchev
*/
public class UriTemplateTests {
@ -47,6 +48,15 @@ public class UriTemplateTests { @@ -47,6 +48,15 @@ public class UriTemplateTests {
assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
}
// SPR-9712
@Test @SuppressWarnings("PrimitiveArrayArgumentToVariableArgMethod")
public void expandVarArgsWithArrayValue() throws Exception {
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
URI result = template.expand(new int[] {1, 2, 3});
assertEquals(new URI("/sum?numbers=1,2,3"), result);
}
@Test(expected = IllegalArgumentException.class)
public void expandVarArgsNotEnoughVariables() throws Exception {
UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");

Loading…
Cancel
Save