Repository: swagger-api/swagger-parser
Pull request #1645 (2022-02-02 14:26:08)

improves AllowEmptyString option validation
Refs #1644 
Improves AllowEmptyString option validation for field "In" in parameters deserialization.


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 14f71983..cc10c5bf 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
@@ -1558,7 +1558,6 @@ public class OpenAPIDeserializer {
 			return null;
 		}
 
-
 		Parameter parameter = null;
 
 		JsonNode ref = obj.get("$ref");
@@ -1589,11 +1588,10 @@ public class OpenAPIDeserializer {
 
 		String value = getString("in", obj, true, location, result);
 
-		if (!result.isAllowEmptyStrings() && value == null) {
+		if (!result.isAllowEmptyStrings() && StringUtils.isBlank(value) || result.isAllowEmptyStrings() && value == null) {
 			return null;
 		}
 
-
 		if (QUERY_PARAMETER.equals(value)) {
 			parameter = new QueryParameter();
 		} else if (HEADER_PARAMETER.equals(value)) {
@@ -1609,7 +1607,6 @@ public class OpenAPIDeserializer {
 			return null;
 		}
 
-
 		parameter.setIn(value);
 
 		value = getString("name", obj, true, location, result);


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 bb902ea0..bc62cb10 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
@@ -83,6 +83,58 @@ public class OpenAPIV3ParserTest {
     protected int serverPort = getDynamicPort();
     protected WireMockServer wireMockServer;
 
+    @Test
+    public void testIssue1644_NullValue() throws Exception{
+        ParseOptions options = new ParseOptions();
+        String issue1644 = "openapi: 3.0.0\n" +
+                "info:\n" +
+                "  title: Operations\n" +
+                "  version: 0.0.0\n" +
+                "paths:\n" +
+                "  \"/operations\":\n" +
+                "    post:\n" +
+                "      parameters:\n" +
+                "        - name: param0\n" +
+                "          schema:\n" +
+                "            type: string\n" +
+                "      responses:\n" +
+                "        default:\n" +
+                "          description: None\n";
+        SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);
+
+        Assert.assertNotNull(result);
+        Assert.assertNotNull(result.getOpenAPI());
+        assertEquals(result.getMessages().size(),1);
+        assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is missing"));
+        assertFalse(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `string`"));
+    }
+
+    @Test
+    public void testIssue1644_EmptyValue() throws Exception{
+        ParseOptions options = new ParseOptions();
+        String issue1644 = "openapi: 3.0.0\n" +
+                "info:\n" +
+                "  title: Operations\n" +
+                "  version: 0.0.0\n" +
+                "paths:\n" +
+                "  \"/operations\":\n" +
+                "    post:\n" +
+                "      parameters:\n" +
+                "        - name: param0\n" +
+                "          in: ''\n" +
+                "          schema:\n" +
+                "            type: string\n" +
+                "      responses:\n" +
+                "        default:\n" +
+                "          description: None\n";
+        SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);
+
+        Assert.assertNotNull(result);
+        Assert.assertNotNull(result.getOpenAPI());
+        assertEquals(result.getMessages().size(),1);
+        assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `string`"));
+    }
+
 
     @Test
     public void testEmptyStrings_False() throws Exception{