Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ParamConverterProvider for array support #4684

Merged
merged 3 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this test in this package? Assumingly it should be next to similar tests, in tests/e2e-server perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. By default I was adding tests in JDK connector.

* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.jdk.connector.internal;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jdk.connector.JdkConnectorProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ArrayParamConverterTest extends JerseyTest {

private static final String PARAM_NAME = "paramName";

@Path("/test")
public static class ArraysResource {

@Path("/queryStrArray")
@GET
public Response queryStrArray(@QueryParam(PARAM_NAME) String[] data) {
return Response.ok(String.join("", data)).build();
}

@Path("/queryStrList")
@GET
public Response queryStrList(@QueryParam(PARAM_NAME) List<String> data) {
return Response.ok(String.join("", data)).build();
}

@Path("/queryIntArray")
@GET
public Response queryIntArray(@QueryParam(PARAM_NAME) Integer[] data) {
StringBuilder sb = new StringBuilder(data.length);
for (int d : data) {
sb.append(d);
}
return Response.ok(sb.toString()).build();
}

@Path("/queryIntList")
@GET
public Response queryIntList(@QueryParam(PARAM_NAME) List<Integer> data) {
StringBuilder sb = new StringBuilder(data.size());
for (int d : data) {
sb.append(d);
}
return Response.ok(sb.toString()).build();
}
}

@Override
protected Application configure() {
return new ResourceConfig(ArraysResource.class);
}

@Override
protected void configureClient(ClientConfig config) {
config.connectorProvider(new JdkConnectorProvider());
}

@Test
public void queryStr() {
Response expected = target("/test/queryStrList").queryParam(PARAM_NAME, "1", "2").request().get();
Response arrayResponse = target("/test/queryIntArray").queryParam(PARAM_NAME, "1", "2").request().get();
verifyStr(expected, arrayResponse);
}

@Test
public void queryInt() {
Response expected = target("/test/queryIntList").queryParam(PARAM_NAME, 1, 2).request().get();
Response arrayResponse = target("/test/queryIntArray").queryParam(PARAM_NAME, 1, 2).request().get();
verifyStr(expected, arrayResponse);
}

private void verifyStr(Response expected, Response arrayResponse) {
assertEquals(200, expected.getStatus());
assertEquals(200, arrayResponse.getStatus());
String expectedStr = expected.readEntity(String.class);
String arrayStr = arrayResponse.readEntity(String.class);
// Check the result is the same
assertEquals(expectedStr, arrayStr);
assertEquals("12", arrayStr);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.server.internal.inject;

import java.lang.reflect.Array;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.ParamConverter;

/**
* Extract parameter value as a typed array.
*
* @param <T> parameter value type.
*/
class ArrayExtractor<T> extends AbstractParamValueExtractor<T> implements MultivaluedParameterExtractor<T[]> {

private final Class<?> type;

/**
* Create new array parameter extractor.
*
* @param type the type class to manage runtime T generic.
* @param converter parameter converter to be used to convert parameter from a String.
* @param parameterName parameter name.
* @param defaultStringValue default parameter String value.
*/
protected ArrayExtractor(Class<?> type, ParamConverter<T> converter, String parameterName, String defaultStringValue) {
super(converter, parameterName, defaultStringValue);
this.type = type;
}

@SuppressWarnings("unchecked")
@Override
public T[] extract(MultivaluedMap<String, String> parameters) {
List<String> stringList = parameters.get(getName());
T[] args = null;
if (stringList != null) {
args = (T[]) Array.newInstance(type, stringList.size());
for (int i = 0; i < stringList.size(); i++) {
args[i] = fromString(stringList.get(i));
}
} else if (isDefaultValueRegistered()) {
args = (T[]) Array.newInstance(type, 1);
args[0] = defaultValue();
} else {
args = (T[]) new Object[0];
}
return args;
}

/**
* Get string array extractor instance supporting.
*
* @param type the type class to manage runtime generic.
* @param parameterName extracted parameter name.
* @param defaultValue default parameter value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no defaultValue param

* @return string array extractor instance.
*/
public static <T> ArrayExtractor<T> getInstance(Class<?> type,
ParamConverter<T> converter,
String parameterName,
String defaultValueString) {
return new ArrayExtractor<>(type, converter, parameterName, defaultValueString);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates.
*
* This program and the accompanying materials are made available under the
Expand All @@ -25,19 +25,18 @@
import java.util.Set;
import java.util.SortedSet;

import javax.inject.Singleton;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.ext.ParamConverter;

import javax.inject.Singleton;

import org.glassfish.jersey.internal.inject.ExtractorException;
import org.glassfish.jersey.internal.inject.ParamConverterFactory;
import org.glassfish.jersey.internal.inject.PrimitiveMapper;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.internal.util.collection.ClassTypePair;
import org.glassfish.jersey.internal.util.collection.LazyValue;
import org.glassfish.jersey.server.internal.LocalizationMessages;
import org.glassfish.jersey.model.Parameter;
import org.glassfish.jersey.internal.inject.PrimitiveMapper;
import org.glassfish.jersey.server.internal.LocalizationMessages;

/**
* Implementation of {@link MultivaluedParameterExtractorProvider}. For each
Expand Down Expand Up @@ -120,6 +119,20 @@ private MultivaluedParameterExtractor<?> process(
throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
}
}
} else if (rawType.isArray()) {
converter = paramConverterFactory.getConverter(rawType.getComponentType(),
rawType.getComponentType(),
annotations);
if (converter == null) {
return null;
}
try {
return ArrayExtractor.getInstance(rawType.getComponentType(), converter, parameterName, defaultValue);
} catch (final ExtractorException e) {
throw e;
} catch (final Exception e) {
throw new ProcessingException(LocalizationMessages.ERROR_PARAMETER_TYPE_PROCESSING(rawType), e);
}
}

// Check primitive types.
Expand Down