diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index 7964c027..328edb88 100644 --- a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java @@ -325,7 +325,12 @@ public static Example fromProperty(Property property, Map definit } } else if (property instanceof ArrayProperty) { if (example != null) { - output = new ArrayExample(); + try { + output = Json.mapper().readValue(example.toString(), ArrayExample.class); + } catch (IOException e) { + LOGGER.error("unable to convert `" + example + "` to JsonNode"); + output = new ArrayExample(); + } } else { ArrayProperty ap = (ArrayProperty) property; @@ -527,7 +532,6 @@ else if(model instanceof ComposedModel) { } else if(model instanceof ArrayModel) { ArrayModel am = (ArrayModel) model; - ObjectExample ex = new ObjectExample(); Property inner = am.getItems(); if (inner != null) { diff --git a/src/main/java/io/swagger/inflector/examples/models/ArrayExample.java b/src/main/java/io/swagger/inflector/examples/models/ArrayExample.java index 86ab0193..18943923 100644 --- a/src/main/java/io/swagger/inflector/examples/models/ArrayExample.java +++ b/src/main/java/io/swagger/inflector/examples/models/ArrayExample.java @@ -16,9 +16,13 @@ package io.swagger.inflector.examples.models; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.swagger.inflector.processors.JsonExampleDeserializer; + import java.util.ArrayList; import java.util.List; +@JsonDeserialize(using = JsonExampleDeserializer.class) public class ArrayExample extends AbstractExample { List values = null; diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index a18bd318..c9188ca0 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -576,6 +576,20 @@ public void testIssue1177RefArrayExample() throws Exception { assertEqualsIgnoreLineEnding(output, "[ \"string\" ]"); } + @Test + public void testInlinedArrayExample() throws Exception { + Swagger swagger = new SwaggerParser().read("src/test/swagger/array-example.yaml"); + + String output = getExampleForPath(swagger, "/"); + assertEqualsIgnoreLineEnding(output, "[ {\n" + + " \"id\" : 1,\n" + + " \"name\" : \"Arthur Dent\"\n" + + "}, {\n" + + " \"id\" : 2,\n" + + " \"name\" : \"Ford Prefect\"\n" + + "} ]"); + } + @Test public void testIssue1263SchemaExampleNestedObjects() throws Exception { Swagger swagger = new SwaggerParser().read("src/test/swagger/issue-1263.yaml"); diff --git a/src/test/swagger/array-example.yaml b/src/test/swagger/array-example.yaml new file mode 100644 index 00000000..2b9f7823 --- /dev/null +++ b/src/test/swagger/array-example.yaml @@ -0,0 +1,32 @@ +swagger: '2.0' +info: + version: '1.0' + title: Array example + +paths: + /: + get: + produces: + - application/json + responses: + 200: + description: completed successfully + schema: + type: array + items: + $ref: '#/definitions/User' + example: + - id: 1 + name: Arthur Dent + - id: 2 + name: Ford Prefect +definitions: + User: + type: object + properties: + id: + type: integer + example: 0 + name: + type: string + example: Marvin \ No newline at end of file