Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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"));
}

Expand Down Expand Up @@ -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<String, Schema> definitions = openAPI.getComponents().getSchemas();
Expand Down
69 changes: 69 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue-1015.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
}
}
}