Skip to content

Commit 62912a1

Browse files
authored
Merge pull request #2801 from swagger-api/issue-2319-apiresponse-examples-annotation
Refs #2319 - response example support
2 parents 1558df6 + e093481 commit 62912a1

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

modules/swagger-annotations/src/main/java/io/swagger/annotations/ApiResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,13 @@
8585
* Valid values are "List", "Set" or "Map". Any other value will be ignored.
8686
*/
8787
String responseContainer() default "";
88+
89+
/**
90+
* Examples for the response.
91+
*
92+
* @since 1.5.20
93+
*
94+
* @return
95+
*/
96+
Example examples() default @Example(value = @ExampleProperty(value = "", mediaType = ""));
8897
}

modules/swagger-annotations/src/main/java/io/swagger/annotations/ExampleProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @return the name of the property
3838
*/
39-
String mediaType() default "default";
39+
String mediaType() default "";
4040

4141
/**
4242
* The value of the example.

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/Reader.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import io.swagger.annotations.Authorization;
3434
import io.swagger.annotations.AuthorizationScope;
3535
import io.swagger.annotations.BasicAuthDefinition;
36+
import io.swagger.annotations.Example;
37+
import io.swagger.annotations.ExampleProperty;
3638
import io.swagger.annotations.Info;
3739
import io.swagger.annotations.OAuth2Definition;
3840
import io.swagger.annotations.ResponseHeader;
@@ -1007,9 +1009,12 @@ private void processOperationDecorator(Operation operation, Method method) {
10071009

10081010
private void addResponse(Operation operation, ApiResponse apiResponse, JsonView jsonView) {
10091011
Map<String, Property> responseHeaders = parseResponseHeaders(apiResponse.responseHeaders(), jsonView);
1012+
Map<String, Object> examples = parseExamples(apiResponse.examples());
10101013

10111014
Response response = new Response()
1012-
.description(apiResponse.message()).headers(responseHeaders);
1015+
.description(apiResponse.message())
1016+
.headers(responseHeaders);
1017+
response.setExamples(examples);
10131018

10141019
if (apiResponse.code() == 0) {
10151020
operation.defaultResponse(response);
@@ -1029,6 +1034,24 @@ private void addResponse(Operation operation, ApiResponse apiResponse, JsonView
10291034
}
10301035
}
10311036

1037+
private Map<String, Object> parseExamples(Example examples) {
1038+
if(examples == null){
1039+
return null;
1040+
}
1041+
1042+
Map<String, Object> map = null;
1043+
for(ExampleProperty prop : examples.value()){
1044+
1045+
if(prop.mediaType().equals("") && prop.value().equals("")){
1046+
continue;
1047+
}
1048+
1049+
map = map == null ? new LinkedHashMap<String, Object>() : map;
1050+
map.put(prop.mediaType(), prop.value());
1051+
}
1052+
return map;
1053+
}
1054+
10321055
private List<Parameter> getParameters(Type type, List<Annotation> annotations) {
10331056
final Iterator<SwaggerExtension> chain = SwaggerExtensions.chain();
10341057
if (!chain.hasNext()) {

modules/swagger-jaxrs/src/test/java/io/swagger/SimpleReaderTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import io.swagger.resources.ResourceWithMapReturnValue;
5151
import io.swagger.resources.ResourceWithRanges;
5252
import io.swagger.resources.ResourceWithResponse;
53+
import io.swagger.resources.ResourceWithResponseExamples;
5354
import io.swagger.resources.ResourceWithResponseHeaders;
5455
import io.swagger.resources.ResourceWithTypedResponses;
5556
import io.swagger.resources.ResourceWithVoidReturns;
@@ -585,6 +586,30 @@ public void checkResponseModelsProcessing() {
585586
}
586587
}
587588

589+
@Test(description = "test response examples")
590+
public void testResponseExamples() {
591+
Swagger swagger = getSwagger(ResourceWithResponseExamples.class);
592+
for (Map.Entry<String, Path> entry : swagger.getPaths().entrySet()) {
593+
String name = entry.getKey().substring(entry.getKey().lastIndexOf("/") + 1);
594+
if ("testPrimitiveResponses".equals(name)) {
595+
Map<String, String[]> expected = ImmutableMap.of("404", new String[]{"string", null});
596+
assertEquals(entry.getValue().getGet().getResponses().size(), expected.size());
597+
for (Map.Entry<String, Response> responseEntry : entry.getValue().getGet().getResponses().entrySet()) {
598+
String[] expectedProp = expected.get(responseEntry.getKey());
599+
Model model = responseEntry.getValue().getResponseSchema();
600+
ModelImpl modelImpl = (ModelImpl) model;
601+
assertEquals(modelImpl.getType(), expectedProp[0]);
602+
assertEquals(modelImpl.getFormat(), expectedProp[1]);
603+
Response response = responseEntry.getValue();
604+
assertEquals(response.getExamples().size(), 2);
605+
assertEquals(response.getExamples().get("*/*").toString(), "message example 1");
606+
assertEquals(response.getExamples().get("application/json").toString(), "message example 2");
607+
}
608+
}
609+
}
610+
}
611+
612+
588613
@Test(description = "scan a resource with custom operation nickname")
589614
public void scanResourceWithApiOperationNickname() {
590615
Swagger swagger = getSwagger(NicknamedOperation.class);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.swagger.resources;
2+
3+
import io.swagger.annotations.Api;
4+
import io.swagger.annotations.ApiResponse;
5+
import io.swagger.annotations.ApiResponses;
6+
import io.swagger.annotations.Example;
7+
import io.swagger.annotations.ExampleProperty;
8+
9+
import javax.ws.rs.GET;
10+
import javax.ws.rs.Path;
11+
import javax.ws.rs.core.Response;
12+
13+
@Api(value = "/root")
14+
@Path("/")
15+
public class ResourceWithResponseExamples {
16+
17+
@GET
18+
@Path("testPrimitiveResponses")
19+
@ApiResponses({
20+
@ApiResponse(
21+
code = 404,
22+
message = "Message for String",
23+
response = String.class,
24+
examples = @Example(value =
25+
{
26+
@ExampleProperty(mediaType = "*/*", value = "message example 1"),
27+
@ExampleProperty(mediaType = "application/json", value = "message example 2")
28+
}
29+
))
30+
})
31+
public Response testPrimitiveResponses() {
32+
return null;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)