From 6f288fa1c0b24813585b7799e16238c51a7ec531 Mon Sep 17 00:00:00 2001 From: Joel Jonsson Date: Wed, 28 Dec 2016 13:33:03 +0100 Subject: [PATCH] Generate example for $ref'd array schemas --- .../inflector/examples/ExampleBuilder.java | 15 +++++++++++++ .../test/examples/ExampleBuilderTest.java | 11 ++++++++++ src/test/swagger/issue-1177.yaml | 22 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/test/swagger/issue-1177.yaml diff --git a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java index 25828cd7..17f72416 100644 --- a/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/inflector/examples/ExampleBuilder.java @@ -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; @@ -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); diff --git a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java index fb1de935..67b51665 100644 --- a/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/test/examples/ExampleBuilderTest.java @@ -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\" ]"); + } } diff --git a/src/test/swagger/issue-1177.yaml b/src/test/swagger/issue-1177.yaml new file mode 100644 index 00000000..cbde132f --- /dev/null +++ b/src/test/swagger/issue-1177.yaml @@ -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 \ No newline at end of file