Repository: swagger-api/swagger-parser
Pull request #1092 (2019-05-04 08:27:57)

Fix for issue #1090
Extract the `StringUtils.isBlank(schema.getType())` condition from the if introduced with #1076.

Fix for issue #1090.


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 30b6ec86..31a6ca35 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
@@ -2260,14 +2260,16 @@ public class OpenAPIDeserializer {
         }
 
         value = getString("type",node,false,location,result);
-        if (StringUtils.isNotBlank(value) && StringUtils.isBlank(schema.getType())) {
-            schema.setType(value);
-        }else{
-            // may have an enum where type can be inferred
-            JsonNode enumNode = node.get("enum");
-            if(enumNode != null && enumNode.isArray()) {
-                String type = inferTypeFromArray((ArrayNode) enumNode);
-                schema.setType(type);
+        if (StringUtils.isBlank(schema.getType())) {
+            if (StringUtils.isNotBlank(value)) {
+                schema.setType(value);
+            }else{
+                // may have an enum where type can be inferred
+                JsonNode enumNode = node.get("enum");
+                if(enumNode != null && enumNode.isArray()) {
+                    String type = inferTypeFromArray((ArrayNode) enumNode);
+                    schema.setType(type);
+                }
             }
         }
 


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 c2da4671..5ba942f3 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
@@ -855,6 +855,35 @@ public class OpenAPIDeserializerTest {
         assertEquals(numberImpl.getDefault(), new BigDecimal("3.14"));
     }
 
+    @Test
+    public void testEnumType() {
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./src/test/resources/issue-1090.yaml", null, options);
+        assertNotNull(result.getOpenAPI());
+        OpenAPI openAPI = result.getOpenAPI();
+
+        Schema someObj = openAPI.getComponents().getSchemas().get("SomeObj");
+        assertNotNull(someObj);
+
+        Map<String, Schema> properties = someObj.getProperties();
+        assertNotNull(properties);
+
+        Schema iprop = properties.get("iprop");
+        assertNotNull(iprop);
+        assertEquals(iprop.getType(), "integer");
+        assertEquals(iprop.getFormat(), "int32");
+
+        Schema lprop = properties.get("lprop");
+        assertNotNull(lprop);
+        assertEquals(lprop.getType(), "integer");
+        assertEquals(lprop.getFormat(), "int64");
+
+        Schema nprop = properties.get("nprop");
+        assertNotNull(nprop);
+        assertEquals(nprop.getType(), "number");
+    }
+
     @Test
     public void testDeserializeDateString() {
         String yaml = "openapi: 3.0.0\n" +
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1090.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1090.yaml
new file mode 100644
index 00000000..e02834e9
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1090.yaml
@@ -0,0 +1,37 @@
+openapi: 3.0.1
+info:
+  title: Object with enum test
+  description: Issue 1090
+  version: '1.0'
+servers:
+  - url: 'http://localhost:9999/'
+paths:
+  /ping:
+    get:
+      operationId: pingGet
+      responses:
+        '201':
+          description: OK
+components:
+  schemas:
+    SomeObj:
+      type: object
+      properties:
+        iprop:
+          type: integer
+          format: int32
+          enum:
+            - 1
+            - 2
+        lprop:
+          type: integer
+          format: int64
+          enum:
+            - 1
+            - 2
+        nprop:
+          type: number
+          format: double
+          enum:
+            - 1.1
+            - 1.2
\ No newline at end of file