Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate example for $ref'd array schemas #182

Merged
merged 1 commit into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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