Skip to content

Commit 1e41459

Browse files
authored
Merge pull request #532 from swagger-api/fix_examples
Fix examples
2 parents 169e8d6 + 90acf1d commit 1e41459

File tree

2 files changed

+29
-40
lines changed

2 files changed

+29
-40
lines changed

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

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,9 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
831831
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s'", location, "examples"), result));
832832
}
833833

834-
String value = getAnyExample(contentNode, location,result);
835-
if (StringUtils.isNotBlank(value)){
836-
mediaType.setExample(value);
834+
Object example = getAnyExample("example",contentNode, location,result);
835+
if (example != null){
836+
mediaType.setExample(example);
837837
}
838838

839839

@@ -1334,9 +1334,9 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
13341334
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s'", location, "examples"), result));
13351335
}
13361336

1337-
value = getAnyExample(obj, location,result);
1338-
if (StringUtils.isNotBlank(value)){
1339-
parameter.setExample(value);
1337+
Object example = getAnyExample("example", obj, location,result);
1338+
if (example != null){
1339+
parameter.setExample(example);
13401340
}
13411341

13421342
ObjectNode contentNode = getObject("content",obj,false,location,result);
@@ -1443,9 +1443,9 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
14431443
header.setExamples(getExamples(examplesObject, location, result));
14441444
}
14451445

1446-
value = getAnyExample(headerNode, location,result);
1447-
if (StringUtils.isNotBlank(value)){
1448-
header.setExample(value);
1446+
Object example = getAnyExample("example", headerNode, location,result);
1447+
if (example != null){
1448+
header.setExample(example);
14491449
}
14501450

14511451
ObjectNode contentNode = getObject("content",headerNode,false,location,result);
@@ -1468,34 +1468,34 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
14681468
return header;
14691469
}
14701470

1471-
public String getAnyExample(ObjectNode node, String location, ParseResult result ){
1472-
JsonNode example = node.get("example");
1471+
public Object getAnyExample(String nodeKey,ObjectNode node, String location, ParseResult result ){
1472+
JsonNode example = node.get(nodeKey);
14731473
if (example != null) {
14741474
if (example.getNodeType().equals(JsonNodeType.STRING)) {
1475-
String value = getString("example", node, false, location, result);
1475+
String value = getString(nodeKey, node, false, location, result);
14761476
if (StringUtils.isNotBlank(value)) {
14771477
return value;
14781478
}
14791479
} if (example.getNodeType().equals(JsonNodeType.NUMBER)) {
1480-
Integer integerExample = getInteger("example", node, false, location, result);
1480+
Integer integerExample = getInteger(nodeKey, node, false, location, result);
14811481
if (integerExample != null) {
1482-
return integerExample.toString();
1482+
return integerExample;
14831483
}else {
1484-
BigDecimal bigDecimalExample = getBigDecimal("example", node, false, location, result);
1484+
BigDecimal bigDecimalExample = getBigDecimal(nodeKey, node, false, location, result);
14851485
if (bigDecimalExample != null) {
1486-
return bigDecimalExample.toString();
1486+
return bigDecimalExample;
14871487

14881488
}
14891489
}
14901490
} else if (example.getNodeType().equals(JsonNodeType.OBJECT)) {
1491-
ObjectNode objectValue = getObject("example", node, false, location, result);
1491+
ObjectNode objectValue = getObject(nodeKey, node, false, location, result);
14921492
if (objectValue != null) {
1493-
return objectValue.toString();
1493+
return objectValue;
14941494
}
14951495
} else if (example.getNodeType().equals(JsonNodeType.ARRAY)) {
1496-
ArrayNode arrayValue = getArray("example", node, false, location, result);
1496+
ArrayNode arrayValue = getArray(nodeKey, node, false, location, result);
14971497
if (arrayValue != null) {
1498-
return arrayValue.toString();
1498+
return arrayValue;
14991499
}
15001500
}
15011501
}
@@ -2031,9 +2031,9 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
20312031
}
20322032
}
20332033

2034-
value = getAnyExample(node, location,result);
2035-
if (StringUtils.isNotBlank(value)){
2036-
schema.setExample(value);
2034+
Object example = getAnyExample("example", node, location,result);
2035+
if (example != null){
2036+
schema.setExample(example);
20372037
}
20382038

20392039
bool = getBoolean("deprecated", node, false, location, result);
@@ -2127,20 +2127,9 @@ public Example getExample(ObjectNode node, String location, ParseResult result)
21272127
example.setDescription(value);
21282128
}
21292129

2130-
JsonNode v = node.get("value");
2131-
if (v.getNodeType().equals(JsonNodeType.STRING)) {
2132-
value = getString("value", node, false, location, result);
2133-
example.setValue(value);
2134-
}else if (v.getNodeType().equals(JsonNodeType.OBJECT)) {
2135-
ObjectNode objectValue = getObject("value", node, false, location, result);
2136-
if(objectValue != null) {
2137-
example.setValue(objectValue.toString());
2138-
}
2139-
}else if (v.getNodeType().equals(JsonNodeType.ARRAY)) {
2140-
ArrayNode arrayValue = getArray("value", node, false, location, result);
2141-
if (arrayValue != null) {
2142-
example.setValue(arrayValue.toString());
2143-
}
2130+
Object sample = getAnyExample("value", node, location,result);
2131+
if (sample != null){
2132+
example.setValue(sample);
21442133
}
21452134

21462135

modules/swagger-parser-v3/src/test/java/io/swagger/parser/v3/util/OpenAPIDeserializerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ public void testExamples(@Injectable List<AuthorizationValue> auths){
371371
Assert.assertEquals(header1.getExamples().get("httpbin").getValue(),"httpbin");
372372

373373
Assert.assertNotNull(header2.getExample());
374-
Assert.assertEquals(header2.getExample(),"37");
374+
Assert.assertEquals(header2.getExample(),37);
375375

376376
Assert.assertNotNull(parameter1.getExamples());
377377
Assert.assertEquals(parameter1.getExamples().get("unicorn").getValue(),"unicorn");
378378

379379
Assert.assertNotNull(parameter2.getExample());
380-
Assert.assertEquals(parameter2.getExample(),"37");
380+
Assert.assertEquals(parameter2.getExample(),37);
381381
}
382382

383383

@@ -695,7 +695,7 @@ public void readContentObject(JsonNode rootNode) throws Exception {
695695
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getSummary(),"List of names");
696696
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("list").getValue(),"Bob,Diane,Mary,Bill");
697697
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getSummary(),"Empty");
698-
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getValue(),"");
698+
Assert.assertNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("text/plain").getExamples().get("empty").getValue());
699699

700700
PathItem petEndpoint = paths.get("/pet");
701701
Assert.assertNotNull(petEndpoint.getPut());

0 commit comments

Comments
 (0)