-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Introduce @Separator annotation for query parameters
- Loading branch information
Showing
12 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
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
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
69 changes: 69 additions & 0 deletions
69
...rc/test/java/io/quarkus/resteasy/reactive/server/test/simple/SeparatorQueryParamTest.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,69 @@ | ||
package io.quarkus.resteasy.reactive.server.test.simple; | ||
|
||
import static io.restassured.RestAssured.*; | ||
|
||
import java.util.List; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.resteasy.reactive.RestQuery; | ||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.jboss.resteasy.reactive.Separator; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SeparatorQueryParamTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(HelloResource.class)); | ||
|
||
@Test | ||
public void noQueryParams() { | ||
get("/hello") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo("hello world")) | ||
.header("x-size", "0"); | ||
} | ||
|
||
@Test | ||
public void singleQueryParam() { | ||
get("/hello?name=foo") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo("hello foo")) | ||
.header("x-size", "1"); | ||
} | ||
|
||
@Test | ||
public void multipleQueryParams() { | ||
get("/hello?name=foo,bar&name=one,two,three&name=yolo") | ||
.then() | ||
.statusCode(200) | ||
.body(Matchers.equalTo("hello foo bar one two three yolo")) | ||
.header("x-size", "6"); | ||
} | ||
|
||
@Path("hello") | ||
public static class HelloResource { | ||
|
||
@GET | ||
public RestResponse<String> hello(@RestQuery("name") @Separator(",") List<String> names) { | ||
int size = names.size(); | ||
String body = ""; | ||
if (names.isEmpty()) { | ||
body = "hello world"; | ||
} else { | ||
body = "hello " + String.join(" ", names); | ||
} | ||
return RestResponse.ResponseBuilder.ok(body).header("x-size", size).build(); | ||
} | ||
|
||
} | ||
} |
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
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
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
18 changes: 18 additions & 0 deletions
18
...resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/Separator.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,18 @@ | ||
package org.jboss.resteasy.reactive; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import java.util.List; | ||
|
||
/** | ||
* When used on a {@link List} parameter annotated with {@link RestQuery}, RESTEasy Reactive will split the value of the | ||
* parameter (using the value of the annotation) and populate the list with those values. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.PARAMETER }) | ||
public @interface Separator { | ||
|
||
String value(); | ||
} |
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
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
30 changes: 28 additions & 2 deletions
30
...src/main/java/org/jboss/resteasy/reactive/server/core/parameters/QueryParamExtractor.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 |
---|---|---|
@@ -1,21 +1,47 @@ | ||
package org.jboss.resteasy.reactive.server.core.parameters; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
|
||
public class QueryParamExtractor implements ParameterExtractor { | ||
|
||
private final String name; | ||
private final boolean single; | ||
private final boolean encoded; | ||
private final String separator; | ||
|
||
public QueryParamExtractor(String name, boolean single, boolean encoded) { | ||
public QueryParamExtractor(String name, boolean single, boolean encoded, String separator) { | ||
this.name = name; | ||
this.single = single; | ||
this.encoded = encoded; | ||
this.separator = separator; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public Object extractParameter(ResteasyReactiveRequestContext context) { | ||
return context.getQueryParameter(name, single, encoded); | ||
Object queryParameter = context.getQueryParameter(name, single, encoded); | ||
if (separator != null) { | ||
if (queryParameter instanceof List) { // it's List<String> | ||
List<String> list = (List<String>) queryParameter; | ||
List<String> result = new ArrayList<>(list.size()); | ||
for (int i = 0; i < list.size(); i++) { | ||
String[] parts = list.get(i).split(separator); | ||
result.addAll(Arrays.asList(parts)); | ||
} | ||
queryParameter = result; | ||
} else if (queryParameter instanceof String) { | ||
List<String> result = new ArrayList<>(1); | ||
String[] parts = ((String) queryParameter).split(separator); | ||
result.addAll(Arrays.asList(parts)); | ||
queryParameter = result; | ||
} else { | ||
// can't really happen | ||
} | ||
} | ||
return queryParameter; | ||
} | ||
} |
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
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