Skip to content

Commit 2f17c36

Browse files
authored
Merge pull request #1019 from swagger-api/issue-1015
fix #1015 and test
2 parents b49f9ee + c7021bd commit 2f17c36

File tree

3 files changed

+120
-7
lines changed

3 files changed

+120
-7
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
16571657
return header;
16581658
}
16591659

1660+
16601661
public Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ){
16611662
JsonNode example = node.get(nodeKey);
16621663
if (example != null) {
@@ -1673,7 +1674,6 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
16731674
BigDecimal bigDecimalExample = getBigDecimal(nodeKey, node, false, location, result);
16741675
if (bigDecimalExample != null) {
16751676
return bigDecimalExample;
1676-
16771677
}
16781678
}
16791679
} else if (example.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1686,6 +1686,11 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
16861686
if (arrayValue != null) {
16871687
return arrayValue;
16881688
}
1689+
} else if (example.getNodeType().equals(JsonNodeType.BOOLEAN)){
1690+
Boolean bool = getBoolean(nodeKey,node,false,location,result);
1691+
if (bool != null){
1692+
return bool;
1693+
}
16891694
}
16901695
}
16911696
return null;
@@ -2250,12 +2255,36 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22502255
schema.setFormat(value);
22512256
}
22522257

2253-
value = getString("default", node, false, location, result);
2254-
if (StringUtils.isNotBlank(value)) {
2255-
schema.setDefault(value);
2258+
//sets default value according to the schema type
2259+
if(node.get("default")!= null) {
2260+
if(schema.getType().equals("array")) {
2261+
ArrayNode array = getArray("default", node, false, location, result);
2262+
if (array != null) {
2263+
schema.setDefault(array);
2264+
}
2265+
}else if(schema.getType().equals("string")) {
2266+
value = getString("default", node, false, location, result);
2267+
if (value != null) {
2268+
schema.setDefault(value);
2269+
}
2270+
}else if(schema.getType().equals("boolean")) {
2271+
bool = getBoolean("default", node, false, location, result);
2272+
if (bool != null) {
2273+
schema.setDefault(bool);
2274+
}
2275+
}else if(schema.getType().equals("object")) {
2276+
Object object = getObject("default", node, false, location, result);
2277+
if (object != null) {
2278+
schema.setDefault(object);
2279+
}
2280+
}else if(schema.getType().equals("number")) {
2281+
Integer number = getInteger("default", node, false, location, result);
2282+
if (number != null) {
2283+
schema.setDefault(number);
2284+
}
2285+
}
22562286
}
22572287

2258-
//discriminator
22592288

22602289
bool = getBoolean("nullable", node, false, location, result);
22612290
if(bool != null) {

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ public class OpenAPIV3ParserTest {
6060
protected int serverPort = getDynamicPort();
6161
protected WireMockServer wireMockServer;
6262

63+
@Test
64+
public void testIssue1015() {
65+
66+
ParseOptions options = new ParseOptions();
67+
options.setResolve(true);
68+
options.setResolveCombinators(true);
69+
SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1015.json", null, options);
70+
if (parseResult.getMessages() != null && !parseResult.getMessages().isEmpty()) {
71+
parseResult.getMessages().forEach(s -> System.out.println(s));
72+
fail("Error while loading apispec!");
73+
}
74+
75+
OpenAPI apispec = parseResult.getOpenAPI();
76+
assertNotNull(apispec);
77+
}
78+
6379
@Test
6480
public void testIssueIntegerDefault() {
6581
OpenAPIV3Parser parser = new OpenAPIV3Parser();
@@ -80,7 +96,6 @@ public void testIssue983() {
8096
options.setResolve(true);
8197
final OpenAPI openAPI = parser.readLocation("issue-983.yaml", null, options).getOpenAPI();
8298
Assert.assertNotNull(openAPI);
83-
Yaml.prettyPrint(openAPI);
8499
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("InventoryId"));
85100
}
86101

@@ -827,7 +842,7 @@ public void testPetstore() throws Exception {
827842
SwaggerParseResult result = parser.readLocation("src/test/resources/petstore.yaml", null, options);
828843

829844
assertNotNull(result);
830-
assertTrue(result.getMessages().size()==1);
845+
assertTrue(result.getMessages().size()==2);
831846

832847
OpenAPI openAPI = result.getOpenAPI();
833848
Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Testcase",
5+
"version": "1.0",
6+
"description": "This is a testcase to demonstrate the problem with supplying a list as the default value for an array schema."
7+
},
8+
"paths": {
9+
"/v1/doc/{document_id}/filter": {
10+
"post": {
11+
"operationId": "filterDocument",
12+
"summary": "Filter a document by including only the requested headings and subheadings",
13+
"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.",
14+
"parameters": [
15+
{
16+
"name": "document_id",
17+
"in": "path",
18+
"description": "The id of the stored document to be filtered.",
19+
"required": true,
20+
"schema": {
21+
"type": "string"
22+
}
23+
}
24+
],
25+
"requestBody": {
26+
"content": {
27+
"application/json": {
28+
"schema": {
29+
"$ref": "#/components/schemas/DocFilterSettings"
30+
}
31+
}
32+
},
33+
"description": "An object that defines the filter settings.",
34+
"required": true
35+
},
36+
"x-codegen-request-body-name": "filterSettings",
37+
"responses": {
38+
"200": {
39+
"description": "It worked."
40+
},
41+
"default": {
42+
"description": "Rut Roh"
43+
}
44+
}
45+
}
46+
}
47+
},
48+
"components": {
49+
"schemas": {
50+
"DocFilterSettings": {
51+
"type": "object",
52+
"description": "This model describes the attributes used to filter a document.",
53+
"properties": {
54+
"tag_list": {
55+
"description": "This is the set of tags to include in the output.",
56+
"type": "array",
57+
"items": {
58+
"type": "string"
59+
},
60+
"default": [
61+
"h1",
62+
"h2"
63+
]
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)