Skip to content

Commit

Permalink
Merge pull request #182 from joeljons/issue-1177-ref-array-example
Browse files Browse the repository at this point in the history
Generate example for $ref'd array schemas
  • Loading branch information
fehguy authored Jan 23, 2017
2 parents efe2353 + 6f288fa commit 5fda599
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/io/swagger/inflector/examples/ExampleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.swagger.inflector.examples.models.LongExample;
import io.swagger.inflector.examples.models.ObjectExample;
import io.swagger.inflector.examples.models.StringExample;
import io.swagger.models.ArrayModel;
import io.swagger.models.ComposedModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
Expand Down Expand Up @@ -523,6 +524,20 @@ else if(model instanceof ComposedModel) {
mergeTo(ex, innerExamples);
output = ex;
}
else if(model instanceof ArrayModel) {
ArrayModel am = (ArrayModel) model;
ObjectExample ex = new ObjectExample();

Property inner = am.getItems();
if (inner != null) {
Example innerExample = fromProperty(inner, definitions, processedModels);
if (innerExample != null) {
ArrayExample an = new ArrayExample();
an.add(innerExample);
output = an;
}
}
}
if (output != null) {
if (attribute != null) {
output.setAttribute(attribute);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/io/swagger/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,15 @@ public void testIssue1261InlineSchemaExample() throws Exception {
" \"name\" : \"Arthur Dent\"\n" +
"}");
}

@Test
public void testIssue1177RefArrayExample() throws Exception {
Swagger swagger = new SwaggerParser().read("src/test/swagger/issue-1177.yaml");

Response response = swagger.getPath("/array").getGet().getResponses().get("200");
Example example = ExampleBuilder.fromProperty(response.getSchema(), swagger.getDefinitions());

String output = Json.pretty(example);
assertEqualsIgnoreLineEnding(output, "[ \"string\" ]");
}
}
22 changes: 22 additions & 0 deletions src/test/swagger/issue-1177.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
swagger: '2.0'
info:
version: 1.0.0
title: Array Mocking Test

produces:
- application/json

paths:
/array:
get:
responses:
200:
description: OK
schema:
$ref: "#/definitions/AnArray"

definitions:
AnArray:
type: array
items:
type: string

0 comments on commit 5fda599

Please sign in to comment.