Repository: swagger-api/swagger-parser
Pull request #1827 (2022-10-13 13:20:29)

Allow deferencing External Parameters and External responses as numbers
Allow deferencing External Parameters and External responses as numbers


Patch:
diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
index c5e08fe0..382ede57 100644
--- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
+++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java
@@ -180,7 +180,16 @@ public class ResolverCache {
         //a definition path is defined, meaning we need to "dig down" through the JSON tree and get the desired entity
         String[] jsonPathElements = definitionPath.split("/");
         for (String jsonPathElement : jsonPathElements) {
-            tree = tree.get(unescapePointer(jsonPathElement));
+            if (tree.isArray()) {
+                try {
+                    tree = tree.get(Integer.valueOf(jsonPathElement));
+                } catch (NumberFormatException numberFormatException) {
+                    //
+                }
+            } else {
+                tree = tree.get(unescapePointer(jsonPathElement));
+            }
+
             //if at any point we do find an element we expect, print and error and abort
             if (tree == null) {
                 throw new RuntimeException("Could not find " + definitionPath + " in contents of " + file);
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 e07c2dfe..2662e3ca 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
@@ -50,7 +50,7 @@ public class OperationProcessor {
 
         RequestBody requestBody = operation.getRequestBody();
         if (requestBody != null) {
-            // This part allows paser to put requestBody inline without the resolveFully
+            // This part allows parser to put requestBody inline without the resolveFully
             // option set to true
             if (requestBody.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveRequestBody()) {
                 requestBodyProcessor.processRequestBody(requestBody);


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 596db6cf..148bfe66 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
@@ -108,6 +108,18 @@ public class OpenAPIV3ParserTest {
 
     }
 
+    @Test
+    public void testParametersAndResponsesAsNumbers() throws Exception {
+        ParseOptions options = new ParseOptions();
+        options.setResolve(true);
+        SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/parametersAsNumbers/swagger.yaml", null, options);
+
+        Assert.assertNotNull(result);
+        Assert.assertNotNull(result.getOpenAPI());
+        Assert.assertEquals(result.getOpenAPI().getPaths().get("/api/deal/{dealId}").getGet().getParameters().get(0).getName(), "dealId");
+        Assert.assertEquals(result.getOpenAPI().getPaths().get("/api/deal/{dealId}").getGet().getResponses().get("200").getDescription(), "Success");
+    }
+
     @Test
     public void testIssue1758() throws Exception{
         ParseOptions options = new ParseOptions();
diff --git a/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/domain.yaml b/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/domain.yaml
new file mode 100644
index 00000000..ae1ef89d
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/domain.yaml
@@ -0,0 +1,114 @@
+openapi: 3.0.0
+info:
+  title: common
+  description: Common components for REST APIs
+  version: v1-test
+
+components:
+  pathitems:
+    GetDeal:
+      get:
+        tags:
+          - Deal
+        summary: Gets the latest version of a deal (based on TimeStamp) or the specified version or the transactional deal by state
+        description: "Versions of a deal are ordered by the TimeStamp, if the TimeStamp is not set, the Timestamp is\r\nset to the creation time."
+        operationId: DealGetVersionOne
+        parameters:
+          - name: dealId
+            in: path
+            description: The deal identifier.
+            required: true
+            style: simple
+            explode: false
+            schema:
+              maxLength: 250
+              type: string
+              description: The deal identifier.
+          - name: version
+            in: query
+            description: The version of the deal
+            required: false
+            style: form
+            explode: true
+            schema:
+              type: string
+              description: The version of the deal
+              nullable: true
+          - name: transform
+            in: query
+            description: 'The transform''s name used to format the output data. <p>Example: vinCRM</p>'
+            required: false
+            style: form
+            explode: true
+            schema:
+              type: string
+              description: The transform's name used to format the output data
+              nullable: true
+        responses:
+          '200':
+            description: Success
+            content:
+              application/vnd.common.v3+json:
+                schema:
+                  $ref: '#/components/schemas/GetDealResponse'
+          '401':
+            description: Unauthorized
+          '403':
+            description: Forbidden
+          '404':
+            description: Not Found
+            content:
+              application/vnd.common.v3+json:
+                schema:
+                  $ref: '#/components/schemas/inlineResponse4041'
+          '503':
+            description: Server Error
+  schemas:
+    ReferenceKeyModel:
+      type: object
+      properties:
+        name:
+          type: string
+          nullable: true
+        createdByClientId:
+          type: string
+          nullable: true
+        value:
+          type: string
+          nullable: true
+        createdByDateTime:
+          type: string
+          format: date-time
+      additionalProperties: false
+    GetDealResponse:
+      type: object
+      properties:
+        id:
+          type: string
+          description: Deal API specified unique identifier
+          nullable: true
+        version:
+          type: string
+          description: The current version of the deal represented as a hash of the contract
+          nullable: true
+        href:
+          type: string
+          description: The href/url for the deal as it was created
+          nullable: true
+        references:
+          type: object
+          additionalProperties:
+            uniqueItems: true
+            type: array
+            items:
+              $ref: '#/components/schemas/ReferenceKeyModel'
+          description: Gets or sets the reference keys.
+          nullable: true
+        createdDate:
+          type: string
+          description: Gets the creation date and time of the deal version
+          format: date-time
+          nullable: true
+      additionalProperties: false
+      description: Full deal model
+    
\ No newline at end of file
diff --git a/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/swagger.yaml b/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/swagger.yaml
new file mode 100644
index 00000000..ceb48257
--- /dev/null
+++ b/modules/swagger-parser-v3/src/test/resources/parametersAsNumbers/swagger.yaml
@@ -0,0 +1,53 @@
+openapi: 3.0.1
+servers:
+  # Added by API Auto Mocking Plugin
+  - description: SwaggerHub API Auto Mocking
+    url: https://demo.com
+info:
+  description: api
+  version: v1
+  title: API
+  contact:
+    name: John Doe
+    email: John.Doe@mail.com
+  license:
+    name: Apache 2.0
+    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
+tags:
+  - name: Payments
+    description: Request payments
+  - name: Incentives
+    description: Retrieve incentives
+  - name: Deals
+    description: Save, update and search deals
+paths:
+  /api/deal/{dealId}:
+    get:
+      tags:
+        - Deals
+      summary: Gets the latest version of a deal (based on TimeStamp) or the specified version or the transactional deal by state
+      description: "Versions of a deal are ordered by the TimeStamp, if the TimeStamp is not set, the Timestamp is\r\nset to the creation time."
+      operationId: DealGetVersionOne
+      parameters:
+        - $ref: 'domain.yaml#/components/pathitems/GetDeal/get/parameters/0' #access by position
+      responses:
+        '200':
+          $ref: 'domain.yaml#/components/pathitems/GetDeal/get/responses/200' #access by name
+        '401':
+          description: Unauthorized
+        '403':
+          description: Forbidden
+        '503':
+          description: Server Error
+components:
+  schemas:
+    FuelType:
+      type: string
+      description: "Describe the fuel type for the vehicle"
+      enum:
+        - gas
+        - unleaded
+        - flexible
+        - other
+        - diesel
+      example: gas
\ No newline at end of file