Skip to content

Commit b4a9ca3

Browse files
authored
Merge pull request #878 from ymohdriz/branch_fix_parameter
Fix for issue 877
2 parents 87beaa2 + 948a02a commit b4a9ca3

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public ResolverFully(boolean aggregateCombinators) {
4949
private Map<String, Schema> schemas;
5050
private Map<String, Schema> resolvedModels = new HashMap<>();
5151
private Map<String, Example> examples;
52+
private Map<String, Parameter> parameters;
5253
private Map<String, RequestBody> requestBodies;
5354
private Map<String, Link> links;
5455

@@ -74,10 +75,18 @@ public void resolveFully(OpenAPI openAPI) {
7475
}
7576
}
7677

78+
79+
if (openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
80+
parameters = openAPI.getComponents().getParameters();
81+
if (parameters == null) {
82+
parameters = new HashMap<>();
83+
}
84+
}
7785
if (openAPI.getComponents() != null && openAPI.getComponents().getLinks() != null) {
7886
links = openAPI.getComponents().getLinks();
7987
if (links == null) {
8088
links = new HashMap<>();
89+
8190
}
8291
}
8392

@@ -94,6 +103,7 @@ public void resolvePath(PathItem pathItem){
94103
// inputs
95104
if (op.getParameters() != null) {
96105
for (Parameter parameter : op.getParameters()) {
106+
parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
97107
if (parameter.getSchema() != null) {
98108
Schema resolved = resolveSchema(parameter.getSchema());
99109
if (resolved != null) {
@@ -202,6 +212,18 @@ public RequestBody resolveRequestBody(RequestBody requestBody){
202212
return requestBody;
203213
}
204214

215+
public Parameter resolveParameter(Parameter parameter){
216+
String $ref = parameter.get$ref();
217+
RefFormat refFormat = computeRefFormat($ref);
218+
if (!isAnExternalRefFormat(refFormat)){
219+
if (parameters != null && !parameters.isEmpty()) {
220+
String referenceKey = computeDefinitionName($ref);
221+
return parameters.getOrDefault(referenceKey, parameter);
222+
}
223+
}
224+
return parameter;
225+
}
226+
205227
public Schema resolveSchema(Schema schema) {
206228
if(schema.get$ref() != null) {
207229
String ref= schema.get$ref();

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,17 @@ public void shouldParseRequestBody() {
15381538
assertEquals(actualPathContent, actualComponentContent);
15391539
}
15401540

1541+
@Test
1542+
public void shouldParseParameters() {
1543+
ParseOptions parseOptions = new ParseOptions();
1544+
parseOptions.setResolveFully(true);
1545+
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_877.yaml", null, parseOptions);
1546+
Parameter parameter = openAPI.getPaths().get("/adopt").getGet().getParameters().get(0);
1547+
assertNotNull(parameter);
1548+
assertEquals(parameter.getIn(), "path");
1549+
assertEquals(parameter.getName(), "playerId");
1550+
}
1551+
15411552
@Test
15421553
public void testIssue884() {
15431554
ParseOptions parseOptions = new ParseOptions();
@@ -1547,6 +1558,7 @@ public void testIssue884() {
15471558
String operationId = links.get("userRepository").getOperationId();
15481559
assertEquals(operationId, "getRepository");
15491560
}
1561+
15501562
@Test
15511563
public void testLinkIssue() {
15521564
ParseOptions parseOptions = new ParseOptions();
@@ -1557,8 +1569,6 @@ public void testLinkIssue() {
15571569
assertEquals(requestBody, "$response.body#/slug");
15581570
}
15591571

1560-
1561-
15621572
private static int getDynamicPort() {
15631573
return new Random().ints(10000, 20000).findFirst().getAsInt();
15641574
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
openapi: 3.0.0
2+
servers:
3+
- url: 'http://localhost:8000/v2/api'
4+
info:
5+
version: 1.0.0
6+
title: Swagger Petstore
7+
paths:
8+
/adopt:
9+
get:
10+
tags:
11+
- pet
12+
summary: Find pet by ID
13+
description: It gets pets
14+
operationId: getPetById
15+
parameters:
16+
- $ref: '#/components/parameters/limit'
17+
responses:
18+
'200':
19+
description: successful operation
20+
components:
21+
parameters:
22+
limit:
23+
name: playerId
24+
in: path
25+
required: true
26+
schema:
27+
type: string

0 commit comments

Comments
 (0)