-
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.
Support for populating Map<String, String> and MultiValueMap<String, …
…String> with request parameters in @RequestParam
- Loading branch information
1 parent
f040b40
commit 88ef3ec
Showing
12 changed files
with
380 additions
and
23 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
32 changes: 32 additions & 0 deletions
32
...t/src/main/java/io/quarkus/spring/web/resteasy/reactive/deployment/MapParamConverter.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,32 @@ | ||
package io.quarkus.spring.web.resteasy.reactive.deployment; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import jakarta.ws.rs.ext.ParamConverter; | ||
|
||
public class MapParamConverter implements ParamConverter<Map<String, String>> { | ||
|
||
@Override | ||
public Map<String, String> fromString(String value) { | ||
// Parse params to a Map | ||
Map<String, String> map = new HashMap<>(); | ||
if (value != null && !value.isEmpty()) { | ||
String[] pairs = value.split("&"); | ||
for (String pair : pairs) { | ||
String[] keyValue = pair.split("="); | ||
if (keyValue.length == 2) { | ||
map.put(keyValue[0], keyValue[1]); | ||
} | ||
} | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public String toString(Map<String, String> value) { | ||
StringBuilder sb = new StringBuilder(); | ||
value.forEach((key, val) -> sb.append(key).append("=").append(val).append("&")); | ||
return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : ""; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...in/java/io/quarkus/spring/web/resteasy/reactive/deployment/MapParamConverterProvider.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,20 @@ | ||
package io.quarkus.spring.web.resteasy.reactive.deployment; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.Map; | ||
|
||
import jakarta.ws.rs.ext.ParamConverter; | ||
import jakarta.ws.rs.ext.ParamConverterProvider; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
@Provider | ||
public class MapParamConverterProvider implements ParamConverterProvider { | ||
@Override | ||
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { | ||
if (rawType == Map.class) { | ||
return (ParamConverter<T>) new MapParamConverter(); | ||
} | ||
return null; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...active/tests/src/test/java/io/quarkus/spring/web/requestparam/RequestParamController.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,60 @@ | ||
package io.quarkus.spring.web.requestparam; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping | ||
public class RequestParamController { | ||
|
||
@GetMapping("/api/foos") | ||
@ResponseBody | ||
public String getFoos(@RequestParam String id) { | ||
return "ID: " + id; | ||
} | ||
|
||
@PostMapping("/api/foos") | ||
@ResponseBody | ||
public String addFoo(@RequestParam(name = "id") String fooId, @RequestParam String name) { | ||
return "ID: " + fooId + " Name: " + name; | ||
} | ||
|
||
@GetMapping("/api/foos/notParamRequired") | ||
@ResponseBody | ||
public String getFoosNotParamRequired2(@RequestParam(required = false) String id) { | ||
return "ID: " + id; | ||
} | ||
|
||
@GetMapping("/api/foos/optional") | ||
@ResponseBody | ||
public String getFoosOptional(@RequestParam Optional<String> id) { | ||
return "ID: " + id.orElseGet(() -> "not provided"); | ||
} | ||
|
||
@GetMapping("/api/foos/defaultValue") | ||
@ResponseBody | ||
public String getFoosDefaultValue(@RequestParam(defaultValue = "test") String id) { | ||
return "ID: " + id; | ||
} | ||
|
||
@PostMapping("/api/foos/map") | ||
@ResponseBody | ||
public String updateFoos(@RequestParam Map<String, String> allParams) { | ||
return "Parameters are " + allParams.entrySet(); | ||
} | ||
|
||
@GetMapping("/api/foos/multivalue") | ||
@ResponseBody | ||
public String getFoosMultiValue(@RequestParam List<String> id) { | ||
return "IDs are " + id; | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...ve/tests/src/test/java/io/quarkus/spring/web/requestparam/RequestParamControllerTest.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,94 @@ | ||
package io.quarkus.spring.web.requestparam; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class RequestParamControllerTest { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(RequestParamController.class)); | ||
|
||
@Test | ||
public void whenInvokeGetQueryStringThenTheOriginQueryStringIsReturned() throws Exception { | ||
when().get("/api/foos?id=abc") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: abc")); | ||
|
||
// should return 400 because, in Spring, method parameters annotated with @RequestParam are required by default. | ||
// see SpringWebResteasyReactiveProcessor L298 | ||
when().get("/api/foos") | ||
.then() | ||
.statusCode(200); | ||
} | ||
|
||
@Test | ||
public void whenConfigureParamToBeOptionalThenTheGetQueryWorksWithAndWithoutRequestParam() throws Exception { | ||
when().get("/api/foos/notParamRequired?id=abc") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: abc")); | ||
|
||
when().get("/api/foos/notParamRequired") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: null")); | ||
} | ||
|
||
@Test | ||
public void whenWrapingParamInOptionalThenTheGetQueryWorksWithAndWithoutRequestParam() throws Exception { | ||
when().get("/api/foos/optional?id=abc") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: abc")); | ||
|
||
when().get("/api/foos/optional") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: not provided")); | ||
} | ||
|
||
@Test | ||
public void whenDefaultValueProvidedThenItIsReturnedIfRequestParamIsAbsent() throws Exception { | ||
when().get("/api/foos/defaultValue?id=abc") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: abc")); | ||
|
||
when().get("/api/foos/defaultValue") | ||
.then() | ||
.statusCode(200) | ||
.body(is("ID: test")); | ||
} | ||
|
||
@Test | ||
public void whenInvokePostQueryWithSpecificParamNameThenTheOriginQueryStringIsReturned() throws Exception { | ||
when().post("/api/foos/map?id=abc&name=bar") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Parameters are [name=bar, id=abc]")); | ||
|
||
} | ||
|
||
@Test | ||
public void testMultivalue() throws Exception { | ||
when().get("/api/foos/multivalue?id=1,2,3") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("IDs are [1,2,3]")); | ||
|
||
when().get("/api/foos/multivalue?id=1&id=2") | ||
.then() | ||
.statusCode(200).log().body() | ||
.body(containsString("IDs are [1, 2]")); | ||
} | ||
|
||
} |
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
Oops, something went wrong.