Skip to content

Commit 56ab4d9

Browse files
author
bnasslahsen
committed
Warning on referenced example. Fixes #437
1 parent 1c979b8 commit 56ab4d9

File tree

8 files changed

+567
-2
lines changed

8 files changed

+567
-2
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocAnnotationsUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,13 @@ private static void setExamples(MediaType mediaType, ExampleObject[] examples) {
135135
}
136136
else {
137137
for (ExampleObject example : examples) {
138-
getExample(example).ifPresent(exampleObject -> mediaType.addExamples(example.name(), exampleObject));
138+
getExample(example).ifPresent(exampleObject -> {
139+
if (exampleObject.get$ref() != null)
140+
//Ignore description
141+
exampleObject.setDescription(null);
142+
mediaType.addExamples(example.name(), exampleObject);
143+
}
144+
);
139145
}
140146
}
141147
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/SpringDocDataRestConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import io.swagger.v3.oas.models.media.ObjectSchema;
2929
import io.swagger.v3.oas.models.media.StringSchema;
3030
import org.springdoc.core.converters.Pageable;
31-
import org.springdoc.core.customizers.OpenApiCustomiser;
3231
import org.springdoc.core.converters.RepresentationModelLinksOASMixin;
32+
import org.springdoc.core.customizers.OpenApiCustomiser;
3333

3434
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3535
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app90;
20+
21+
import io.swagger.v3.oas.annotations.media.Content;
22+
import io.swagger.v3.oas.annotations.media.ExampleObject;
23+
import io.swagger.v3.oas.annotations.media.Schema;
24+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
25+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
26+
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.RestController;
29+
30+
@RestController
31+
public class HelloController {
32+
33+
@GetMapping("/test")
34+
@ApiResponses(value = { @ApiResponse(description = "successful operation", content = { @Content(examples = @ExampleObject(name = "500", ref = "#/components/examples/http500Example"), mediaType = "application/json", schema = @Schema(implementation = User.class)), @Content(mediaType = "application/xml", schema = @Schema(implementation = User.class)) }) })
35+
public void test1(String hello) {
36+
}
37+
38+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app90;
20+
21+
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
public class SpringDocApp90Test extends AbstractSpringDocTest {
25+
26+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app90;
20+
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.io.Reader;
24+
import java.io.UncheckedIOException;
25+
import java.util.AbstractMap;
26+
import java.util.Collection;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
29+
30+
import io.swagger.v3.oas.models.examples.Example;
31+
import org.springdoc.core.customizers.OpenApiCustomiser;
32+
33+
import org.springframework.beans.factory.annotation.Value;
34+
import org.springframework.boot.autoconfigure.SpringBootApplication;
35+
import org.springframework.context.annotation.Bean;
36+
import org.springframework.core.io.Resource;
37+
import org.springframework.util.FileCopyUtils;
38+
39+
@SpringBootApplication
40+
class SpringDocTestApp {
41+
42+
@Value("classpath:/500-90.json")
43+
private Resource http500ExampleResource;
44+
45+
46+
@Bean
47+
public OpenApiCustomiser openApiCustomiser(Collection<Entry<String, Example>> examples) {
48+
return openAPI -> {
49+
examples.forEach(example -> {
50+
openAPI.getComponents().addExamples(example.getKey(), example.getValue());
51+
});
52+
};
53+
}
54+
55+
@Bean
56+
public Map.Entry<String, Example> http500Example() {
57+
Example http500Example = new Example();
58+
Map.Entry<String, Example> entry = new AbstractMap.SimpleEntry<String, Example>("http500Example", http500Example);
59+
http500Example.setSummary("HTTP 500 JSON Body response example");
60+
http500Example.setDescription(
61+
"An example of HTTP response in case an error occurs on server side. instance attribute reference a traceId to ease server side analysis.");
62+
http500Example.setValue(asString(http500ExampleResource));
63+
return entry;
64+
}
65+
66+
public static String asString(Resource resource) {
67+
try (Reader reader = new InputStreamReader(resource.getInputStream())) {
68+
return FileCopyUtils.copyToString(reader);
69+
} catch (IOException e) {
70+
throw new UncheckedIOException(e);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)