Skip to content

Commit 9fee155

Browse files
gracekarinafrantuma
authored andcommitted
Handle 3.0 dereferencing paths accessing arrays by index
1 parent 6e83a8b commit 9fee155

File tree

5 files changed

+190
-2
lines changed

5 files changed

+190
-2
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/ResolverCache.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,16 @@ else if (rootPath != null) {
180180
//a definition path is defined, meaning we need to "dig down" through the JSON tree and get the desired entity
181181
String[] jsonPathElements = definitionPath.split("/");
182182
for (String jsonPathElement : jsonPathElements) {
183-
tree = tree.get(unescapePointer(jsonPathElement));
183+
if (tree.isArray()) {
184+
try {
185+
tree = tree.get(Integer.valueOf(jsonPathElement));
186+
} catch (NumberFormatException numberFormatException) {
187+
//
188+
}
189+
} else {
190+
tree = tree.get(unescapePointer(jsonPathElement));
191+
}
192+
184193
//if at any point we do find an element we expect, print and error and abort
185194
if (tree == null) {
186195
throw new RuntimeException("Could not find " + definitionPath + " in contents of " + file);

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/OperationProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void processOperation(Operation operation) {
5050

5151
RequestBody requestBody = operation.getRequestBody();
5252
if (requestBody != null) {
53-
// This part allows paser to put requestBody inline without the resolveFully
53+
// This part allows parser to put requestBody inline without the resolveFully
5454
// option set to true
5555
if (requestBody.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveRequestBody()) {
5656
requestBodyProcessor.processRequestBody(requestBody);

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ public void testIssue1780() {
108108

109109
}
110110

111+
@Test
112+
public void testParametersAndResponsesAsNumbers() throws Exception {
113+
ParseOptions options = new ParseOptions();
114+
options.setResolve(true);
115+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/parametersAsNumbers/swagger.yaml", null, options);
116+
117+
Assert.assertNotNull(result);
118+
Assert.assertNotNull(result.getOpenAPI());
119+
Assert.assertEquals(result.getOpenAPI().getPaths().get("/api/deal/{dealId}").getGet().getParameters().get(0).getName(), "dealId");
120+
Assert.assertEquals(result.getOpenAPI().getPaths().get("/api/deal/{dealId}").getGet().getResponses().get("200").getDescription(), "Success");
121+
}
122+
111123
@Test
112124
public void testIssue1758() throws Exception{
113125
ParseOptions options = new ParseOptions();
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
openapi: 3.0.0
2+
info:
3+
title: common
4+
description: Common components for REST APIs
5+
version: v1-test
6+
7+
components:
8+
pathitems:
9+
GetDeal:
10+
get:
11+
tags:
12+
- Deal
13+
summary: Gets the latest version of a deal (based on TimeStamp) or the specified version or the transactional deal by state
14+
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."
15+
operationId: DealGetVersionOne
16+
parameters:
17+
- name: dealId
18+
in: path
19+
description: The deal identifier.
20+
required: true
21+
style: simple
22+
explode: false
23+
schema:
24+
maxLength: 250
25+
type: string
26+
description: The deal identifier.
27+
- name: version
28+
in: query
29+
description: The version of the deal
30+
required: false
31+
style: form
32+
explode: true
33+
schema:
34+
type: string
35+
description: The version of the deal
36+
nullable: true
37+
- name: transform
38+
in: query
39+
description: 'The transform''s name used to format the output data. <p>Example: vinCRM</p>'
40+
required: false
41+
style: form
42+
explode: true
43+
schema:
44+
type: string
45+
description: The transform's name used to format the output data
46+
nullable: true
47+
responses:
48+
'200':
49+
description: Success
50+
content:
51+
application/vnd.common.v3+json:
52+
schema:
53+
$ref: '#/components/schemas/GetDealResponse'
54+
'401':
55+
description: Unauthorized
56+
'403':
57+
description: Forbidden
58+
'404':
59+
description: Not Found
60+
content:
61+
application/vnd.common.v3+json:
62+
schema:
63+
$ref: '#/components/schemas/inlineResponse4041'
64+
'503':
65+
description: Server Error
66+
schemas:
67+
ReferenceKeyModel:
68+
type: object
69+
properties:
70+
name:
71+
type: string
72+
nullable: true
73+
createdByClientId:
74+
type: string
75+
nullable: true
76+
value:
77+
type: string
78+
nullable: true
79+
createdByDateTime:
80+
type: string
81+
format: date-time
82+
additionalProperties: false
83+
GetDealResponse:
84+
type: object
85+
properties:
86+
id:
87+
type: string
88+
description: Deal API specified unique identifier
89+
nullable: true
90+
version:
91+
type: string
92+
description: The current version of the deal represented as a hash of the contract
93+
nullable: true
94+
href:
95+
type: string
96+
description: The href/url for the deal as it was created
97+
nullable: true
98+
references:
99+
type: object
100+
additionalProperties:
101+
uniqueItems: true
102+
type: array
103+
items:
104+
$ref: '#/components/schemas/ReferenceKeyModel'
105+
description: Gets or sets the reference keys.
106+
nullable: true
107+
createdDate:
108+
type: string
109+
description: Gets the creation date and time of the deal version
110+
format: date-time
111+
nullable: true
112+
additionalProperties: false
113+
description: Full deal model
114+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
openapi: 3.0.1
2+
servers:
3+
# Added by API Auto Mocking Plugin
4+
- description: SwaggerHub API Auto Mocking
5+
url: https://demo.com
6+
info:
7+
description: api
8+
version: v1
9+
title: API
10+
contact:
11+
name: John Doe
12+
email: John.Doe@mail.com
13+
license:
14+
name: Apache 2.0
15+
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
16+
tags:
17+
- name: Payments
18+
description: Request payments
19+
- name: Incentives
20+
description: Retrieve incentives
21+
- name: Deals
22+
description: Save, update and search deals
23+
paths:
24+
/api/deal/{dealId}:
25+
get:
26+
tags:
27+
- Deals
28+
summary: Gets the latest version of a deal (based on TimeStamp) or the specified version or the transactional deal by state
29+
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."
30+
operationId: DealGetVersionOne
31+
parameters:
32+
- $ref: 'domain.yaml#/components/pathitems/GetDeal/get/parameters/0' #access by position
33+
responses:
34+
'200':
35+
$ref: 'domain.yaml#/components/pathitems/GetDeal/get/responses/200' #access by name
36+
'401':
37+
description: Unauthorized
38+
'403':
39+
description: Forbidden
40+
'503':
41+
description: Server Error
42+
components:
43+
schemas:
44+
FuelType:
45+
type: string
46+
description: "Describe the fuel type for the vehicle"
47+
enum:
48+
- gas
49+
- unleaded
50+
- flexible
51+
- other
52+
- diesel
53+
example: gas

0 commit comments

Comments
 (0)