Skip to content

Commit a34d022

Browse files
authored
Merge pull request #50400 from aureamunoz/request-param-required-fix-50321
Modify @RequestParam presence handling
2 parents 9f1f9ca + ef996a0 commit a34d022

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

extensions/spring-web/resteasy-reactive/runtime/src/main/java/io/quarkus/spring/web/resteasy/reactive/runtime/SpringRequestParamHandler.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import java.util.List;
55
import java.util.Map;
66

7-
import org.jboss.resteasy.reactive.common.jaxrs.ResponseImpl;
7+
import jakarta.ws.rs.WebApplicationException;
8+
import jakarta.ws.rs.core.Response;
9+
810
import org.jboss.resteasy.reactive.common.model.ResourceClass;
911
import org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext;
1012
import org.jboss.resteasy.reactive.server.model.HandlerChainCustomizer;
@@ -21,18 +23,19 @@ public class SpringRequestParamHandler implements HandlerChainCustomizer {
2123
@Override
2224
public List<ServerRestHandler> handlers(HandlerChainCustomizer.Phase phase, ResourceClass resourceClass,
2325
ServerResourceMethod resourceMethod) {
24-
if (phase == Phase.AFTER_RESPONSE_CREATED) {
26+
if (phase == Phase.RESOLVE_METHOD_PARAMETERS) {
2527
return Collections.singletonList(new ServerRestHandler() {
2628
@Override
2729
public void handle(ResteasyReactiveRequestContext requestContext) throws Exception {
2830
Map<String, List<String>> parametersMap = requestContext.serverRequest().getQueryParamsMap();
2931
if (parametersMap.isEmpty()) {
30-
ResponseImpl response = (ResponseImpl) requestContext.getResponse().get();
31-
response.setStatus(400);
32+
throw new WebApplicationException("Missing required param in method '" + resourceMethod.getName() + "'",
33+
Response.Status.BAD_REQUEST);
3234
}
3335
}
3436
});
3537
}
3638
return Collections.emptyList();
3739
}
40+
3841
}

extensions/spring-web/resteasy-reactive/tests/src/test/java/io/quarkus/spring/web/requestparam/RequestParamController.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
import java.util.Optional;
66
import java.util.stream.Collectors;
77

8+
import jakarta.ws.rs.WebApplicationException;
9+
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
812
import org.springframework.util.MultiValueMap;
13+
import org.springframework.web.bind.annotation.ExceptionHandler;
914
import org.springframework.web.bind.annotation.GetMapping;
1015
import org.springframework.web.bind.annotation.PostMapping;
1116
import org.springframework.web.bind.annotation.RequestMapping;
1217
import org.springframework.web.bind.annotation.RequestParam;
1318
import org.springframework.web.bind.annotation.ResponseBody;
1419
import org.springframework.web.bind.annotation.RestController;
20+
import org.springframework.web.bind.annotation.RestControllerAdvice;
1521

1622
@RestController
1723
@RequestMapping
@@ -69,4 +75,18 @@ public String updateFoos(@RequestParam MultiValueMap<String, String> allParams)
6975
return result;
7076
}
7177

78+
@GetMapping("/api/foos/paramRequired")
79+
public String getFoosParamRequired(@RequestParam String id) {
80+
throw new IllegalStateException("Unexpected state. Should have not been called");
81+
}
82+
83+
@RestControllerAdvice
84+
public static class RestExceptionHandler {
85+
86+
@ExceptionHandler(WebApplicationException.class)
87+
public ResponseEntity<Object> handleException(Exception ex) {
88+
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
89+
}
90+
}
91+
7292
}

extensions/spring-web/resteasy-reactive/tests/src/test/java/io/quarkus/spring/web/requestparam/RequestParamControllerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void testSimpleMapping() throws Exception {
2626
// In Spring, method parameters annotated with @RequestParam are required by default.
2727
when().get("/api/foos")
2828
.then()
29-
.statusCode(400);
29+
.statusCode(400).body(containsString("Missing required param in method"));
3030
}
3131

3232
@Test
@@ -123,4 +123,12 @@ public void testMultiMap() throws Exception {
123123
.statusCode(400);
124124
}
125125

126+
@Test
127+
public void shouldFailWithBadRequestWhenMissingMandatoryParam() throws Exception {
128+
// In Spring, method parameters annotated with @RequestParam are required by default.
129+
when().get("/api/foos/paramRequired")
130+
.then()
131+
.statusCode(400).body(containsString("Missing required param in method"));
132+
}
133+
126134
}

0 commit comments

Comments
 (0)