forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance Quarkus support for Spring @RequestParam annotated parameters.
- Loading branch information
1 parent
c52d210
commit d09ef82
Showing
14 changed files
with
636 additions
and
22 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
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
54 changes: 54 additions & 0 deletions
54
...a/io/quarkus/spring/web/resteasy/reactive/runtime/SpringMultiValueListParamExtractor.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,54 @@ | ||
package io.quarkus.spring.web.resteasy.reactive.runtime; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.core.parameters.ParameterExtractor; | ||
|
||
@SuppressWarnings("ForLoopReplaceableByForEach") | ||
public class SpringMultiValueListParamExtractor implements ParameterExtractor { | ||
|
||
/** | ||
* Returns a List containing all query parameters from the request, splitting values by commas if necessary. | ||
* | ||
* <p> | ||
* Spring MVC maps comma-delimited request parameters into a {@code List<String>}. | ||
* To maintain compatibility, this method follows the same approach: | ||
* If a query parameter contains multiple values, separated by commas, it adds those multiple values; otherwise, it is added | ||
* directly. | ||
* </p> | ||
* | ||
* <p> | ||
* Example: | ||
* | ||
* <pre> | ||
* ?tags=java,quarkus&category=framework | ||
* </pre> | ||
* | ||
* would produce: | ||
* | ||
* <pre> | ||
* ["java", "quarkus", "framework"] | ||
* </pre> | ||
* </p> | ||
* | ||
* @param context The ResteasyReactiveRequestContext containing the HTTP request. | ||
* @return An immutable list of all extracted query parameters. | ||
*/ | ||
public Object extractParameter(ResteasyReactiveRequestContext context) { | ||
Pattern commaPattern = Pattern.compile(","); | ||
|
||
List<String> allQueryParams = context.serverRequest().queryParamNames().stream() | ||
.map(context.serverRequest()::getAllQueryParams) | ||
.filter(Objects::nonNull) | ||
.flatMap(List::stream) | ||
.flatMap(value -> commaPattern.splitAsStream(value)) | ||
.collect(Collectors.toList()); | ||
|
||
return List.copyOf(allQueryParams); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...va/io/quarkus/spring/web/resteasy/reactive/runtime/SpringMultiValueMapParamExtractor.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,21 @@ | ||
package io.quarkus.spring.web.resteasy.reactive.runtime; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.core.parameters.ParameterExtractor; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
@SuppressWarnings("ForLoopReplaceableByForEach") | ||
public class SpringMultiValueMapParamExtractor implements ParameterExtractor { | ||
|
||
@Override | ||
public Object extractParameter(ResteasyReactiveRequestContext context) { | ||
Map<String, List<String>> parametersMap = context.serverRequest().getParametersMap(); | ||
MultiValueMap<String, String> springMap = new LinkedMultiValueMap<>(); | ||
parametersMap.forEach(springMap::put); | ||
return springMap; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../main/java/io/quarkus/spring/web/resteasy/reactive/runtime/SpringRequestParamHandler.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,38 @@ | ||
package io.quarkus.spring.web.resteasy.reactive.runtime; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl; | ||
import org.jboss.resteasy.reactive.common.model.ResourceClass; | ||
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext; | ||
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer; | ||
import org.jboss.resteasy.reactive.server.model.ServerResourceMethod; | ||
import org.jboss.resteasy.reactive.server.spi.ServerRestHandler; | ||
|
||
/** | ||
* In Spring, parameters annotated with {@code @RequestParam} are required by default unless explicitly marked as | ||
* optional. | ||
* This {@link SpringRequestParamHandler} enforces the required constraint responding with a BAD_REQUEST status. | ||
* | ||
*/ | ||
public class SpringRequestParamHandler implements HandlerChainCustomizer { | ||
@Override | ||
public List<ServerRestHandler> handlers(HandlerChainCustomizer.Phase phase, ResourceClass resourceClass, | ||
ServerResourceMethod resourceMethod) { | ||
if (phase == Phase.AFTER_RESPONSE_CREATED) { | ||
return Collections.singletonList(new ServerRestHandler() { | ||
@Override | ||
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception { | ||
Map<String, List<String>> parametersMap = requestContext.serverRequest().getParametersMap(); | ||
if (parametersMap.isEmpty()) { | ||
ResponseImpl response = (ResponseImpl) requestContext.getResponse().get(); | ||
response.setStatus(400); | ||
} | ||
} | ||
}); | ||
} | ||
return Collections.emptyList(); | ||
} | ||
} |
Oops, something went wrong.