Repository: swagger-api/swagger-parser
Pull request #1077 (2019-04-15 21:31:35)

Fix parsing of MapSchema 
Fix parsing of MapSchema  refs #1071 


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 ceeafcce..66140117 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
@@ -24,6 +24,7 @@ import io.swagger.v3.oas.models.media.DateSchema;
 import io.swagger.v3.oas.models.media.DateTimeSchema;
 import io.swagger.v3.oas.models.media.Discriminator;
 import io.swagger.v3.oas.models.media.Encoding;
+import io.swagger.v3.oas.models.media.MapSchema;
 import io.swagger.v3.oas.models.media.MediaType;
 import io.swagger.v3.oas.models.media.Schema;
 import io.swagger.v3.oas.models.media.XML;
@@ -2023,7 +2024,7 @@ 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);
 
 
         if((allOfArray != null )||(anyOfArray != null)|| (oneOfArray != null)) {
@@ -2075,6 +2076,31 @@ 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);
+                    }
+                }
+            } else if (additionalPropertiesNode.getNodeType().equals(JsonNodeType.BOOLEAN)) {
+                Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
+                if (additionalProperties != null) {
+                    if (additionalProperties == true) {
+                        mapSchema.setAdditionalProperties(additionalProperties);
+                    }else{
+                        schema.setAdditionalProperties(additionalProperties);
+                    }
+                }
+            }
+            schema = mapSchema;
+        }
+
+
+
         if (schema == null){
             schema = SchemaTypeUtil.createSchemaByType(node);
         }
@@ -2267,24 +2293,6 @@ public class OpenAPIDeserializer {
             schema.setProperties(properties);
         }
 
-
-        if (node.get("additionalProperties") != null) {
-            if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.OBJECT)) {
-                ObjectNode additionalPropertiesObj = getObject("additionalProperties", node, false, location, result);
-                if (additionalPropertiesObj != null) {
-                    Schema additionalProperties = getSchema(additionalPropertiesObj, location, result);
-                    if (additionalProperties != null) {
-                        schema.setAdditionalProperties(additionalProperties);
-                    }
-                }
-            } else if (node.get("additionalProperties").getNodeType().equals(JsonNodeType.BOOLEAN)) {
-                Boolean additionalProperties = getBoolean("additionalProperties", node, false, location, result);
-                if (additionalProperties != null) {
-                    schema.setAdditionalProperties(additionalProperties);
-                }
-
-            }
-        }
         value = getString("description",node,false,location,result);
         if (StringUtils.isNotBlank(value)) {
             schema.setDescription(value);


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
index cd5007d4..a7273287 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java
@@ -65,6 +65,20 @@ public class OpenAPIV3ParserTest {
     protected int serverPort = getDynamicPort();
     protected WireMockServer wireMockServer;
 
+    @Test
+    public void testIssue1071() {
+
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+
+        SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1071.yaml", null, options);
+        OpenAPI apispec = parseResult.getOpenAPI();
+        assertNotNull(apispec);
+        Schema test = apispec.getPaths().get("/mapschema").getGet().getResponses().get("200").getContent().get("application/json").getSchema();
+        System.out.println(test.getClass());
+        assertTrue(test instanceof MapSchema);
+
+    }
 
     @Test
     public void testIssue1039() {
@@ -76,7 +90,7 @@ public class OpenAPIV3ParserTest {
         OpenAPI apispec = parseResult.getOpenAPI();
         assertNotNull(apispec);
         assertEquals(apispec.getPaths().get("/pets").getGet().getParameters().get(0).getSchema().getType(),"array");
-        
+
     }
 
     @Test
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1071.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1071.yaml
new file mode 100644
index 00000000..697aeaf6
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1071.yaml
@@ -0,0 +1,29 @@
+openapi: "3.0.0"
+info:
+  version: 1.0.0
+  title: Swagger Petstore
+  description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
+  termsOfService: http://swagger.io/terms/
+  contact:
+    name: Swagger API Team
+    email: apiteam@swagger.io
+    url: http://swagger.io
+  license:
+    name: Apache 2.0
+    url: https://www.apache.org/licenses/LICENSE-2.0.html
+servers:
+  - url: http://petstore.swagger.io/api
+paths:
+  '/mapschema':
+    get:
+      description: MapSchema
+      operationId: MapSchema
+      responses:
+        '200':
+          description: Successful response.
+          content:
+            application/json:
+              schema:
+                type: object
+                additionalProperties:
+                  type: string
\ No newline at end of file