Repository: swagger-api/swagger-parser
Pull request #1609 (2021-09-23 18:16:20)

Fix and Issue 1561: Add resolved responses to components section in main spec
Fix and Issue 1561:
Add resolved responses to components section in main spec


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
index 06648258..a675833a 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java
@@ -391,6 +391,7 @@ public final class ExternalRefProcessor {
             }
         }
         newRef = possiblyConflictingDefinitionName;
+        openAPI.getComponents().addResponses(newRef, response);
         cache.putRenamedRef($ref, newRef);
 
         if(response != null) {
@@ -408,6 +409,7 @@ public final class ExternalRefProcessor {
         return newRef;
     }
 
+
     public String processRefToExternalRequestBody(String $ref, RefFormat refFormat) {
         String renamedRef = cache.getRenamedRef($ref);
         if(renamedRef != null) {
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/OperationProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/OperationProcessor.java
index d5155db3..cc6cf566 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/OperationProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/OperationProcessor.java
@@ -51,6 +51,7 @@ public class OperationProcessor {
             for (String responseCode : responses.keySet()) {
                 ApiResponse response = responses.get(responseCode);
                 if(response != null) {
+                    //This part allows parser to put response schema inline without the resolveFully option set to true
                     if (response.get$ref() != null) {
 
                         responseProcessor.processResponse(response);
@@ -63,9 +64,7 @@ public class OperationProcessor {
                             responses.put(responseCode, resolvedResponse);
                         }
                     }
-
                     responseProcessor.processResponse(response);
-
                 }
             }
         }
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ResponseProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ResponseProcessor.java
index 926641f4..959026de 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ResponseProcessor.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ResponseProcessor.java
@@ -81,7 +81,11 @@ public class ResponseProcessor {
         RefFormat refFormat = computeRefFormat(response.get$ref());
         String $ref = response.get$ref();
         if (isAnExternalRefFormat(refFormat)){
-            externalRefProcessor.processRefToExternalResponse($ref, refFormat);
+            final String newRef =  externalRefProcessor.processRefToExternalResponse($ref, refFormat);
+
+            if (newRef != null) {
+                response.set$ref(newRef);
+            }
         }
     }
 }
\ No newline at end of file


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 28e82cd9..4b63a6d9 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,15 @@ public class OpenAPIV3ParserTest {
     protected int serverPort = getDynamicPort();
     protected WireMockServer wireMockServer;
 
+    @Test
+    public void testIssue1561() {
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue-1561/swagger.yaml", null, options);
+        OpenAPI openAPI = result.getOpenAPI();
+        assertTrue(openAPI.getComponents().getResponses().size() == 3);
+    }
+
     @Test
     public void testAnonymousModelAllOf() {
         ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1561/exceptions.yml b/modules/swagger-parser-v3/src/test/resources/issue-1561/exceptions.yml
new file mode 100644
index 00000000..a94d3d93
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1561/exceptions.yml
@@ -0,0 +1,67 @@
+components:
+  schemas:
+    Exception:
+      type: object
+    Rfc7807Exception:
+      description: The Rfc7807 specification for exceptions.
+      allOf:
+        - $ref: '#/components/schemas/Exception'
+        - type: object
+          properties:
+            type:
+              type: string
+              description: A URI reference that identifies the problem type.
+          required:
+            - type
+            - title
+            - status
+            - detail
+    AbsisExceptionType:
+      type: string
+      enum: [VALIDATION, BUSINESS, RUNTIME, CHALLENGE, NOTFOUND, UNAUTHORIZED, AUTHENTICATION]
+      description: The possible types of an AbsisException.
+    AbsisFieldError:
+      type: object
+      description: The representation of a validation error.
+      properties:
+        code:
+          type: string
+          description: The error code.
+      example:
+        code: request
+        parameters:
+          - codes:
+              - aCode
+              - otherCode
+        value: NotNull
+    AbsisException:
+      description: Representation of an exception.
+      allOf:
+        - $ref: '#/components/schemas/Rfc7807Exception'
+        - type: object
+          properties:
+            _class:
+              type: string
+              description: The canonical name of the class AbsisException.
+            cause:
+              $ref: '#/components/schemas/AbsisException'
+              description: The AbsisException wrapping the exception that originated this exception.
+  responses:
+    '400':
+      description: Error when validating input parameters or request body.
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/AbsisException'
+    '404':
+      description: The endpoint does not exists or the attempted resource does not exist.
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/AbsisException'
+    '500':
+      description: Unexpected error occurred at one of the layers of the microservice, controller, business service, repository etc.
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/AbsisException'
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1561/swagger.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1561/swagger.yaml
new file mode 100644
index 00000000..6051b0b4
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/issue-1561/swagger.yaml
@@ -0,0 +1,67 @@
+openapi: 3.0.0
+info:
+  description: The Trazabilidad API offers the functionalities of the new trace regardless of the channel in which it is in invoked.
+  version: 1.0.2
+  title: ApiTrazabilidad
+
+tags:
+  - name: Trazabilidad
+    description: Trazabilidad API
+paths:
+  '/operationalServices/tracks/scopes/{scopeId}/processes/{processId}/petitions':
+    post:
+      tags:
+        - createPetition
+      summary: Creates a new instance in the traceability operation.
+      operationId: createPetition
+      description: Creates a new instance in the traceability operation.
+      parameters:
+        - in: path
+          name: processId
+          description: instance process ID
+          required: true
+          schema:
+            type: integer
+      requestBody:
+        $ref: '#/components/requestBodies/TrazabilidadBodyRequest'
+      responses:
+        '201':
+          description: Created
+          content:
+            application/json:
+              schema:
+                $ref: '#/components/schemas/BodyOutResponse'
+        '400':
+          $ref: >-
+            exceptions.yml#/components/responses/400
+        '404':
+          $ref: >-
+            exceptions.yml#/components/responses/404
+        '500':
+          $ref: >-
+            exceptions.yml#/components/responses/500
+
+components:
+  requestBodies:
+    TrazabilidadBodyRequest:
+      content:
+        application/json:
+          schema:
+            $ref: '#/components/schemas/TrazabilidadBodyRequest'
+      description: body request
+      required: true
+  schemas:
+    BodyOutResponse:
+      type: object
+      properties:
+        petitionId:
+          type: string
+          description: The ID of the request that has been created
+          example: "971205125921"
+    TrazabilidadBodyRequest:
+      type: object
+      properties:
+        action:
+          type: string
+          description: Action to be performed. values -- alta , mod , fin , baja
+          example: "alta"
\ No newline at end of file