Repository: swagger-api/swagger-parser
Pull request #977 (2019-01-09 09:40:47)

fix bug of validation failure when path parameter contains ref
This patch fixes ~https://github.com/swagger-api/swagger-parser/issues/911~ https://github.com/swagger-api/swagger-parser/issues/976

jjlin: Did you mean to say it fixes #976?
fujigon: exactly.
Thanks for following up!
mih-kopylov: @gracekarina could you please take a look at this PR? It might be useful

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 35d96e8a..5d79ddd7 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
@@ -560,7 +560,7 @@ public class OpenAPIDeserializer {
             return false;
         } else {
             Parameter pathParamDefined = parameters.stream()
-                            .filter(parameter -> pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn()))
+                            .filter(parameter -> (parameter.get$ref() != null) || (pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn())))
                             .findFirst()
                             .orElse(null);
             if (pathParamDefined == null) {


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 0c15fa33..ad04e8ff 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
@@ -36,6 +36,7 @@ import io.swagger.v3.parser.core.models.ParseOptions;
 import io.swagger.v3.parser.core.models.SwaggerParseResult;
 import mockit.Injectable;
 import org.apache.commons.io.FileUtils;
+import org.hamcrest.CoreMatchers;
 import org.testng.Assert;
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -50,6 +51,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static org.junit.Assert.assertThat;
 import static org.testng.Assert.*;
 
 
@@ -69,7 +71,7 @@ public class OpenAPIV3ParserTest {
         Assert.assertNull ( openAPI.getPaths().get("/issue-125").getGet().getResponses().get("200").getContent().get("*/*").getSchema().getFormat());
         Assert.assertNull (openAPI.getPaths().get("/primitiveBody/binary").getPost().getRequestBody().getContent().get("application/octet-stream").getSchema().getFormat());
     }
-  
+
     @Test
     public void testIssue983() {
         OpenAPIV3Parser parser = new OpenAPIV3Parser();
@@ -80,7 +82,7 @@ public class OpenAPIV3ParserTest {
         Yaml.prettyPrint(openAPI);
         Assert.assertNotNull(openAPI.getComponents().getSchemas().get("InventoryId"));
     }
-  
+
     @Test
     public void testIssue913() {
         OpenAPIV3Parser parser = new OpenAPIV3Parser();
@@ -161,7 +163,7 @@ public class OpenAPIV3ParserTest {
         Assert.assertEquals(examples.get("local").get$ref(), "#/components/examples/LocalRef");
         Assert.assertEquals(examples.get("external").get$ref(), "#/components/examples/ExternalRef");
     }
-  
+
     @Test
     public void testIssue834() {
         ParseOptions options = new ParseOptions();
@@ -1744,6 +1746,14 @@ public class OpenAPIV3ParserTest {
         assertEquals(composed.getAllOf().get(0).get$ref(), "#/components/schemas/Image");
     }
 
+    @Test
+    public void testValidationIssue() {
+        ParseOptions parseOptions = new ParseOptions();
+        parseOptions.setResolveFully(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/validation/path-parameter-validation.yaml", null, parseOptions);
+        assertThat(result.getMessages().size(), CoreMatchers.is(0));
+    }
+
     private static int getDynamicPort() {
         return new Random().ints(10000, 20000).findFirst().getAsInt();
     }
diff --git a/modules/swagger-parser-v3/src/test/resources/validation/path-parameter-validation.yaml b/modules/swagger-parser-v3/src/test/resources/validation/path-parameter-validation.yaml
new file mode 100644
index 00000000..21e05a0a
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/validation/path-parameter-validation.yaml
@@ -0,0 +1,25 @@
+openapi: 3.0.0
+info:
+  title: Sample APIs
+  version: 1.0.0
+  description:
+    Sample API
+
+components:
+  parameters:
+    accountId:
+      name: accountId
+      schema:
+        type: string
+      in: path
+      required: true
+
+paths:
+  /users/{accountId}:
+    get:
+      operationId: ReadUser
+      parameters:
+      - $ref: '#/components/parameters/accountId'
+      responses:
+        '200':
+          description: 200 response