springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.29k stars 500 forks source link

Wrong ApiResponse Schema picked up in ExceptionHandlers returning void #711

Closed birnbuazn closed 4 years ago

birnbuazn commented 4 years ago

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 commented 4 years ago

@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.