Skip to content

Controller advice documents ApiResponse on every operation, even if the operation does not annotate the exception to be thrown #2483

Closed
@jamesbradlee

Description

@jamesbradlee

Describe the bug

Exception handlers annotated with @ApiResponse and @ResponseStatus seem to be applied to all operations, even if the implementing operation method handler does not throw the exception handled by the exception handler.

To Reproduce
Steps to reproduce the behavior:

  • spring-boot: 3.2.1
  • org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
  • Provide with a sample code (HelloController) or Test that reproduces the problem
data class HelloResponse(val value: String)

@RestController
class HelloController {
  @GetMapping("/hello")
  @Operation(operationId = "hello", summary = "Hello, World!")
  fun hello() = HelloResponse("World")

  @GetMapping("/greet/{name}")
  @Operation(operation = "greet", summary = "Hello someone!")
  @Throws(InvalidNameException::class)
  fun greet(@PathVariable name: String) = throw InvalidNameException("Name '$name' is invalid!")
}

@ControllerAdvice
class HelloAdvisor {
  @ExceptionHandler(InvalidNameException::class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ApiResponse(description = "The provided name is invalid", useReturnScheme = true)
  fun handleNameException(e: InvalidNameException): MyErrorResponse {
    TODO()
  }
}

Expected behavior

I would expect only the greeting operation to be documented with the invalid name exception, but the hello exception is also documented with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions