Skip to content

Fixed exception handler order #1320

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ public Map<String, Object> getControllerAdviceMap() {
Map<String, Object> controllerAdviceMap = context.getBeansWithAnnotation(ControllerAdvice.class);
return Stream.of(controllerAdviceMap).flatMap(mapEl -> mapEl.entrySet().stream()).filter(
controller -> (AnnotationUtils.findAnnotation(controller.getValue().getClass(), Hidden.class) == null))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1, LinkedHashMap::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.org.springdoc.api.app166;

public class GlobalErrorResponseDto {

private String globalMessage;

public GlobalErrorResponseDto(String globalMessage) {
this.globalMessage = globalMessage;
}

public String getGlobalMessage() {
return globalMessage;
}

public void setGlobalMessage(String globalMessage) {
this.globalMessage = globalMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test.org.springdoc.api.app166;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Component("1") // bean name override to simulate order in HashMap
public class GlobalExceptionHandler {

@ExceptionHandler(RuntimeException.class)
@ResponseStatus
public GlobalErrorResponseDto processException() {
return new GlobalErrorResponseDto("global");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.org.springdoc.api.app166;

public class LocalErrorResponseDto {

private String localMessage;

public LocalErrorResponseDto(String localMessage) {
this.localMessage = localMessage;
}

public String getLocalMessage() {
return localMessage;
}

public void setLocalMessage(String localMessage) {
this.localMessage = localMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
*
* * Copyright 2019-2021 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app166;

import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;

/**
* In this test, it is checked that the error handler is displayed correctly in the documentation.
* Exactly, that the local handler takes precedence over the global one
* */
@TestPropertySource(properties = "springdoc.api-docs.resolve-schema-properties=true")
public class SpringDocApp166Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test.org.springdoc.api.app166;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Component("0") // bean name override to simulate order in HashMap
public class TestApiController {

@GetMapping(value = "/test")
public String throwError() {
throw new IllegalArgumentException();
}

@ExceptionHandler(RuntimeException.class)
@ResponseStatus
public LocalErrorResponseDto processException() {
return new LocalErrorResponseDto("local");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/test": {
"get": {
"tags": [
"test-api-controller"
],
"operationId": "throwError",
"responses": {
"500": {
"description": "Internal Server Error",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/LocalErrorResponseDto"
}
}
}
},
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"LocalErrorResponseDto": {
"type": "object",
"properties": {
"localMessage": {
"type": "string"
}
}
}
}
}
}