Repository: swagger-api/swagger-parser
Pull request #752 (2018-07-06 13:37:51)

Fix for 749
Hi team,

This PR is to solve the issue '$ref in external file in parameter with resolve option true' as mentioned in https://github.com/swagger-api/swagger-parser/issues/749

Please review the PR and merge the same.

Thanks,
Mohammed




Patch:
diff --git a/README.md b/README.md
index 2bc8bcd0..a8cd031a 100644
--- a/README.md
+++ b/README.md
@@ -95,7 +95,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f
 <dependency>
   <groupId>io.swagger.parser.v3</groupId>
   <artifactId>swagger-parser</artifactId>
-  <version>2.0.2-SNAPSHOT</version>
+  <version>2.0.1</version>
 </dependency>
 
 ```
@@ -106,7 +106,7 @@ or
 <dependency>
   <groupId>io.swagger.parser.v3</groupId>
   <artifactId>swagger-parser</artifactId>
-  <version>2.0.2-SNAPSHOT</version>
+  <version>2.0.1</version>
 </dependency>
 
 ```
diff --git a/modules/swagger-parser-core/pom.xml b/modules/swagger-parser-core/pom.xml
index 8559734b..7e39477f 100644
--- a/modules/swagger-parser-core/pom.xml
+++ b/modules/swagger-parser-core/pom.xml
@@ -3,7 +3,7 @@
     <parent>
         <groupId>io.swagger.parser.v3</groupId>
         <artifactId>swagger-parser-project</artifactId>
-        <version>2.0.2-SNAPSHOT</version>
+        <version>2.0.1</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/modules/swagger-parser-v2-converter/pom.xml b/modules/swagger-parser-v2-converter/pom.xml
index e8e47330..33cc1ded 100644
--- a/modules/swagger-parser-v2-converter/pom.xml
+++ b/modules/swagger-parser-v2-converter/pom.xml
@@ -3,7 +3,7 @@
     <parent>
         <groupId>io.swagger.parser.v3</groupId>
         <artifactId>swagger-parser-project</artifactId>
-        <version>2.0.2-SNAPSHOT</version>
+        <version>2.0.1</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
index 814f1543..b4250e44 100644
--- a/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
+++ b/modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java
@@ -317,10 +317,7 @@ public class SwaggerConverter implements SwaggerParserExtension {
         }
 
         Scopes scopes = new Scopes();
-        Map<String, String> oAuth2Scopes = oAuth2Definition.getScopes();
-        if (oAuth2Scopes != null) {
-            oAuth2Scopes.forEach((k, v) -> scopes.addString(k, v));
-        }
+        oAuth2Definition.getScopes().forEach((k, v) -> scopes.addString(k, v));
         oAuthFlow.setScopes(scopes);
 
         securityScheme.setFlows(oAuthFlows);
diff --git a/modules/swagger-parser-v3/pom.xml b/modules/swagger-parser-v3/pom.xml
index 35cbe7f9..407fd1c9 100644
--- a/modules/swagger-parser-v3/pom.xml
+++ b/modules/swagger-parser-v3/pom.xml
@@ -3,7 +3,7 @@
     <parent>
         <groupId>io.swagger.parser.v3</groupId>
         <artifactId>swagger-parser-project</artifactId>
-        <version>2.0.2-SNAPSHOT</version>
+        <version>2.0.1</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ParameterProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ParameterProcessor.java
index 273a19fa..fde98ab3 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ParameterProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ParameterProcessor.java
@@ -133,6 +133,13 @@ public class ParameterProcessor {
 
         }
 
+        for (Parameter parameter : processedPathLevelParameters) {
+            Schema schema = parameter.getSchema();
+            if(schema != null){
+                schemaProcessor.processSchema(schema);
+            }
+        }
+
         return processedPathLevelParameters;
     }
 }
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
index 4eb0579a..be93c1d4 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/InlineModelResolver.java
@@ -436,7 +436,6 @@ public class InlineModelResolver {
         model.setExample(example);
         model.setName(name);
         model.setXml(xml);
-        model.setType(object.getType());
 
         if (properties != null) {
             flattenProperties(properties, path);
diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml
index 06ae69f4..18737a5e 100644
--- a/modules/swagger-parser/pom.xml
+++ b/modules/swagger-parser/pom.xml
@@ -3,7 +3,7 @@
     <parent>
         <groupId>io.swagger.parser.v3</groupId>
         <artifactId>swagger-parser-project</artifactId>
-        <version>2.0.2-SNAPSHOT</version>
+        <version>2.0.1</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/pom.xml b/pom.xml
index 5562d6e9..376ecdda 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,7 +8,7 @@
     <modelVersion>4.0.0</modelVersion>
     <groupId>io.swagger.parser.v3</groupId>
     <artifactId>swagger-parser-project</artifactId>
-    <version>2.0.2-SNAPSHOT</version>
+    <version>2.0.1</version>
     <packaging>pom</packaging>
     <name>swagger-parser-project</name>
     <developers>


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ParameterProcessorTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ParameterProcessorTest.java
index 60e82e41..318c0f0e 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ParameterProcessorTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/processors/ParameterProcessorTest.java
@@ -13,10 +13,7 @@ import io.swagger.v3.oas.models.parameters.QueryParameter;
 import io.swagger.v3.oas.models.parameters.RequestBody;
 import io.swagger.v3.parser.ResolverCache;
 import io.swagger.v3.parser.models.RefFormat;
-import mockit.FullVerifications;
-import mockit.Injectable;
-import mockit.Mocked;
-import mockit.StrictExpectations;
+import mockit.*;
 import org.testng.annotations.Test;
 
 
@@ -44,7 +41,18 @@ public class ParameterProcessorTest {
                                                                @Injectable final CookieParameter cookieParameter,
                                                                @Injectable final PathParameter pathParameter) throws Exception {
         expectedModelProcessorCreation();
-
+        new Expectations() {
+            {
+                headerParameter.getSchema();
+                result = null;
+                queryParameter.getSchema();
+                result = null;
+                cookieParameter.getSchema();
+                result = null;
+                pathParameter.getSchema();
+                result = null;
+            }
+        };
         final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI)
                 .processParameters(Arrays.<Parameter>asList(headerParameter,
                         queryParameter,
@@ -78,6 +86,12 @@ public class ParameterProcessorTest {
         Parameter refParameter = new Parameter().$ref(ref);
 
         expectLoadingRefFromCache(ref, RefFormat.INTERNAL, resolvedHeaderParam);
+        new Expectations() {
+            {
+                resolvedHeaderParam.getSchema();
+                result = null;
+            }
+        };
 
         final List<Parameter> processedParameters = new ParameterProcessor(cache, openAPI)
                 .processParameters(Arrays.<Parameter>asList(refParameter));
@@ -132,8 +146,6 @@ public class ParameterProcessorTest {
             new SchemaProcessor(cache, openAPI);
             times = 1;
             result = modelProcessor;
-
-
         }};
     }
 }
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
index 768b1aa8..99ff7e90 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/InlineModelResolverTest.java
@@ -16,8 +16,6 @@ import io.swagger.v3.oas.models.parameters.Parameter;
 import io.swagger.v3.oas.models.parameters.RequestBody;
 import io.swagger.v3.oas.models.responses.ApiResponse;
 import io.swagger.v3.oas.models.responses.ApiResponses;
-import io.swagger.v3.parser.OpenAPIV3Parser;
-import io.swagger.v3.parser.core.models.ParseOptions;
 import org.testng.annotations.Test;
 
 
@@ -31,17 +29,6 @@ import static org.testng.AssertJUnit.*;
 
 @SuppressWarnings("static-method")
 public class InlineModelResolverTest {
-
-    @Test
-    public void testIssue705() throws Exception {
-        ParseOptions options = new ParseOptions();
-        options.setFlatten(true);
-        OpenAPI openAPI = new OpenAPIV3Parser().read("issue-705.yaml",null, options);
-        assertNotNull(openAPI);
-        assertNotNull(openAPI.getComponents().getSchemas().get("inline_response_200").getType());
-    }
-
-
     @Test
     public void resolveInlineModelTestWithoutTitle() throws Exception {
         OpenAPI openAPI = new OpenAPI();
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-705.yaml b/modules/swagger-parser-v3/src/test/resources/issue-705.yaml
deleted file mode 100644
index c2b4c4b7..00000000
--- a/modules/swagger-parser-v3/src/test/resources/issue-705.yaml
+++ /dev/null
@@ -1,41 +0,0 @@
-openapi: 3.0.0
-info:
-  version: 1.0.0
-  title: Swagger Petstore
-  license:
-    name: MIT
-paths:
-  /pets:
-    get:
-      summary: List all pets
-      operationId: listPets
-      tags:
-        - pets
-      parameters:
-        - name: limit
-          in: query
-          description: How many items to return at one time (max 100)
-          required: false
-          schema:
-            type: integer
-            format: int32
-      responses:
-        '200':
-          description: An paged array of pets
-          headers:
-            x-next:
-              description: A link to the next page of responses
-              schema:
-                type: string
-          content:
-            application/json:
-              schema:
-                 type: object
-                 properties:
-                    id:
-                      type: integer
-                      format: int64
-                    name:
-                      type: string
-                    tag:
-                      type: string
\ No newline at end of file
diff --git a/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java b/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java
index ca583b42..911755b0 100644
--- a/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java
+++ b/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java
@@ -1,15 +1,50 @@
 package io.swagger.parser;
 
+import io.swagger.v3.oas.models.Components;
 import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.PathItem;
+import io.swagger.v3.oas.models.media.Schema;
+import io.swagger.v3.oas.models.parameters.Parameter;
 import io.swagger.v3.parser.core.models.ParseOptions;
 import io.swagger.v3.parser.core.models.SwaggerParseResult;
 import io.swagger.v3.core.util.Json;
 import org.junit.Test;
 
+import java.util.List;
+import java.util.Map;
+
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
 public class OpenAPIParserTest {
+    @Test
+    public void testIssue749() {
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIParser().readLocation("issue749-main.yaml", null, options);
+        assertNotNull(result);
+
+        OpenAPI openAPI = result.getOpenAPI();
+        assertNotNull(openAPI);
+
+        Components components = openAPI.getComponents();
+        assertNotNull(components);
+
+        PathItem pathItem = openAPI.getPaths().get("/some/ping");
+        assertNotNull(pathItem);
+        List<Parameter> parameters = pathItem.getGet().getParameters();
+        assertNotNull(parameters);
+        assertEquals(parameters.size(), 1);
+        assertEquals(parameters.get(0).getName(), "i");
+        assertNotNull(parameters.get(0).getSchema());
+        assertEquals(parameters.get(0).getSchema().get$ref(), "#/components/schemas/SomeId");
+
+        Map<String, Schema> schemas = components.getSchemas();
+        assertNotNull(schemas);
+        assertEquals(schemas.size(), 1);
+        assertNotNull(schemas.get("SomeId"));
+    }
+
     @Test
     public void testSimple() {
         SwaggerParseResult result = new OpenAPIParser().readLocation("petstore.yaml", null, null);
diff --git a/modules/swagger-parser/src/test/resources/issue749-main.yaml b/modules/swagger-parser/src/test/resources/issue749-main.yaml
new file mode 100644
index 00000000..0b907bc3
--- /dev/null
+++ b/modules/swagger-parser/src/test/resources/issue749-main.yaml
@@ -0,0 +1,22 @@
+openapi: 3.0.1
+info:
+  title: ping test
+  version: '1.0'
+servers:
+  - url: 'http://localhost:8000/'
+paths:
+  /some/ping:
+    get:
+      operationId: pingGet
+      parameters:
+        - name: i
+          in: query
+          description: Test
+          required: true
+          schema:
+            $ref: './issue749-reference.yaml#/SomeId'
+      responses:
+        '201':
+          description: OK
+components:
+  schemas: {}
\ No newline at end of file
diff --git a/modules/swagger-parser/src/test/resources/issue749-reference.yaml b/modules/swagger-parser/src/test/resources/issue749-reference.yaml
new file mode 100644
index 00000000..8b956381
--- /dev/null
+++ b/modules/swagger-parser/src/test/resources/issue749-reference.yaml
@@ -0,0 +1,5 @@
+SomeId:
+  type: integer
+  format: int32
+  description: My value 123
+  example: 123
\ No newline at end of file