Skip to content

Commit abf6816

Browse files
committed
failling test case 199 for issue 1962
1 parent da42652 commit abf6816

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

springdoc-openapi-webmvc-core/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22
<modelVersion>4.0.0</modelVersion>
3+
<properties>
4+
<maven.compiler.source>17</maven.compiler.source>
5+
<maven.compiler.target>17</maven.compiler.target>
6+
</properties>
37
<parent>
48
<groupId>org.springdoc</groupId>
59
<artifactId>springdoc-openapi</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app199;
2+
3+
import org.springframework.web.bind.annotation.ControllerAdvice;
4+
import org.springframework.web.bind.annotation.ExceptionHandler;
5+
import org.springframework.web.bind.annotation.ResponseBody;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
8+
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
9+
10+
@ControllerAdvice
11+
public class CustomExceptionHandler {
12+
13+
static public class MyInternalException extends Exception {}
14+
15+
@ResponseStatus( value = INTERNAL_SERVER_ERROR )
16+
@ExceptionHandler( MyInternalException.class )
17+
@ResponseBody
18+
public ErrorDto handleMyInternalException( final MyInternalException ex ) {
19+
return new ErrorDto( ex.getMessage() );
20+
}
21+
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.v30.app199;
2+
3+
public class ErrorDto {
4+
private String message;
5+
6+
public ErrorDto( final String message ) {
7+
this.message = message;
8+
}
9+
10+
public String getMessage() { return message; }
11+
public void setMessage( final String message ) { this.message = message; }
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app199;
24+
25+
26+
import com.fasterxml.jackson.databind.ObjectMapper;
27+
import io.swagger.v3.oas.models.Operation;
28+
import io.swagger.v3.oas.models.examples.Example;
29+
import org.springdoc.core.customizers.OperationCustomizer;
30+
import org.springframework.beans.factory.annotation.Autowired;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.web.bind.annotation.GetMapping;
33+
import org.springframework.web.bind.annotation.RestController;
34+
import org.springframework.web.method.HandlerMethod;
35+
36+
import java.util.LinkedHashMap;
37+
import java.util.Map;
38+
39+
import static org.springframework.http.MediaType.ALL_VALUE;
40+
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
41+
42+
@RestController
43+
public class HelloController {
44+
45+
@Autowired
46+
ObjectMapper defaultObjectMapper;
47+
48+
@GetMapping(
49+
value = "/first",
50+
produces = APPLICATION_JSON_VALUE
51+
)
52+
public void first() {}
53+
54+
@GetMapping(
55+
value = "/second",
56+
produces = APPLICATION_JSON_VALUE
57+
)
58+
public void second() {}
59+
60+
@Bean
61+
public OperationCustomizer operationCustomizer()
62+
{
63+
return ( Operation operation, HandlerMethod handlerMethod ) ->
64+
{
65+
final io.swagger.v3.oas.models.media.MediaType mediaType = operation
66+
.getResponses()
67+
.get( "500" )
68+
.getContent()
69+
.get( ALL_VALUE );
70+
71+
mediaType.setExamples( mediaType.getExamples() != null
72+
? mediaType.getExamples()
73+
: new LinkedHashMap<>() );
74+
75+
final Map<String, Example> examples = mediaType.getExamples();
76+
77+
switch ( handlerMethod.getMethod().getName() ) {
78+
79+
case "first" :
80+
examples.put(
81+
"First case example",
82+
new Example().value(
83+
defaultObjectMapper.valueToTree(
84+
new ErrorDto( "An ErrorDto sample specific to /first endpoint" )
85+
)
86+
)
87+
);
88+
break;
89+
90+
case "second" :
91+
examples.put(
92+
"Second case example",
93+
new Example().value(
94+
defaultObjectMapper.valueToTree(
95+
new ErrorDto( "An ErrorDto sample specific to /second endpoint" )
96+
)
97+
)
98+
);
99+
break;
100+
}
101+
102+
return operation;
103+
};
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app199;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
27+
28+
public class SpringDocApp199Test extends AbstractSpringDocV30Test {
29+
30+
@SpringBootApplication
31+
static class SpringDocTestApp {}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}}}}}}

0 commit comments

Comments
 (0)