Skip to content

Commit

Permalink
handle iterable query parameters (fixes Mercateo#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed Jan 9, 2018
1 parent 2b31fac commit 000dd79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Expand Up @@ -182,7 +182,12 @@ private void setQueryParameters(final UriBuilder uriBuilder, Scope scope, Object
.getInvokedMethod(), scope.getInvokedClass());
visitAnnotations((parameter, parameterIndex, annotation) -> {
if (annotation instanceof QueryParam && parameter != null) {
uriBuilder.queryParam(((QueryParam) annotation).value(), parameter.toString());
final String parameterName = ((QueryParam) annotation).value();
if (parameter instanceof Iterable) {
uriBuilder.queryParam(parameterName, Iterables.toArray((Iterable) parameter, Object.class));
} else {
uriBuilder.queryParam(parameterName, parameter.toString());
}
} else if (annotation instanceof BeanParam && parameter != null) {
if (realParamTypes[parameterIndex] instanceof Class<?>) {
BeanParamExtractor beanParamExtractor = new BeanParamExtractor();
Expand Down
@@ -1,5 +1,6 @@
package com.mercateo.common.rest.schemagen;

import java.util.List;
import java.util.Optional;

import javax.annotation.security.RolesAllowed;
Expand Down Expand Up @@ -86,6 +87,10 @@ public String getTwoIds(@PathParam("id") String id, @PathParam("id2") String id2
public void noHttpMethod() {
}

@GET
public void multipleQueryParameters(@QueryParam("test") List<String> parameters) {
}

@Path("parentResource")
public static class ParentResourceClass implements JerseyResource {

Expand Down
Expand Up @@ -20,6 +20,7 @@
import javax.ws.rs.core.SecurityContext;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -196,6 +197,22 @@ public void testCorrectLinkGenerationWithTwoScopeLevels() {
assertThat(link.getParams()).containsEntry("rel", "self").containsEntry("method", "GET");
}

@Test
public void shouldHandleListOfQueryParameter() {
linkMetaFactory = LinkMetaFactory.create(new RestJsonSchemaGenerator(), linkFactoryContext);

final LinkFactory<ResourceClass> linkFactory = linkMetaFactory.createFactoryFor(ResourceClass.class);
final Optional<Link> linkOption = linkFactory.forCall(Relation.of("multi-param"), r -> r
.multipleQueryParameters(Arrays.asList("foo", "bar", "baz")));

assertThat(linkOption).isPresent();

final Link link = linkOption.get();

assertThat(link.getUri().getPath()).isEqualTo("basePath/resource");
assertThat(link.getUri().getQuery()).isEqualTo("test=foo&test=bar&test=baz");
}

private void allowRole(String roleName) {
when(securityContext.isUserInRole(roleName)).thenReturn(true);
}
Expand Down

0 comments on commit 000dd79

Please sign in to comment.