From f5aa01172285473f8f676bf2ed8af9edb0a96e41 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 29 Jan 2016 15:32:42 -0500 Subject: [PATCH] UriComponents support for array query params Issue: SPR-9712 --- .../web/util/HierarchicalUriComponents.java | 21 ++++++++++++++++++- .../web/util/UriTemplateTests.java | 12 ++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index 7966e32fec8..811dfc1e6c6 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -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 { private MultiValueMap expandQueryParams(UriTemplateVariables variables) { int size = this.queryParams.size(); MultiValueMap result = new LinkedMultiValueMap(size); + variables = new QueryUriTemplateVariables(variables); for (Map.Entry> entry : this.queryParams.entrySet()) { String name = expandUriComponent(entry.getKey(), variables); List values = new ArrayList(entry.getValue().size()); @@ -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; + } + } + } diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index a047af03ebe..7af5550da07 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -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.*; /** * @author Arjen Poutsma * @author Juergen Hoeller + * @author Rossen Stoyanchev */ 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}");