Repository: swagger-api/swagger-parser
Pull request #1100 (2019-05-15 22:41:16)

Avoid incorrect error message when parsing a boolean value for "additionalProperties" (issue #1099)


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 31a6ca35..91af5d6f 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
@@ -2032,9 +2032,6 @@ public class OpenAPIDeserializer {
         ArrayNode allOfArray = getArray("allOf", node, false, location, result);
         ArrayNode anyOfArray = getArray("anyOf", node, false, location, result);
         ObjectNode itemsNode = getObject("items", node, false, location, result);
-        ObjectNode additionalPropertiesNode = getObject("additionalProperties", node, false, location, result);
-        Boolean additionalPropertiesBoolean = getBoolean("additionalProperties", node, false, location, result);
-
 
         if((allOfArray != null )||(anyOfArray != null)|| (oneOfArray != null)) {
             ComposedSchema composedSchema = new ComposedSchema();
@@ -2085,31 +2082,25 @@ public class OpenAPIDeserializer {
             schema = items;
         }
 
-        if(additionalPropertiesNode != null) {
-            MapSchema mapSchema = new MapSchema();
-            if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.OBJECT)) {
-                ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location, result);
-                if (additionalPropertiesObj != null) {
-                    Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
-                    if (additionalProperties != null) {
-                        mapSchema.setAdditionalProperties(additionalProperties);
-                        schema = mapSchema;
-                    }
-                }
-            }
-        } else if(additionalPropertiesBoolean != null){
-            MapSchema mapSchema = new MapSchema();
-            if (additionalPropertiesBoolean) {
-                mapSchema.setAdditionalProperties(additionalPropertiesBoolean);
-                schema = mapSchema;
-            }else{
-                ObjectSchema objectSchema = new ObjectSchema();
-                objectSchema.setAdditionalProperties(additionalPropertiesBoolean);
-                schema = objectSchema;
-            }
-        }
+        Boolean additionalPropertiesBoolean = getBoolean("additionalProperties", node, false, location, result);
 
+        ObjectNode additionalPropertiesObject =
+            additionalPropertiesBoolean == null
+            ? getObject("additionalProperties", node, false, location, result)
+            : null;
 
+        Object additionalProperties =
+            additionalPropertiesObject != null
+            ? getSchema(additionalPropertiesObject, location, result)
+            : additionalPropertiesBoolean;
+
+        if(additionalProperties != null) {
+            schema =
+                additionalProperties.equals( Boolean.FALSE)
+                ? new ObjectSchema()
+                : new MapSchema();
+            schema.setAdditionalProperties( additionalProperties);
+        }
 
         if (schema == null){
             schema = SchemaTypeUtil.createSchemaByType(node);


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 5ba942f3..5e6fcdba 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
@@ -20,6 +20,7 @@ import io.swagger.v3.oas.models.media.Content;
 import io.swagger.v3.oas.models.media.DateSchema;
 import io.swagger.v3.oas.models.media.DateTimeSchema;
 import io.swagger.v3.oas.models.media.IntegerSchema;
+import io.swagger.v3.oas.models.media.MapSchema;
 import io.swagger.v3.oas.models.media.MediaType;
 import io.swagger.v3.oas.models.media.ObjectSchema;
 import io.swagger.v3.oas.models.media.Schema;
@@ -58,10 +59,11 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TimeZone;
 
+import static java.util.Collections.emptyList;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.assertFalse;
 
 public class OpenAPIDeserializerTest {
 
@@ -679,6 +681,44 @@ public class OpenAPIDeserializerTest {
         assertTrue(response.getAdditionalProperties() != null);
     }
 
+    @Test
+    public void testAdditionalPropertiesBoolean() {
+        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" +
+            "              additionalProperties: true\n" +
+            "      responses:\n" +
+            "        '200':\n" +
+            "          description: successful operation\n" +
+            "          content:\n" +
+            "            application/json:\n" +
+            "              schema:\n" +
+            "                additionalProperties: false\n";
+
+        OpenAPIV3Parser parser = new OpenAPIV3Parser();
+        SwaggerParseResult result = parser.readContents(yaml, null, null);
+        assertEquals(result.getMessages(), emptyList());
+        
+        OpenAPI openAPI = result.getOpenAPI();
+
+        Schema body = openAPI.getPaths().get("/store/inventory").getPost().getRequestBody().getContent().get("application/json").getSchema();
+        assertEquals(body.getAdditionalProperties(), Boolean.TRUE);
+        assertEquals(body.getClass(), MapSchema.class);
+
+        Schema response = openAPI.getPaths().get("/store/inventory").getPost().getResponses().get("200").getContent().get("application/json").getSchema();
+        assertEquals(response.getAdditionalProperties(), Boolean.FALSE);
+        assertEquals(response.getClass(), ObjectSchema.class);
+    }
+
     @Test
     public void testArrayQueryParam() throws Exception {
         String yaml = "openapi: 3.0.0\n" +