Repository: swagger-api/swagger-parser
Pull request #880 (2018-10-13 06:07:55)

Fix for issue 878
Hi team,

This PR is to solve the issue #878 (Callback with $ref is not working).

Please review the PR and merge the same.

Thanks,
Mohammed


Patch:
diff --git a/README.md b/README.md
index b9649cf2..e8bc01f5 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,6 @@
 # Swagger Parser
 
-**NOTE:** If you're looking for `swagger-parser` 1.X and OpenApi 2.0, please refer to [v1 branch](https://github.com/swagger-api/swagger-parser/tree/v1)
-
-[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v2.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-v2)
+[![Build Status](https://img.shields.io/jenkins/s/https/jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-2.0.svg)](https://jenkins.swagger.io/view/OSS%20-%20Java/job/oss-swagger-parser-2.0)
 
 ## Overview 
 This is the swagger parser project, which reads OpenAPI Specifications into current Java POJOs.  It also provides a simple framework to add additional converters from different formats into the Swagger objects, making the entire toolchain available.
@@ -99,7 +97,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.6-SNAPSHOT</version>
+  <version>2.0.5</version>
 </dependency>
 
 ```
@@ -110,7 +108,7 @@ or
 <dependency>
   <groupId>io.swagger.parser.v3</groupId>
   <artifactId>swagger-parser</artifactId>
-  <version>2.0.6-SNAPSHOT</version>
+  <version>2.0.5</version>
 </dependency>
 
 ```
diff --git a/modules/swagger-parser-core/pom.xml b/modules/swagger-parser-core/pom.xml
index 2076a651..1be34981 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.6-SNAPSHOT</version>
+        <version>2.0.5</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 5fe0918d..53c0a8ca 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.6-SNAPSHOT</version>
+        <version>2.0.5</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/modules/swagger-parser-v3/pom.xml b/modules/swagger-parser-v3/pom.xml
index aef571d0..abcbccb8 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.6-SNAPSHOT</version>
+        <version>2.0.5</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
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 a7dcca31..0c8e3340 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
@@ -1122,14 +1122,13 @@ public class OpenAPIDeserializer {
                 JsonNode ref = node.get("$ref");
                 if (ref != null) {
                     if (ref.getNodeType().equals(JsonNodeType.STRING)) {
-                        PathItem pathItem = new PathItem();
                         String mungedRef = mungedRef(ref.textValue());
                         if (mungedRef != null) {
-                            pathItem.set$ref(mungedRef);
+                            callback.set$ref(mungedRef);
                         }else{
-                            pathItem.set$ref(ref.textValue());
+                            callback.set$ref(ref.textValue());
                         }
-                        return callback.addPathItem(name,pathItem);
+                        return callback;
                     } else {
                         result.invalidType(location, "$ref", "string", node);
                         return null;
diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml
index 8acd8c28..6c73e870 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.6-SNAPSHOT</version>
+        <version>2.0.5</version>
         <relativePath>../..</relativePath>
     </parent>
     <modelVersion>4.0.0</modelVersion>
diff --git a/pom.xml b/pom.xml
index 1d5a3c21..85c87b5a 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.6-SNAPSHOT</version>
+    <version>2.0.5</version>
     <packaging>pom</packaging>
     <name>swagger-parser-project</name>
     <developers>
@@ -263,7 +263,7 @@
         </repository>
     </repositories>
     <properties>
-        <swagger-parser-v2-version>1.0.40-SNAPSHOT</swagger-parser-v2-version>
+        <swagger-parser-v2-version>1.0.39</swagger-parser-v2-version>
         <commons-io-version>2.4</commons-io-version>
         <slf4j-version>1.6.3</slf4j-version>
         <swagger-core-version>2.0.4</swagger-core-version>


Test patch:
diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java
index 1d42b0d0..d238da89 100644
--- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java
+++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java
@@ -339,11 +339,11 @@ public class OpenAPIResolverTest {
 
         Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
         // internal callback reference
-        assertEquals(callbacks.get("referenced").get("$ref").get$ref(),"#/components/callbacks/failed");
+        assertEquals(callbacks.get("referenced").get$ref(),"#/components/callbacks/failed");
         //callback pathItem -> operation ->requestBody
         assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(),"#/components/requestBodies/requestBody3");
         //remote callback ref
-        assertEquals(callbacks.get("remoteCallback").get("$ref").get$ref(),"#/components/callbacks/callback");
+        assertEquals(callbacks.get("remoteCallback").get$ref(),"http://localhost:" + serverPort + "/remote/callback");
 
     }
 
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 34c60ece..fff345f9 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
@@ -403,4 +403,19 @@ public class OpenAPIParserTest {
         assertEquals(prop.get$ref(),"#/components/schemas/simpleIDType_v01");
 
     }
+
+    @Test
+    public void testIssue879() {
+        OpenAPIParser openApiParser = new OpenAPIParser();
+        ParseOptions options = new ParseOptions();
+        OpenAPI openAPI = openApiParser.readLocation("issue_879.yaml", null, options).getOpenAPI();
+
+        String ref = openAPI.getPaths()
+                .get("/register")
+                .getPost()
+                .getCallbacks()
+                .get("myEvent")
+                .get$ref();
+        assertEquals(ref, "#/components/callbacks/callbackEvent");
+    }
 }
diff --git a/modules/swagger-parser/src/test/resources/issue_879.yaml b/modules/swagger-parser/src/test/resources/issue_879.yaml
new file mode 100644
index 00000000..103c9a18
--- /dev/null
+++ b/modules/swagger-parser/src/test/resources/issue_879.yaml
@@ -0,0 +1,59 @@
+openapi: 3.0.0
+info:
+  title: Callback with ref Example
+  version: 1.0.0
+paths:
+  /register:
+    post:
+      summary: Subscribe to a webhook
+      requestBody:
+        required: true
+        content:
+          application/json:
+            schema:
+              type: object
+              properties:
+                callbackUrl: # Callback URL
+                  type: string
+                  format: uri
+                  example: https://myserver.com/send/callback/here
+              required:
+              - callbackUrl
+      responses:
+        '200':
+          description: subscription successfully created
+          content:
+            application/json:
+              schema:
+                type: object
+                description: subscription information
+                required:
+                - subscriptionId
+                properties:
+                  subscriptionId:
+                    description: unique identifier
+                    type: string
+                    example: 2531329f-fb09-4ef7-887e-84e648214436
+      callbacks:
+        myEvent:
+          $ref: '#/components/callbacks/callbackEvent'
+components:
+  callbacks:
+    callbackEvent:
+      '{$request.body#/callbackUrl}':
+        post:
+          requestBody: # Contents of the callback message
+            required: true
+            content:
+              application/json:
+                schema:
+                  type: object
+                  properties:
+                    message:
+                      type: string
+                      example: Some event happened
+                  required:
+                  - message
+          responses:
+            '200':
+              description: ok
\ No newline at end of file