-
Notifications
You must be signed in to change notification settings - Fork 357
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
...or/src/test/java/org/glassfish/jersey/jdk/connector/internal/ArrayParamConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
core-server/src/main/java/org/glassfish/jersey/server/internal/inject/ArrayExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no |
||
* @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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.