diff --git a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java index cdea9240..65603ce2 100644 --- a/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java +++ b/src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-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. @@ -15,12 +15,26 @@ */ package org.springframework.hateoas.mvc; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + import org.springframework.core.MethodParameter; import org.springframework.hateoas.Link; import org.springframework.hateoas.MethodLinkBuilderFactory; -import org.springframework.hateoas.core.*; +import org.springframework.hateoas.core.AnnotationAttribute; +import org.springframework.hateoas.core.AnnotationMappingDiscoverer; import org.springframework.hateoas.core.DummyInvocationUtils.LastInvocationAware; import org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation; +import org.springframework.hateoas.core.LinkBuilderSupport; +import org.springframework.hateoas.core.MappingDiscoverer; +import org.springframework.hateoas.core.MethodParameters; import org.springframework.hateoas.mvc.AnnotatedParametersParameterAccessor.BoundMethodParameter; import org.springframework.util.Assert; import org.springframework.util.MultiValueMap; @@ -31,9 +45,6 @@ import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriTemplate; -import java.lang.reflect.Method; -import java.util.*; - /** * Factory for {@link LinkBuilderSupport} instances based on the request mapping annotated on the given controller. * @@ -41,6 +52,7 @@ import java.util.*; * @author Oliver Gierke * @author Dietrich Schulten * @author Kamill Sokol + * @author Ross Turner */ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory { @@ -119,29 +131,7 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory) { - MultiValueMap requestParams = (MultiValueMap)value; - for (Map.Entry> multiValueEntry : requestParams.entrySet()) { - for (String singleEntryValue : multiValueEntry.getValue()) { - builder.queryParam(multiValueEntry.getKey(), singleEntryValue); - } - } - } else if (value instanceof Map) { - Map requestParams = (Map)value; - for (Map.Entry requestParamEntry : requestParams.entrySet()) { - builder.queryParam(requestParamEntry.getKey(), requestParamEntry.getValue()); - } - } else if (value instanceof Collection) { - for (Object element : (Collection) value) { - builder.queryParam(key, element); - } - } else { - builder.queryParam(key, parameter.asString()); - } + bindRequestParameters(builder, parameter); } UriComponents components = applyUriComponentsContributer(builder, invocation).buildAndExpand(values); @@ -181,6 +171,48 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory requestParams = (MultiValueMap) value; + + for (Map.Entry> multiValueEntry : requestParams.entrySet()) { + for (String singleEntryValue : multiValueEntry.getValue()) { + builder.queryParam(multiValueEntry.getKey(), singleEntryValue); + } + } + + } else if (value instanceof Map) { + + Map requestParams = (Map) value; + + for (Map.Entry requestParamEntry : requestParams.entrySet()) { + builder.queryParam(requestParamEntry.getKey(), requestParamEntry.getValue()); + } + + } else if (value instanceof Collection) { + + for (Object element : (Collection) value) { + builder.queryParam(key, element); + } + + } else { + builder.queryParam(key, parameter.asString()); + } + } + /** * Custom extension of {@link AnnotatedParametersParameterAccessor} for {@link RequestParam} to allow {@literal null} * values handed in for optional request parameters. diff --git a/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java b/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java index 031d0b79..c8f11bcf 100644 --- a/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java +++ b/src/test/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactoryUnitTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-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. @@ -15,6 +15,14 @@ */ package org.springframework.hateoas.mvc; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*; + +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + import org.joda.time.DateTime; import org.joda.time.format.ISODateTimeFormat; import org.junit.Test; @@ -34,22 +42,13 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.util.UriComponentsBuilder; -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.Map; - -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; -import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; - /** * Unit tests for {@link ControllerLinkBuilderFactory}. * * @author Ricardo Gladwell * @author Oliver Gierke * @author Kamill Sokol + * @author Ross Turner */ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { @@ -131,6 +130,7 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { */ @Test public void createsLinkToControllerMethodWithMapRequestParam() { + Map queryParams = new LinkedHashMap(); queryParams.put("firstKey", "firstValue"); queryParams.put("secondKey", "secondValue"); @@ -142,8 +142,12 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { assertThat(link.getHref(), endsWith("/sample/mapsupport?firstKey=firstValue&secondKey=secondValue")); } + /** + * @see #209 + */ @Test public void createsLinkToControllerMethodWithMultiValueMapRequestParam() { + MultiValueMap queryParams = new LinkedMultiValueMap(); queryParams.put("key1", Arrays.asList("value1a", "value1b")); queryParams.put("key2", Arrays.asList("value2a", "value2b")); @@ -152,7 +156,8 @@ public class ControllerLinkBuilderFactoryUnitTest extends TestUtils { assertPointsToMockServer(link); assertThat(link.getRel(), is(Link.REL_SELF)); - assertThat(link.getHref(), endsWith("/sample/multivaluemapsupport?key1=value1a&key1=value1b&key2=value2a&key2=value2b")); + assertThat(link.getHref(), + endsWith("/sample/multivaluemapsupport?key1=value1a&key1=value1b&key2=value2a&key2=value2b")); } static interface SampleController {