Repository: swagger-api/swagger-parser
Pull request #1065 (2019-03-29 08:38:37)

Fixed referenced parameters validation
Fix for issue #1063.


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 520929ad..c03ab661 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
@@ -46,8 +46,12 @@ import io.swagger.v3.oas.models.servers.ServerVariable;
 import io.swagger.v3.oas.models.servers.ServerVariables;
 import io.swagger.v3.parser.core.models.SwaggerParseResult;
 import io.swagger.v3.core.util.Json;
+import io.swagger.v3.core.util.RefUtils;
+
 import org.apache.commons.lang3.StringUtils;
 
+import static io.swagger.v3.core.util.RefUtils.extractSimpleName;
+
 import java.math.BigDecimal;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -1408,13 +1412,25 @@ public class OpenAPIDeserializer {
         }
         Set<String> filter = new HashSet<>();
 
-        for(Parameter param:parameters) {
+        parameters.stream().map(this::getParameterDefinition).forEach(param -> {
             if(!filter.add(param.getName()+"#"+param.getIn())) {
                 result.warning(location,"There are duplicate parameter values");
             }
-        }
+        });
         return parameters;
     }
+    
+    private Parameter getParameterDefinition(Parameter parameter) {
+        if (parameter.get$ref() == null) {
+            return parameter;
+        }
+        Object parameterSchemaName = extractSimpleName(parameter.get$ref()).getLeft();
+        return Optional.ofNullable(components)
+            .map(Components::getParameters)
+            .map(parameters -> parameters.get(parameterSchemaName))
+            .orElse(parameter);
+            
+    }
 
     public Parameter getParameter(ObjectNode obj, String location, ParseResult result) {
         if (obj == 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 e858a164..7e2eb2d5 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
@@ -52,6 +52,8 @@ import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 import static com.github.tomakehurst.wiremock.client.WireMock.*;
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.notNullValue;
@@ -1801,6 +1803,26 @@ public class OpenAPIV3ParserTest {
         assertThat(((Schema) modelSchema.getProperties().get("id")).get$ref(), equalTo("#/components/schemas/ValueId"));
     }
 
+    @Test
+    public void shouldParseApiWithMultipleParameterReferences() {
+        // given
+        String location = "src/test/resources/issue-1063/api.yaml";
+        ParseOptions options = new ParseOptions();
+        OpenAPIV3Parser tested = new OpenAPIV3Parser();
+
+        // when
+        SwaggerParseResult result = tested.readLocation(location, emptyList(), options);
+
+        // then
+        OpenAPI api = result.getOpenAPI();
+        Map<String, Parameter> parameters = api.getComponents().getParameters();
+        assertThat(parameters.keySet(), equalTo(new HashSet<>(asList("IdParam", "NameParam"))));
+        assertThat(parameters.get("IdParam").getName(), equalTo("id"));
+        assertThat(parameters.get("NameParam").getName(), equalTo("name"));
+        
+        assertThat(result.getMessages(), equalTo(emptyList()));
+    }
+
     private static int getDynamicPort() {
         return new Random().ints(10000, 20000).findFirst().getAsInt();
     }
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1063/api.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1063/api.yaml
new file mode 100644
index 00000000..5595caf9
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1063/api.yaml
@@ -0,0 +1,36 @@
+openapi: 3.0.0
+info:
+  title: Test
+  version: 1.0.0
+
+paths:
+  /value:
+    get:
+      operationId: getValues
+      parameters:
+        - $ref: '#/components/parameters/IdParam'
+        - $ref: '#/components/parameters/NameParam'
+      responses:
+        200:
+          description: Successful response
+          content:
+            application/json:
+              schema:
+                type:
+                  string
+
+components:
+  parameters:
+    IdParam:
+      required: true
+      name: id
+      in: query
+      schema:
+        type: integer
+        format: int64
+    NameParam:
+      required: false
+      name: name
+      in: query
+      schema:
+        type: string
\ No newline at end of file