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.
Add SpringRequestParamHandler to implement the required by default be…
…haviour in @RequestParam parameters. Add and improve tests
- Loading branch information
1 parent
01d9040
commit b9c599a
Showing
3 changed files
with
121 additions
and
45 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
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(); | ||
} | ||
} |
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