Skip to content

Commit e093481

Browse files
committed
refs #2319 - response example support: tests and fixes
1 parent 749665b commit e093481

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,12 @@
8686
*/
8787
String responseContainer() default "";
8888

89+
/**
90+
* Examples for the response.
91+
*
92+
* @since 1.5.20
93+
*
94+
* @return
95+
*/
8996
Example examples() default @Example(value = @ExampleProperty(value = "", mediaType = ""));
9097
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@
2323
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
2424
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
2525
import com.fasterxml.jackson.databind.type.TypeFactory;
26-
import io.swagger.annotations.*;
26+
import io.swagger.annotations.Api;
27+
import io.swagger.annotations.ApiImplicitParam;
28+
import io.swagger.annotations.ApiImplicitParams;
29+
import io.swagger.annotations.ApiKeyAuthDefinition;
30+
import io.swagger.annotations.ApiOperation;
31+
import io.swagger.annotations.ApiResponse;
32+
import io.swagger.annotations.ApiResponses;
33+
import io.swagger.annotations.Authorization;
34+
import io.swagger.annotations.AuthorizationScope;
35+
import io.swagger.annotations.BasicAuthDefinition;
36+
import io.swagger.annotations.Example;
37+
import io.swagger.annotations.ExampleProperty;
38+
import io.swagger.annotations.Info;
39+
import io.swagger.annotations.OAuth2Definition;
40+
import io.swagger.annotations.ResponseHeader;
41+
import io.swagger.annotations.Scope;
42+
import io.swagger.annotations.SwaggerDefinition;
2743
import io.swagger.converter.ModelConverters;
2844
import io.swagger.jaxrs.config.DefaultReaderConfig;
2945
import io.swagger.jaxrs.config.ReaderConfig;
@@ -996,7 +1012,9 @@ private void addResponse(Operation operation, ApiResponse apiResponse, JsonView
9961012
Map<String, Object> examples = parseExamples(apiResponse.examples());
9971013

9981014
Response response = new Response()
999-
.description(apiResponse.message()).headers(responseHeaders).setExamples(examples);
1015+
.description(apiResponse.message())
1016+
.headers(responseHeaders);
1017+
response.setExamples(examples);
10001018

10011019
if (apiResponse.code() == 0) {
10021020
operation.defaultResponse(response);
@@ -1028,7 +1046,7 @@ private Map<String, Object> parseExamples(Example examples) {
10281046
continue;
10291047
}
10301048

1031-
map = map == null ? new HashMap<String, Object>() : map;
1049+
map = map == null ? new LinkedHashMap<String, Object>() : map;
10321050
map.put(prop.mediaType(), prop.value());
10331051
}
10341052
return map;

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+
}

modules/swagger-models/src/main/java/io/swagger/models/Response.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ public Map<String, Object> getExamples() {
9191
return this.examples;
9292
}
9393

94-
public Response setExamples(Map<String, Object> examples) {
94+
public void setExamples(Map<String, Object> examples) {
9595
this.examples = examples;
96-
return this;
9796
}
9897

9998
public Map<String, Property> getHeaders() {

0 commit comments

Comments
 (0)