Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 89d59b4

Browse files
authored
Merge pull request swagger-api#1101 from kerrykimbrough/issue-1047
[issue swagger-api#1047] Ensure error message when "items" undefined for schema with type=array
2 parents c5404fd + b4f4940 commit 89d59b4

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,9 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
22622262
schema.setType(type);
22632263
}
22642264
}
2265+
if("array".equals( schema.getType()) && !(schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null)) {
2266+
result.missing(location, "items");
2267+
}
22652268
}
22662269

22672270
ObjectNode notObj = getObject("not", node, false, location, result);

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,47 @@ public void testArrayQueryParam() throws Exception {
763763
assertTrue(((ArraySchema) p).getItems() instanceof StringSchema);
764764
}
765765

766+
@Test
767+
public void testArrayItems() {
768+
String yaml =
769+
"openapi: 3.0.0\n" +
770+
"info:\n" +
771+
" title: Test\n" +
772+
" version: 1.0.0\n" +
773+
"paths:\n" +
774+
" \"/store/inventory\":\n" +
775+
" post:\n" +
776+
" requestBody:\n" +
777+
" content:\n" +
778+
" application/json:\n" +
779+
" schema:\n" +
780+
" type: array\n" +
781+
" minItems: 1\n" +
782+
" responses:\n" +
783+
" '200':\n" +
784+
" description: successful operation\n" +
785+
" content:\n" +
786+
" application/json:\n" +
787+
" schema:\n" +
788+
" items:\n" +
789+
" type: string"
790+
;
791+
792+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
793+
SwaggerParseResult result = parser.readContents(yaml, null, null);
794+
assertEquals(result.getMessages(), Arrays.asList("attribute paths.'/store/inventory'(post).requestBody.content.schema.items is missing"));
795+
796+
OpenAPI openAPI = result.getOpenAPI();
797+
798+
Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
799+
assertFalse(body.getClass().equals( ArraySchema.class), "body is an ArraySchema");
800+
assertEquals(body.getType(), "array");
801+
assertEquals(body.getMinItems(), Integer.valueOf(1));
802+
803+
Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
804+
assertTrue(response.getClass().equals( ArraySchema.class), "response is an ArraySchema");
805+
assertEquals(body.getType(), "array");
806+
}
766807

767808
@Test(description = "it should read a top-level extension per https://github.com/openAPI-api/validator-badge/issues/59")
768809
public void testToplevelExtension() throws Exception {

0 commit comments

Comments
 (0)