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

ExceptionHandler in controller is not used by another controller #1909

Closed
uc4w6c opened this issue Oct 24, 2022 · 0 comments · Fixed by #1910
Closed

ExceptionHandler in controller is not used by another controller #1909

uc4w6c opened this issue Oct 24, 2022 · 0 comments · Fixed by #1910
Labels
enhancement New feature or request

Comments

@uc4w6c
Copy link
Collaborator

uc4w6c commented Oct 24, 2022

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": {
                                          ...
					}
				}
			}
		},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants