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

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

Closed
testersen opened this issue Jan 14, 2024 · 4 comments

Comments

@testersen
Copy link

testersen commented Jan 14, 2024

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.

@iosonopersia
Copy link

I have the same problem

@bnasslahsen
Copy link
Contributor

@iosonopersia and @testersen,

I have added a fix for this request.
You can validate it already based on the Latest SNAPSHOT

@iosonopersia
Copy link

@bnasslahsen I can confirm you that, by using version 2.3.1-SNAPSHOT, the problem is completely gone and everything seems to work as expected. Thank you for the effort, looking forward to the release of version 2.3.1!

@testersen
Copy link
Author

Good work @bnasslahsen, look forward to see it hit 2.3.1! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants