Skip to content

Commit

Permalink
fix for extra square brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
gracekarina committed Feb 25, 2021
1 parent 6c0c54d commit 9d6fa76
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<String, Example> 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();
}
Expand Down
43 changes: 20 additions & 23 deletions src/test/java/io/swagger/oas/test/examples/ExampleBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,15 @@ public void testAdjacentComposedSchema(@Injectable List<AuthorizationValue> 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" +
"}, {\n" +
" \"title\" : \"Blade Runner\",\n" +
" \"directors\" : [ \"Ridley Scott\" ],\n" +
" \"year\" : 1982\n" +
"} ]");
"}");

}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
125 changes: 125 additions & 0 deletions src/test/swagger/issue-22849_noAllOf.yaml
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 9d6fa76

Please sign in to comment.