Skip to content

ExceptionHandler in controller is not used by another controller #1909

Closed
@uc4w6c

Description

@uc4w6c

Describe the bug
In the following cases, each controller should only return the HTTP Status set in its own ExceptionHandler.
An ExceptionHandler in a controller returns BAD_REQUEST.
An ExceptionHandler in another controller returns INTERNAL_SERVER_ERROR.

Related ticket: #1845

To Reproduce

GET /exception1

@RestController
@RequestMapping("exception1")
public class ExceptionController1 {
  @GetMapping
  public String index() {
    throw new ExampleException();
  }

  @ExceptionHandler(ExampleException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ApiResponse(responseCode = "400", description = "bad request")
  public String customControllerException() {
    return "exception1";
  }
}
$ curl -v localhost:8080/exception1
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /exception1 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 10
< Date: Mon, 24 Oct 2022 15:10:58 GMT
< Connection: close
< 
* Closing connection 0
exception1

GET /exception2

@RestController
@RequestMapping("exception2")
public class ExceptionController2 {
  @GetMapping
  public Map<String, String> index() {
    throw new ExampleException();
  }

  @ExceptionHandler(Exception.class)
  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  @ApiResponse(responseCode = "500", description = "bad request")
  public String customControllerException() {
    return "exception2";
  }
}
$ curl -v localhost:8080/exception2
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /exception2 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.79.1
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 10
< Date: Mon, 24 Oct 2022 15:11:37 GMT
< Connection: close
< 
* Closing connection 0
exception2

Steps to reproduce the behavior:

  • What version of spring-boot you are using? 2.7.5
  • What modules and versions of springdoc-openapi are you using? springdoc-openapi-webmvc-core 1.6.12
  • 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

Expected behavior

Actual

		"/exception2": {
			"get": {
                                 ...
				"responses": {
					"200": {
                                          ...
					},
					"400": {
                                          ...
					},
					"500": {
                                          ...
					}
				}
			}
		},
		"/exception1": {
			"get": {
                                 ...
				"responses": {
					"200": {
                                          ...
					},
					"400": {
                                          ...
					},
					"500": {
                                          ...
					}
				}
			}
		},

Expected:

		"/exception2": {
			"get": {
                                 ...
				"responses": {
					"200": {
                                          ...
					},
					"500": {
                                          ...
					}
				}
			}
		},
		"/exception1": {
			"get": {
                                 ...
				"responses": {
					"200": {
                                          ...
					},
					"400": {
                                          ...
					}
				}
			}
		},

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions