Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make @ResponseStatus on a void controller advice method optional #5395

Merged
merged 1 commit into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,14 @@ protected void preGenerateMethodBody(ClassCreator cc) {
void generateMethodBody(MethodCreator toResponse) {
if (returnType.kind() == Type.Kind.VOID) {
AnnotationInstance responseStatusInstance = controllerAdviceMethod.annotation(RESPONSE_STATUS);
if (responseStatusInstance == null) {
throw new IllegalStateException(
"void methods annotated with @ExceptionHandler must also be annotated with @ResponseStatus");
}

// invoke the @ExceptionHandler method
exceptionHandlerMethodResponse(toResponse);

// build a JAX-RS response
ResultHandle status = toResponse.load(getHttpStatusFromAnnotation(responseStatusInstance));
ResultHandle status = toResponse
.load(responseStatusInstance != null ? getHttpStatusFromAnnotation(responseStatusInstance)
: Response.Status.NO_CONTENT.getStatusCode());
ResultHandle responseBuilder = toResponse.invokeStaticMethod(
MethodDescriptor.ofMethod(Response.class, "status", Response.ResponseBuilder.class, int.class),
status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,6 @@ private void generateMappersForExceptionHandlerInControllerAdvice(
reflectiveClassProducer.produce(new ReflectiveClassBuildItem(true, true, returnTypeDotName.toString()));
}

AnnotationInstance responseStatusInstance = method.annotation(RESPONSE_STATUS);
if ((method.returnType().kind() == Type.Kind.VOID) && (responseStatusInstance == null)) {
throw new IllegalStateException(
"void methods annotated with @ExceptionHandler must also be annotated with @ResponseStatus");
}

// we need to generate one JAX-RS ExceptionMapper per Exception type
Type[] handledExceptionTypes = exceptionHandlerInstance.value().asClassArray();
for (Type handledExceptionType : handledExceptionTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public void handleRuntimeException() {

}

@ExceptionHandler(UnannotatedException.class)
public void unannotatedException() {

}

@ExceptionHandler(IllegalStateException.class)
public ResponseEntity<Error> handleIllegalStateException(IllegalStateException e,
HttpServletRequest request, HttpServletResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public void runtimeException() {
throw new RuntimeException();
}

@GetMapping("/unannotated")
public void unannotated() {
throw new UnannotatedException();
}

@GetMapping("/responseEntity")
public Greeting handledByResponseEntity() {
throw new IllegalStateException("bad state");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.quarkus.it.spring.web;

public class UnannotatedException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public void testExceptionHandlerVoidReturnType() {
.statusCode(400);
}

@Test
public void testExceptionHandlerWithoutResponseStatusOnExceptionOrMethod() {
RestAssured.when().get("/exception/unannotated").then()
.contentType("text/plain")
.body(isEmptyString())
.statusCode(204);
}

@Test
public void testExceptionHandlerResponseEntityType() {
RestAssured.when().get("/exception/responseEntity").then()
Expand Down