Repository: swagger-api/swagger-parser
Pull request #1649 (2022-02-03 22:43:41)

OpenAPIDeserializer: Report an error when a request body content defines no media types.
Fixes issues #1648


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 33dfc418..74f6fa43 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
@@ -3123,9 +3123,14 @@ public class OpenAPIDeserializer {
 		}
 
 		final ObjectNode contentNode = getObject("content", node, true, location, result);
-		if (contentNode != null) {
-			body.setContent(getContent(contentNode, location + ".content", result));
-		}
+        Content content = getContent(contentNode, location + ".content", result);
+		if(content != null && content.isEmpty()) {
+            result.unsupported(location,"content with no media type",contentNode);
+            result.invalid();
+        }
+        else {
+            body.setContent(content);
+        }
 
 		Map<String, Object> extensions = getExtensions(node);
 		if (extensions != null && extensions.size() > 0) {


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 23869b30..83ec3a71 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
@@ -1220,6 +1220,63 @@ public class OpenAPIDeserializerTest {
             "attribute components.schemas.ByteString.default=`W.T.F?` is not of type `byte`"));
     }
 
+    @Test
+    public void testBodyContent() {
+        String json =
+            "{"
+            + "  \"openapi\": \"3.0.0\","
+            + "  \"info\": {"
+            + "    \"title\": \"Operations\","
+            + "    \"version\": \"0.0.0\""
+            + "  },"
+            + "  \"paths\": {"
+            + "    \"/operations\": {"
+            + "      \"post\": {"
+            + "        \"requestBody\": {"
+            + "            \"description\": \"Content empty\","
+            + "            \"content\": {"
+            + "            }"
+            + "        },"
+            + "        \"responses\": {"
+            + "          \"default\": {"
+            + "            \"description\": \"None\""
+            + "          }"
+            + "        }"
+            + "      },"
+            + "      \"put\": {"
+            + "        \"requestBody\": {"
+            + "            \"description\": \"Content undefined\""
+            + "        },"
+            + "        \"responses\": {"
+            + "          \"default\": {"
+            + "            \"description\": \"None\""
+            + "          }"
+            + "        }"
+            + "      }"
+            + "    }"
+            + "  }"
+            + "}"
+            ;
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+        SwaggerParseResult result = parser.readContents(json, null, null);
+
+        Operation post = result.getOpenAPI().getPaths().get( "/operations").getPost();
+        assertEquals( post.getRequestBody().getContent(), null, "Empty content");
+        assertEquals
+            (result.getMessages().contains("attribute paths.'/operations'(post).requestBody.content with no media type is unsupported"),
+             true,
+             "Empty content error reported");
+
+        Operation put = result.getOpenAPI().getPaths().get( "/operations").getPut();
+        assertEquals( put.getRequestBody().getContent(), null, "Empty content");
+        assertEquals
+            (result.getMessages().contains("attribute paths.'/operations'(put).requestBody.content is missing"),
+             true,
+             "Missing content error reported");
+
+        assertEquals( result.getMessages().size(), 2, "Messages");
+    }
+
     @Test
     public void testStyleInvalid() {
         String json =