Skip to content

Commit 6a0014a

Browse files
committed
refs #2169 - mark invalid a null 'version' keyword
1 parent 016bb15 commit 6a0014a

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,27 +1153,32 @@ public ExternalDocumentation getExternalDocs(ObjectNode node, String location, P
11531153

11541154
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult
11551155
result, Set<String> uniqueValues, boolean noInvalidError) {
1156-
String value = null;
1157-
JsonNode v = node.get(key);
1158-
if (node == null || v == null) {
1159-
if (required) {
1160-
result.missing(location, key);
1161-
result.invalid();
1162-
}
1163-
} else if (!v.isValueNode()) {
1164-
if (!noInvalidError) {
1165-
result.invalidType(location, key, "string", node);
1166-
}
1167-
} else if (!v.isNull()) {
1168-
value = v.asText();
1169-
if (uniqueValues != null && !uniqueValues.add(value)) {
1170-
result.unique(location, "operationId");
1171-
result.invalid();
1172-
}
1173-
}
1174-
return value;
1156+
return getString(key, node, required, location, result, uniqueValues, noInvalidError, false);
11751157
}
11761158

1159+
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult
1160+
result, Set<String> uniqueValues, boolean noInvalidError, boolean missingForNullNode) {
1161+
String value = null;
1162+
JsonNode v = node.get(key);
1163+
if (node == null || v == null || (v.isNull() && missingForNullNode)) {
1164+
if (required) {
1165+
result.missing(location, key);
1166+
result.invalid();
1167+
}
1168+
} else if (!v.isValueNode()) {
1169+
if (!noInvalidError) {
1170+
result.invalidType(location, key, "string", node);
1171+
}
1172+
} else if (!v.isNull()) {
1173+
value = v.asText();
1174+
if (uniqueValues != null && !uniqueValues.add(value)) {
1175+
result.unique(location, "operationId");
1176+
result.invalid();
1177+
}
1178+
}
1179+
return value;
1180+
}
1181+
11771182
public String getString(String key, ObjectNode node, boolean required, String location, ParseResult
11781183
result, Set<String> uniqueValues) {
11791184
return getString(key, node, required, location, result, uniqueValues, false);
@@ -1289,7 +1294,7 @@ public Info getInfo(ObjectNode node, String location, ParseResult result) {
12891294
info.setLicense(license);
12901295
}
12911296

1292-
value = getString("version", node, true, location, result);
1297+
value = getString("version", node, true, location, result, null, false, true);
12931298
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
12941299
info.setVersion(value);
12951300
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3421,4 +3421,12 @@ public void testStyleAndExplodeExplicit(){
34213421
assertEquals(openAPI.getPaths().get("/test").getPost().getRequestBody().getContent().get("multipart/form-data").getEncoding().get("fileWithout").getStyle().toString(), "form");
34223422
assertNull(openAPI.getPaths().get("/test").getPost().getRequestBody().getContent().get("multipart/form-data").getEncoding().get("fileWithout").getExplode());
34233423
}
3424+
3425+
@Test(description = "null version should cause a message")
3426+
public void testVersion(){
3427+
ParseOptions options = new ParseOptions();
3428+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
3429+
SwaggerParseResult parseResult = openApiParser.readLocation("version-missing.yaml", null, options);
3430+
assertEquals(parseResult.getMessages().get(0), "attribute info.version is missing");
3431+
}
34243432
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Example API with Style and Explode
4+
version:
5+
paths: {}

0 commit comments

Comments
 (0)