Skip to content

Wrong ApiResponse Schema picked up in ExceptionHandlers returning void #711

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

Closed
birnbuazn opened this issue Jun 4, 2020 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@birnbuazn
Copy link

Describe the bug

I have a Spring Boot MVC application where exceptions are handled within a general @ControllerAdvice. Some of them do not include a response body, like for instance:

@ExceptionHandler(EntityNotFoundException.class)
@ResponseStatus(NOT_FOUND)
void handleEntityNotFound() {
}

The exception handler is picked up, but a wrong api response is generated for the 404s:

        [...]
        "responses": {
          "404": {
            "description": "Not Found",
            "content": {
              "*/*": {
                "schema": {
                  "$ref": "#/components/schemas/EventResponse"
                }
              }
            }
          },
        [...]

To Reproduce

The problem manifests itself as soon as I add a GET endpoint (in any RestController) that can serve different media types depending on the Accept header of the request. In my case:

  @GetMapping(value = "/{eventId}")
  EventResponse read(@PathVariable UUID eventId) {
    return new EventResponse(...);
  }

  @GetMapping(value = "/{eventId}", produces = TEXT_EVENT_STREAM_VALUE)
  Flux<ServerSentEvent<EventResponse>> readAsStream(@PathVariable UUID eventId) {
    return Flux.interval(...);
  }

As soon as I put a @Hidden on the readAsStream method, everything works as expected and the api doc does not include a response for the 404.

Expected behavior

I would expect the api doc for the exception handler to never return any content, because its return method reads void:

        [...]
          "404": {
            "description": "Not Found"
          },
        [...]

The bug, btw, only occurs for exception handlers returning void. If the exception handler returns a response (for example a custom ApiError class), everything works fine.

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Jun 4, 2020

@birnbuazn,

Not reproducible, with the provided description.
You mention spring-mvc, but your example contains webflux.
The expected response is correct.

Please make sure you have read the contribution guide: section Using GitHub Issues

We have added an enhancement to handle similar cases, which may fix your issue as well:

If you believe the issue is still there, please provide a complete minimal reproducible sample.

@bnasslahsen bnasslahsen added the bug Something isn't working label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants