From 9d6fa76935eb89013d4174f7d11a815a3fb9c95f Mon Sep 17 00:00:00 2001 From: gracekarina Date: Thu, 25 Feb 2021 10:41:18 -0500 Subject: [PATCH] fix for extra square brackets --- .../inflector/examples/ExampleBuilder.java | 29 ++-- .../oas/test/examples/ExampleBuilderTest.java | 43 +++--- src/test/swagger/issue-22849_noAllOf.yaml | 125 ++++++++++++++++++ 3 files changed, 165 insertions(+), 32 deletions(-) create mode 100644 src/test/swagger/issue-22849_noAllOf.yaml diff --git a/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java b/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java index 9e69a4d4..1f607363 100644 --- a/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java +++ b/src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java @@ -52,14 +52,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.OffsetDateTime; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TimeZone; -import java.util.UUID; +import java.util.*; public class ExampleBuilder { @@ -446,7 +439,25 @@ public static Example fromProperty( if (example != null) { try { output = Json.mapper().readValue(example.toString(), ObjectExample.class); - } catch (IOException e) { + if(output instanceof ArrayExample) { + ObjectExample out = new ObjectExample(); + ArrayExample arrayExample = (ArrayExample) output; + + List examples2 = arrayExample.getItems();; + Map examplesMap = new LinkedHashMap(); + for (Object exam : examples2) { + if (exam instanceof ObjectExample) { + ObjectExample innerExample = (ObjectExample) exam; + if (innerExample.getValues() != null){ + examplesMap.putAll(innerExample.getValues()); + } + } + } + out.putAll(examplesMap); + output = out; + } + + } catch (IOException e) { LOGGER.error("unable to convert `" + example + "` to JsonNode"); output = new ObjectExample(); } diff --git a/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java b/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java index 064aa431..771cd8f5 100644 --- a/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java +++ b/src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java @@ -869,7 +869,7 @@ public void testAdjacentComposedSchema(@Injectable List auth ApiResponse responseAdjacent = openAPI.getPaths().get("/adjacent").getGet().getResponses().get("200"); Example exampleAdjacent = ExampleBuilder.fromSchema(responseAdjacent.getContent().get("application/json").getSchema(),null,ExampleBuilder.RequestType.READ); String outputAdjacent = Json.pretty(exampleAdjacent); - assertEqualsIgnoreLineEnding(outputAdjacent, "[ {\n" + + assertEqualsIgnoreLineEnding(outputAdjacent, "{\n" + " \"title\" : \"The Hitchhiker's Guide to the Galaxy\",\n" + " \"authors\" : [ \"Douglas Adams\" ],\n" + " \"isbn\" : \"0-330-25864-8\"\n" + @@ -877,7 +877,7 @@ public void testAdjacentComposedSchema(@Injectable List auth " \"title\" : \"Blade Runner\",\n" + " \"directors\" : [ \"Ridley Scott\" ],\n" + " \"year\" : 1982\n" + - "} ]"); + "}"); } @@ -1065,26 +1065,23 @@ public void testNullExampleSupportOAS3NoFlag() throws Exception{ @Test public void testAllOfMergeSchemas() throws Exception { String expected = "{\n" + - " \"data\": [\n" + - " {\n" + - " \"topApiId\": \"8ab9b11d-bf43-469e-a276-f601801d043c\",\n" + - " \"name\": \"docman\",\n" + - " \"pathwaysVersion\": \"0.1\",\n" + - " \"active\": false,\n" + - " \"created\": \"2018-07-21T17:32:28Z\"\n" + - " },\n" + - " {\n" + - " \"topApiId\": \"baa0dc91-9711-4b61-9c90-ce8ac0b109a9\",\n" + - " \"name\": \"careweb\",\n" + - " \"pathwaysVersion\": \"0.1\",\n" + - " \"active\": true,\n" + - " \"created\": \"2019-07-21T17:32:28Z\"\n" + - " }\n" + - " ],\n" + - " \"pagination\": {\n" + - " \"offset\": 0,\n" + - " \"limit\": 10,\n" + - " \"totalResultCount\": 100\n" + + " \"data\" : [ {\n" + + " \"topApiId\" : \"8ab9b11d-bf43-469e-a276-f601801d043c\",\n" + + " \"name\" : \"docman\",\n" + + " \"pathwaysVersion\" : \"0.1\",\n" + + " \"active\" : false,\n" + + " \"created\" : \"2018-07-21T17:32:28Z\"\n" + + " }, {\n" + + " \"topApiId\" : \"baa0dc91-9711-4b61-9c90-ce8ac0b109a9\",\n" + + " \"name\" : \"careweb\",\n" + + " \"pathwaysVersion\" : \"0.1\",\n" + + " \"active\" : true,\n" + + " \"created\" : \"2019-07-21T17:32:28Z\"\n" + + " } ],\n" + + " \"pagination\" : {\n" + + " \"offset\" : 0,\n" + + " \"limit\" : 10,\n" + + " \"totalResultCount\" : 100\n" + " }\n" + "}"; ParseOptions options = new ParseOptions(); @@ -1096,6 +1093,6 @@ public void testAllOfMergeSchemas() throws Exception { ApiResponse response = openAPI.getPaths().get("/topApis").getGet().getResponses().get("200"); Example example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ); - assertEquals(expected, Json.pretty(example)); + assertEquals(Json.pretty(example), expected); } } diff --git a/src/test/swagger/issue-22849_noAllOf.yaml b/src/test/swagger/issue-22849_noAllOf.yaml new file mode 100644 index 00000000..3e22c9ce --- /dev/null +++ b/src/test/swagger/issue-22849_noAllOf.yaml @@ -0,0 +1,125 @@ +openapi: 3.0.0 +info: + version: "minimal-repro" + title: TopApis API +paths: + "/topApis": + get: + tags: + - TopApi + summary: List TopApis + operationId: listTopApis + parameters: + - in: query + name: includeInactive + schema: + type: boolean + required: false + example: false + - in: query + name: filterByName + schema: + type: string + required: false + example: 'ada' + - in: query + name: offset + schema: + type: integer + minimum: 0 + default: 0 + required: false + example: 5 + - in: query + name: limit + schema: + type: integer + minimum: 1 + default: 10 + required: false + example: 10 + responses: + '200': + description: a description + content: + application/json: + schema: + $ref: '#/components/schemas/ListOfTopApis' +components: + schemas: + TopApi: + type: object + required: + - name + - pathwaysVersion + additionalProperties: false + properties: + topApiId: + type: string + format: uuid + readOnly: true + example: '884c0363-bbbe-4a5e-8d4f-1d34910803fc' + name: + type: string + example: 'adastra' + pathwaysVersion: + type: string + example: '0.1' + active: + type: boolean + readOnly: true + example: true + created: + type: string + format: date-time + readOnly: true + example: '2017-07-21T17:32:28Z' + ListOfTopApis: + type: object + additionalProperties: false + properties: + data: + type: array + items: + $ref: '#/components/schemas/TopApi' + example: + data: + - topApiId: '8ab9b11d-bf43-469e-a276-f601801d043c' + name: 'docman' + pathwaysVersion: '0.1' + active: false + created: '2018-07-21T17:32:28Z' + - topApiId: 'baa0dc91-9711-4b61-9c90-ce8ac0b109a9' + name: 'careweb' + pathwaysVersion: '0.1' + active: true + created: '2019-07-21T17:32:28Z' + Pagination: + type: object + properties: + pagination: + type: object + properties: + offset: + type: integer + minimum: 0 + default: 0 + example: 5 + limit: + type: integer + example: 20 + totalResultCount: + type: integer + example: 521 + example: + pagination: + offset: 0 + limit: 10 + totalResultCount: 100 + requestBodies: + TopApi: + required: true + content: + application/json: + schema: + $ref: '#/components/schemas/TopApi' \ No newline at end of file