Skip to content

Commit

Permalink
Merge pull request #35850 from geoand/#35845
Browse files Browse the repository at this point in the history
Updated @RequestParam#required handling
  • Loading branch information
geoand authored Sep 11, 2023
2 parents 87eb294 + 3d90b19 commit 99ee1af
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,21 @@ public void transform(TransformationContext transformationContext) {
}
transform.add(create(jaxRsAnnotation, annotation.target(), annotationValues));

boolean required = true; // the default value
String defaultValueStr = DEFAULT_NONE; // default value of @RequestMapping#defaultValue
String defaultValueStr = null;
AnnotationValue defaultValue = annotation.value("defaultValue");
if (defaultValue != null) {
defaultValueStr = defaultValue.asString();
required = false; // implicitly set according to the javadoc of @RequestMapping#defaultValue
} else {
AnnotationValue requiredValue = annotation.value("required");
if (requiredValue != null) {
required = requiredValue.asBoolean();
if (requiredValue.asBoolean()) {
throw new IllegalArgumentException(
"Using required @RequestMapping is not supported. Offending method is '"
+ methodInfo.declaringClass().name() + "#" + methodInfo.name() + "'");
}
}
}
if (!required) {
if (defaultValueStr != null) {
transform.add(create(DEFAULT_VALUE, annotation.target(),
Collections
.singletonList(AnnotationValue.createStringValue("value", defaultValueStr))));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.spring.web.test;

import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.quarkus.test.QuarkusUnitTest;

public class RequiredFalseTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Controller.class));

@Test
public void test() {
when().get("/endpoint/boxed")
.then()
.statusCode(200)
.body(is(""));

when().get("/endpoint/primitive")
.then()
.statusCode(200)
.body(is("0"));
}

@RestController
@RequestMapping("/endpoint")
public static class Controller {

@GetMapping("/boxed")
public ResponseEntity<Long> boxed(@RequestParam(value = "id", required = false) Long id) {
return ResponseEntity.ok(id);
}

@GetMapping("/primitive")
public ResponseEntity<Long> primitive(@RequestParam(value = "id", required = false) long id) {
return ResponseEntity.ok(id);
}
}
}

0 comments on commit 99ee1af

Please sign in to comment.