Repository: swagger-api/swagger-parser
Pull request #1101 (2019-05-16 02:03:39)

[issue #1047] Ensure error message when "items" undefined for schema with type=array


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
index 91af5d6f..5455714e 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java
@@ -2262,6 +2262,9 @@ public class OpenAPIDeserializer {
                     schema.setType(type);
                 }
             }
+            if("array".equals( schema.getType()) && !(schema instanceof ArraySchema && ((ArraySchema) schema).getItems() != null)) {
+                result.missing(location, "items");
+            }
         }
 
         ObjectNode notObj = getObject("not", node, false, location, result);


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
index 5e6fcdba..101dba9f 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java
@@ -763,6 +763,47 @@ public class OpenAPIDeserializerTest {
         assertTrue(((ArraySchema) p).getItems() instanceof StringSchema);
     }
 
+    @Test
+    public void testArrayItems() {
+        String yaml =
+            "openapi: 3.0.0\n" +
+            "info:\n" +
+            "  title: Test\n" +
+            "  version: 1.0.0\n" +
+            "paths:\n" +
+            "  \"/store/inventory\":\n" +
+            "    post:\n" +
+            "      requestBody:\n" +
+            "        content:\n" +
+            "          application/json:\n" +
+            "            schema:\n" +
+            "              type: array\n" +
+            "              minItems: 1\n" +
+            "      responses:\n" +
+            "        '200':\n" +
+            "          description: successful operation\n" +
+            "          content:\n" +
+            "            application/json:\n" +
+            "              schema:\n" +
+            "                items:\n" +
+            "                  type: string"
+            ;
+
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+        SwaggerParseResult result = parser.readContents(yaml, null, null);
+        assertEquals(result.getMessages(), Arrays.asList("attribute paths.'/store/inventory'(post).requestBody.content.schema.items is missing"));
+        
+        OpenAPI openAPI = result.getOpenAPI();
+
+        Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
+        assertFalse(body.getClass().equals( ArraySchema.class), "body is an ArraySchema");
+        assertEquals(body.getType(), "array");
+        assertEquals(body.getMinItems(), Integer.valueOf(1));
+
+        Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
+        assertTrue(response.getClass().equals( ArraySchema.class), "response is an ArraySchema");
+        assertEquals(body.getType(), "array");
+    }
 
     @Test(description = "it should read a top-level extension per https://github.com/openAPI-api/validator-badge/issues/59")
     public void testToplevelExtension() throws Exception {