diff --git a/springdoc-openapi-webmvc-core/pom.xml b/springdoc-openapi-webmvc-core/pom.xml index ee9352285..9d47cbdf8 100644 --- a/springdoc-openapi-webmvc-core/pom.xml +++ b/springdoc-openapi-webmvc-core/pom.xml @@ -1,5 +1,9 @@ 4.0.0 + + 17 + 17 + org.springdoc springdoc-openapi diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/CustomExceptionHandler.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/CustomExceptionHandler.java new file mode 100644 index 000000000..87d700197 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/CustomExceptionHandler.java @@ -0,0 +1,23 @@ +package test.org.springdoc.api.v30.app199; + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +@ControllerAdvice +public class CustomExceptionHandler { + + static public class MyInternalException extends Exception {} + + @ResponseStatus( value = INTERNAL_SERVER_ERROR ) + @ExceptionHandler( MyInternalException.class ) + @ResponseBody + public ErrorDto handleMyInternalException( final MyInternalException ex ) { + return new ErrorDto( ex.getMessage() ); + } + + +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/ErrorDto.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/ErrorDto.java new file mode 100644 index 000000000..20b4a0e73 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/ErrorDto.java @@ -0,0 +1,12 @@ +package test.org.springdoc.api.v30.app199; + +public class ErrorDto { + private String message; + + public ErrorDto( final String message ) { + this.message = message; + } + + public String getMessage() { return message; } + public void setMessage( final String message ) { this.message = message; } +} diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/HelloController.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/HelloController.java new file mode 100644 index 000000000..629f59ef5 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/HelloController.java @@ -0,0 +1,105 @@ +/* + * + * * + * * * + * * * * Copyright 2019-2022 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.v30.app199; + + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.examples.Example; +import org.springdoc.core.customizers.OperationCustomizer; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.method.HandlerMethod; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.springframework.http.MediaType.ALL_VALUE; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; + +@RestController +public class HelloController { + + @Autowired + ObjectMapper defaultObjectMapper; + + @GetMapping( + value = "/first", + produces = APPLICATION_JSON_VALUE + ) + public void first() {} + + @GetMapping( + value = "/second", + produces = APPLICATION_JSON_VALUE + ) + public void second() {} + + @Bean + public OperationCustomizer operationCustomizer() + { + return ( Operation operation, HandlerMethod handlerMethod ) -> + { + final io.swagger.v3.oas.models.media.MediaType mediaType = operation + .getResponses() + .get( "500" ) + .getContent() + .get( ALL_VALUE ); + + mediaType.setExamples( mediaType.getExamples() != null + ? mediaType.getExamples() + : new LinkedHashMap<>() ); + + final Map examples = mediaType.getExamples(); + + switch ( handlerMethod.getMethod().getName() ) { + + case "first" : + examples.put( + "First case example", + new Example().value( + defaultObjectMapper.valueToTree( + new ErrorDto( "An ErrorDto sample specific to /first endpoint" ) + ) + ) + ); + break; + + case "second" : + examples.put( + "Second case example", + new Example().value( + defaultObjectMapper.valueToTree( + new ErrorDto( "An ErrorDto sample specific to /second endpoint" ) + ) + ) + ); + break; + } + + return operation; + }; + } +} \ No newline at end of file diff --git a/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/SpringDocApp199Test.java b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/SpringDocApp199Test.java new file mode 100644 index 000000000..3d4367011 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/v30/app199/SpringDocApp199Test.java @@ -0,0 +1,33 @@ +/* + * + * * + * * * + * * * * Copyright 2019-2022 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.v30.app199; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import test.org.springdoc.api.v30.AbstractSpringDocV30Test; + +public class SpringDocApp199Test extends AbstractSpringDocV30Test { + + @SpringBootApplication + static class SpringDocTestApp {} + +} \ No newline at end of file diff --git a/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app199.json b/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app199.json new file mode 100644 index 000000000..baacf3e64 --- /dev/null +++ b/springdoc-openapi-webmvc-core/src/test/resources/results/3.0.1/app199.json @@ -0,0 +1 @@ +{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost","description":"Generated server url"}],"paths":{"/second":{"get":{"tags":["hello-controller"],"operationId":"second","responses":{"500":{"description":"Internal Server Error","content":{"*/*":{"schema":{"$ref":"#/components/schemas/ErrorDto"},"examples":{"Second case example":{"value":{"message":"An ErrorDto sample specific to /second endpoint"}}}}}},"200":{"description":"OK"}}}},"/first":{"get":{"tags":["hello-controller"],"operationId":"first","responses":{"500":{"description":"Internal Server Error","content":{"*/*":{"schema":{"$ref":"#/components/schemas/ErrorDto"},"examples":{"First case example":{"value":{"message":"An ErrorDto sample specific to /first endpoint"}}}}}},"200":{"description":"OK"}}}}},"components":{"schemas":{"ErrorDto":{"type":"object","properties":{"message":{"type":"string"}}}}}} \ No newline at end of file