Skip to content

Commit 9d6fa76

Browse files
committed
fix for extra square brackets
1 parent 6c0c54d commit 9d6fa76

3 files changed

Lines changed: 165 additions & 32 deletions

File tree

src/main/java/io/swagger/oas/inflector/examples/ExampleBuilder.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,7 @@
5252
import java.text.DateFormat;
5353
import java.text.SimpleDateFormat;
5454
import java.time.OffsetDateTime;
55-
import java.util.ArrayList;
56-
import java.util.Date;
57-
import java.util.HashMap;
58-
import java.util.List;
59-
import java.util.Map;
60-
import java.util.Set;
61-
import java.util.TimeZone;
62-
import java.util.UUID;
55+
import java.util.*;
6356

6457
public class ExampleBuilder {
6558

@@ -446,7 +439,25 @@ public static Example fromProperty(
446439
if (example != null) {
447440
try {
448441
output = Json.mapper().readValue(example.toString(), ObjectExample.class);
449-
} catch (IOException e) {
442+
if(output instanceof ArrayExample) {
443+
ObjectExample out = new ObjectExample();
444+
ArrayExample arrayExample = (ArrayExample) output;
445+
446+
List examples2 = arrayExample.getItems();;
447+
Map<String, Example> examplesMap = new LinkedHashMap();
448+
for (Object exam : examples2) {
449+
if (exam instanceof ObjectExample) {
450+
ObjectExample innerExample = (ObjectExample) exam;
451+
if (innerExample.getValues() != null){
452+
examplesMap.putAll(innerExample.getValues());
453+
}
454+
}
455+
}
456+
out.putAll(examplesMap);
457+
output = out;
458+
}
459+
460+
} catch (IOException e) {
450461
LOGGER.error("unable to convert `" + example + "` to JsonNode");
451462
output = new ObjectExample();
452463
}

src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,15 @@ public void testAdjacentComposedSchema(@Injectable List<AuthorizationValue> auth
869869
ApiResponse responseAdjacent = openAPI.getPaths().get("/adjacent").getGet().getResponses().get("200");
870870
Example exampleAdjacent = ExampleBuilder.fromSchema(responseAdjacent.getContent().get("application/json").getSchema(),null,ExampleBuilder.RequestType.READ);
871871
String outputAdjacent = Json.pretty(exampleAdjacent);
872-
assertEqualsIgnoreLineEnding(outputAdjacent, "[ {\n" +
872+
assertEqualsIgnoreLineEnding(outputAdjacent, "{\n" +
873873
" \"title\" : \"The Hitchhiker's Guide to the Galaxy\",\n" +
874874
" \"authors\" : [ \"Douglas Adams\" ],\n" +
875875
" \"isbn\" : \"0-330-25864-8\"\n" +
876876
"}, {\n" +
877877
" \"title\" : \"Blade Runner\",\n" +
878878
" \"directors\" : [ \"Ridley Scott\" ],\n" +
879879
" \"year\" : 1982\n" +
880-
"} ]");
880+
"}");
881881

882882
}
883883

@@ -1065,26 +1065,23 @@ public void testNullExampleSupportOAS3NoFlag() throws Exception{
10651065
@Test
10661066
public void testAllOfMergeSchemas() throws Exception {
10671067
String expected = "{\n" +
1068-
" \"data\": [\n" +
1069-
" {\n" +
1070-
" \"topApiId\": \"8ab9b11d-bf43-469e-a276-f601801d043c\",\n" +
1071-
" \"name\": \"docman\",\n" +
1072-
" \"pathwaysVersion\": \"0.1\",\n" +
1073-
" \"active\": false,\n" +
1074-
" \"created\": \"2018-07-21T17:32:28Z\"\n" +
1075-
" },\n" +
1076-
" {\n" +
1077-
" \"topApiId\": \"baa0dc91-9711-4b61-9c90-ce8ac0b109a9\",\n" +
1078-
" \"name\": \"careweb\",\n" +
1079-
" \"pathwaysVersion\": \"0.1\",\n" +
1080-
" \"active\": true,\n" +
1081-
" \"created\": \"2019-07-21T17:32:28Z\"\n" +
1082-
" }\n" +
1083-
" ],\n" +
1084-
" \"pagination\": {\n" +
1085-
" \"offset\": 0,\n" +
1086-
" \"limit\": 10,\n" +
1087-
" \"totalResultCount\": 100\n" +
1068+
" \"data\" : [ {\n" +
1069+
" \"topApiId\" : \"8ab9b11d-bf43-469e-a276-f601801d043c\",\n" +
1070+
" \"name\" : \"docman\",\n" +
1071+
" \"pathwaysVersion\" : \"0.1\",\n" +
1072+
" \"active\" : false,\n" +
1073+
" \"created\" : \"2018-07-21T17:32:28Z\"\n" +
1074+
" }, {\n" +
1075+
" \"topApiId\" : \"baa0dc91-9711-4b61-9c90-ce8ac0b109a9\",\n" +
1076+
" \"name\" : \"careweb\",\n" +
1077+
" \"pathwaysVersion\" : \"0.1\",\n" +
1078+
" \"active\" : true,\n" +
1079+
" \"created\" : \"2019-07-21T17:32:28Z\"\n" +
1080+
" } ],\n" +
1081+
" \"pagination\" : {\n" +
1082+
" \"offset\" : 0,\n" +
1083+
" \"limit\" : 10,\n" +
1084+
" \"totalResultCount\" : 100\n" +
10881085
" }\n" +
10891086
"}";
10901087
ParseOptions options = new ParseOptions();
@@ -1096,6 +1093,6 @@ public void testAllOfMergeSchemas() throws Exception {
10961093
ApiResponse response = openAPI.getPaths().get("/topApis").getGet().getResponses().get("200");
10971094

10981095
Example example = ExampleBuilder.fromSchema(response.getContent().get("application/json").getSchema(), null, ExampleBuilder.RequestType.READ);
1099-
assertEquals(expected, Json.pretty(example));
1096+
assertEquals(Json.pretty(example), expected);
11001097
}
11011098
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
openapi: 3.0.0
2+
info:
3+
version: "minimal-repro"
4+
title: TopApis API
5+
paths:
6+
"/topApis":
7+
get:
8+
tags:
9+
- TopApi
10+
summary: List TopApis
11+
operationId: listTopApis
12+
parameters:
13+
- in: query
14+
name: includeInactive
15+
schema:
16+
type: boolean
17+
required: false
18+
example: false
19+
- in: query
20+
name: filterByName
21+
schema:
22+
type: string
23+
required: false
24+
example: 'ada'
25+
- in: query
26+
name: offset
27+
schema:
28+
type: integer
29+
minimum: 0
30+
default: 0
31+
required: false
32+
example: 5
33+
- in: query
34+
name: limit
35+
schema:
36+
type: integer
37+
minimum: 1
38+
default: 10
39+
required: false
40+
example: 10
41+
responses:
42+
'200':
43+
description: a description
44+
content:
45+
application/json:
46+
schema:
47+
$ref: '#/components/schemas/ListOfTopApis'
48+
components:
49+
schemas:
50+
TopApi:
51+
type: object
52+
required:
53+
- name
54+
- pathwaysVersion
55+
additionalProperties: false
56+
properties:
57+
topApiId:
58+
type: string
59+
format: uuid
60+
readOnly: true
61+
example: '884c0363-bbbe-4a5e-8d4f-1d34910803fc'
62+
name:
63+
type: string
64+
example: 'adastra'
65+
pathwaysVersion:
66+
type: string
67+
example: '0.1'
68+
active:
69+
type: boolean
70+
readOnly: true
71+
example: true
72+
created:
73+
type: string
74+
format: date-time
75+
readOnly: true
76+
example: '2017-07-21T17:32:28Z'
77+
ListOfTopApis:
78+
type: object
79+
additionalProperties: false
80+
properties:
81+
data:
82+
type: array
83+
items:
84+
$ref: '#/components/schemas/TopApi'
85+
example:
86+
data:
87+
- topApiId: '8ab9b11d-bf43-469e-a276-f601801d043c'
88+
name: 'docman'
89+
pathwaysVersion: '0.1'
90+
active: false
91+
created: '2018-07-21T17:32:28Z'
92+
- topApiId: 'baa0dc91-9711-4b61-9c90-ce8ac0b109a9'
93+
name: 'careweb'
94+
pathwaysVersion: '0.1'
95+
active: true
96+
created: '2019-07-21T17:32:28Z'
97+
Pagination:
98+
type: object
99+
properties:
100+
pagination:
101+
type: object
102+
properties:
103+
offset:
104+
type: integer
105+
minimum: 0
106+
default: 0
107+
example: 5
108+
limit:
109+
type: integer
110+
example: 20
111+
totalResultCount:
112+
type: integer
113+
example: 521
114+
example:
115+
pagination:
116+
offset: 0
117+
limit: 10
118+
totalResultCount: 100
119+
requestBodies:
120+
TopApi:
121+
required: true
122+
content:
123+
application/json:
124+
schema:
125+
$ref: '#/components/schemas/TopApi'

0 commit comments

Comments
 (0)