From c7021bd9988aed7ef2fad79326cf7e078f408a09 Mon Sep 17 00:00:00 2001 From: gracekarina Date: Fri, 15 Feb 2019 11:17:44 -0500 Subject: [PATCH] fix #1015 and test --- .../v3/parser/util/OpenAPIDeserializer.java | 39 +++++++++-- .../v3/parser/test/OpenAPIV3ParserTest.java | 19 ++++- .../src/test/resources/issue-1015.json | 69 +++++++++++++++++++ 3 files changed, 120 insertions(+), 7 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1015.json diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java index 5d79ddd77b..d8e52f23e5 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java @@ -1657,6 +1657,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu return header; } + public Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ){ JsonNode example = node.get(nodeKey); if (example != null) { @@ -1673,7 +1674,6 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par BigDecimal bigDecimalExample = getBigDecimal(nodeKey, node, false, location, result); if (bigDecimalExample != null) { return bigDecimalExample; - } } } else if (example.getNodeType().equals(JsonNodeType.OBJECT)) { @@ -1686,6 +1686,11 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par if (arrayValue != null) { return arrayValue; } + } else if (example.getNodeType().equals(JsonNodeType.BOOLEAN)){ + Boolean bool = getBoolean(nodeKey,node,false,location,result); + if (bool != null){ + return bool; + } } } return null; @@ -2250,12 +2255,36 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){ schema.setFormat(value); } - value = getString("default", node, false, location, result); - if (StringUtils.isNotBlank(value)) { - schema.setDefault(value); + //sets default value according to the schema type + if(node.get("default")!= null) { + if(schema.getType().equals("array")) { + ArrayNode array = getArray("default", node, false, location, result); + if (array != null) { + schema.setDefault(array); + } + }else if(schema.getType().equals("string")) { + value = getString("default", node, false, location, result); + if (value != null) { + schema.setDefault(value); + } + }else if(schema.getType().equals("boolean")) { + bool = getBoolean("default", node, false, location, result); + if (bool != null) { + schema.setDefault(bool); + } + }else if(schema.getType().equals("object")) { + Object object = getObject("default", node, false, location, result); + if (object != null) { + schema.setDefault(object); + } + }else if(schema.getType().equals("number")) { + Integer number = getInteger("default", node, false, location, result); + if (number != null) { + schema.setDefault(number); + } + } } - //discriminator bool = getBoolean("nullable", node, false, location, result); if(bool != null) { diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index c0c119d0c0..98d999c217 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -60,6 +60,22 @@ public class OpenAPIV3ParserTest { protected int serverPort = getDynamicPort(); protected WireMockServer wireMockServer; + @Test + public void testIssue1015() { + + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveCombinators(true); + SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1015.json", null, options); + if (parseResult.getMessages() != null && !parseResult.getMessages().isEmpty()) { + parseResult.getMessages().forEach(s -> System.out.println(s)); + fail("Error while loading apispec!"); + } + + OpenAPI apispec = parseResult.getOpenAPI(); + assertNotNull(apispec); + } + @Test public void testIssueIntegerDefault() { OpenAPIV3Parser parser = new OpenAPIV3Parser(); @@ -80,7 +96,6 @@ public void testIssue983() { options.setResolve(true); final OpenAPI openAPI = parser.readLocation("issue-983.yaml", null, options).getOpenAPI(); Assert.assertNotNull(openAPI); - Yaml.prettyPrint(openAPI); Assert.assertNotNull(openAPI.getComponents().getSchemas().get("InventoryId")); } @@ -827,7 +842,7 @@ public void testPetstore() throws Exception { SwaggerParseResult result = parser.readLocation("src/test/resources/petstore.yaml", null, options); assertNotNull(result); - assertTrue(result.getMessages().size()==1); + assertTrue(result.getMessages().size()==2); OpenAPI openAPI = result.getOpenAPI(); Map definitions = openAPI.getComponents().getSchemas(); diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1015.json b/modules/swagger-parser-v3/src/test/resources/issue-1015.json new file mode 100644 index 0000000000..66fc809131 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1015.json @@ -0,0 +1,69 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Testcase", + "version": "1.0", + "description": "This is a testcase to demonstrate the problem with supplying a list as the default value for an array schema." + }, + "paths": { + "/v1/doc/{document_id}/filter": { + "post": { + "operationId": "filterDocument", + "summary": "Filter a document by including only the requested headings and subheadings", + "description": "This operation processes the specified stored document by returning a filtered view of the document according to the section heading tags specified in the filter_settings parameter.", + "parameters": [ + { + "name": "document_id", + "in": "path", + "description": "The id of the stored document to be filtered.", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/DocFilterSettings" + } + } + }, + "description": "An object that defines the filter settings.", + "required": true + }, + "x-codegen-request-body-name": "filterSettings", + "responses": { + "200": { + "description": "It worked." + }, + "default": { + "description": "Rut Roh" + } + } + } + } + }, + "components": { + "schemas": { + "DocFilterSettings": { + "type": "object", + "description": "This model describes the attributes used to filter a document.", + "properties": { + "tag_list": { + "description": "This is the set of tags to include in the output.", + "type": "array", + "items": { + "type": "string" + }, + "default": [ + "h1", + "h2" + ] + } + } + } + } + } +}