Skip to content

Commit 91b96f3

Browse files
authored
Merge pull request #923 from r-sreesaran/branch_test
Fix for issue 922
2 parents e301aa9 + 2a307ae commit 91b96f3

File tree

3 files changed

+223
-7
lines changed

3 files changed

+223
-7
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,11 @@ public String getString(String key, ObjectNode node, boolean required, String lo
766766
return getString(key, node, required, location, result, null);
767767
}
768768

769+
public String getStringWithDefaultValueSet(String key, ObjectNode node, boolean required, String location, ParseResult result) {
770+
771+
return getString(key, node, required, location, result, null);
772+
}
773+
769774
public Set<String> getKeys(ObjectNode node) {
770775
Set<String> keys = new LinkedHashSet<>();
771776
if (node == null) {
@@ -1320,6 +1325,23 @@ public Boolean getBoolean(String key, ObjectNode node, boolean required, String
13201325
return value;
13211326
}
13221327

1328+
public Boolean getBooleanWithDefaultValue(String key, ObjectNode node, String location) {
1329+
Boolean value = null;
1330+
JsonNode v = node.get(key);
1331+
if (node == null || v == null) {
1332+
return false;
1333+
} else {
1334+
if (v.getNodeType().equals(JsonNodeType.BOOLEAN)) {
1335+
value = v.asBoolean();
1336+
} else if (v.getNodeType().equals(JsonNodeType.STRING)) {
1337+
String stringValue = v.textValue();
1338+
return Boolean.parseBoolean(stringValue);
1339+
}
1340+
}
1341+
return value;
1342+
}
1343+
1344+
13231345
public BigDecimal getBigDecimal(String key, ObjectNode node, boolean required, String location, ParseResult result) {
13241346
BigDecimal value = null;
13251347
JsonNode v = node.get(key);
@@ -1474,7 +1496,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
14741496
parameter.setDescription(value);
14751497
}
14761498

1477-
Boolean required = getBoolean("required", obj, false, location, result);
1499+
Boolean required = getBooleanWithDefaultValue("required", obj, location);
14781500
if (required != null) {
14791501
parameter.setRequired(required);
14801502
}else {
@@ -2234,6 +2256,7 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22342256
schema.setDefault(value);
22352257
}
22362258

2259+
22372260
//discriminator
22382261

22392262
bool = getBoolean("nullable", node, false, location, result);
@@ -2272,11 +2295,12 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22722295
schema.setExample(example);
22732296
}
22742297

2275-
bool = getBoolean("deprecated", node, false, location, result);
2298+
bool = getBooleanWithDefaultValue("deprecated", node, location);
22762299
if(bool != null){
22772300
schema.setDeprecated(bool);
22782301
}
22792302

2303+
22802304
Map <String,Object> extensions = getExtensions(node);
22812305
if(extensions != null && extensions.size() > 0) {
22822306
schema.setExtensions(extensions);
@@ -2597,7 +2621,7 @@ public Operation getOperation(ObjectNode obj, String location, ParseResult resul
25972621
operation.setCallbacks(callbacks);
25982622
}
25992623

2600-
Boolean deprecated = getBoolean("deprecated", obj, false, location, result);
2624+
Boolean deprecated = getBooleanWithDefaultValue("deprecated", obj, location);
26012625
if (deprecated != null) {
26022626
operation.setDeprecated(deprecated);
26032627
}

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44

5+
6+
import com.sun.org.apache.xpath.internal.operations.Bool;
57
import io.swagger.v3.core.util.Yaml;
68
import io.swagger.v3.oas.models.Components;
79
import io.swagger.v3.oas.models.OpenAPI;
@@ -22,10 +24,7 @@
2224

2325
import java.util.List;
2426

25-
26-
import static org.testng.Assert.assertEquals;
27-
import static org.testng.Assert.assertNotNull;
28-
import static org.testng.Assert.assertTrue;
27+
import static org.testng.Assert.*;
2928

3029
public class OpenAPIParserTest {
3130
@Test
@@ -462,4 +461,14 @@ public void testIssue879() {
462461
assertEquals(ref, "#/components/callbacks/callbackEvent");
463462
}
464463

464+
@Test
465+
public void testIssue922() {
466+
OpenAPIParser openAPIParser = new OpenAPIParser();
467+
ParseOptions options = new ParseOptions();
468+
OpenAPI openAPI = openAPIParser.readLocation("petstore.json",null,null).getOpenAPI();
469+
assertNotNull(openAPI);
470+
assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDeprecated(),Boolean.FALSE);
471+
472+
}
473+
465474
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore",
6+
"license": {
7+
"name": "MIT"
8+
}
9+
},
10+
"servers": [
11+
{
12+
"url": "http://petstore.swagger.io/v1"
13+
}
14+
],
15+
"paths": {
16+
"/pets": {
17+
"get": {
18+
"summary": "List all pets",
19+
"operationId": "listPets",
20+
"tags": [
21+
"pets"
22+
],
23+
"parameters": [
24+
{
25+
"name": "limit",
26+
"in": "query",
27+
"description": "How many items to return at one time (max 100)",
28+
"required": false,
29+
"schema": {
30+
"type": "integer",
31+
"format": "int32"
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "A paged array of pets",
38+
"headers": {
39+
"x-next": {
40+
"description": "A link to the next page of responses",
41+
"schema": {
42+
"type": "string"
43+
}
44+
}
45+
},
46+
"content": {
47+
"application/json": {
48+
"schema": {
49+
"$ref": "#/components/schemas/Pets"
50+
}
51+
}
52+
}
53+
},
54+
"default": {
55+
"description": "unexpected error",
56+
"content": {
57+
"application/json": {
58+
"schema": {
59+
"$ref": "#/components/schemas/Error"
60+
}
61+
}
62+
}
63+
}
64+
}
65+
},
66+
"post": {
67+
"summary": "Create a pet",
68+
"operationId": "createPets",
69+
"tags": [
70+
"pets"
71+
],
72+
"responses": {
73+
"201": {
74+
"description": "Null response"
75+
},
76+
"default": {
77+
"description": "unexpected error",
78+
"content": {
79+
"application/json": {
80+
"schema": {
81+
"$ref": "#/components/schemas/Error"
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
},
89+
"/pets/{petId}": {
90+
"get": {
91+
"summary": "Info for a specific pet",
92+
"operationId": "showPetById",
93+
"tags": [
94+
"pets"
95+
],
96+
"parameters": [
97+
{
98+
"name": "petId",
99+
"in": "path",
100+
"required": true,
101+
"description": "The id of the pet to retrieve",
102+
"schema": {
103+
"type": "string"
104+
}
105+
},
106+
{
107+
"name": "test",
108+
"in": "query",
109+
"description": "The id of the pet to retrieve",
110+
"schema": {
111+
"type": "string"
112+
}
113+
}
114+
],
115+
"responses": {
116+
"200": {
117+
"description": "Expected response to a valid request",
118+
"content": {
119+
"application/json": {
120+
"schema": {
121+
"$ref": "#/components/schemas/Pets"
122+
}
123+
}
124+
}
125+
},
126+
"default": {
127+
"description": "unexpected error",
128+
"content": {
129+
"application/json": {
130+
"schema": {
131+
"$ref": "#/components/schemas/Error"
132+
}
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
139+
},
140+
"components": {
141+
"schemas": {
142+
"Pet": {
143+
"required": [
144+
"id",
145+
"name"
146+
],
147+
"properties": {
148+
"id": {
149+
"type": "integer",
150+
"format": "int64"
151+
},
152+
"name": {
153+
"type": "string"
154+
},
155+
"tag": {
156+
"type": "string"
157+
}
158+
}
159+
},
160+
"Pets": {
161+
"type": "array",
162+
"items": {
163+
"$ref": "#/components/schemas/Pet"
164+
}
165+
},
166+
"Error": {
167+
"required": [
168+
"code",
169+
"message"
170+
],
171+
"properties": {
172+
"code": {
173+
"type": "integer",
174+
"format": "int32"
175+
},
176+
"message": {
177+
"type": "string"
178+
}
179+
}
180+
}
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)