Skip to content

Commit

Permalink
Merge pull request #878 from ymohdriz/branch_fix_parameter
Browse files Browse the repository at this point in the history
Fix for issue 877
  • Loading branch information
gracekarina committed Oct 18, 2018
2 parents 87beaa2 + 948a02a commit b4a9ca3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Expand Up @@ -49,6 +49,7 @@ public ResolverFully(boolean aggregateCombinators) {
private Map<String, Schema> schemas;
private Map<String, Schema> resolvedModels = new HashMap<>();
private Map<String, Example> examples;
private Map<String, Parameter> parameters;
private Map<String, RequestBody> requestBodies;
private Map<String, Link> links;

Expand All @@ -74,10 +75,18 @@ public void resolveFully(OpenAPI openAPI) {
}
}


if (openAPI.getComponents() != null && openAPI.getComponents().getParameters() != null) {
parameters = openAPI.getComponents().getParameters();
if (parameters == null) {
parameters = new HashMap<>();
}
}
if (openAPI.getComponents() != null && openAPI.getComponents().getLinks() != null) {
links = openAPI.getComponents().getLinks();
if (links == null) {
links = new HashMap<>();

}
}

Expand All @@ -94,6 +103,7 @@ public void resolvePath(PathItem pathItem){
// inputs
if (op.getParameters() != null) {
for (Parameter parameter : op.getParameters()) {
parameter = parameter.get$ref() != null ? resolveParameter(parameter) : parameter;
if (parameter.getSchema() != null) {
Schema resolved = resolveSchema(parameter.getSchema());
if (resolved != null) {
Expand Down Expand Up @@ -202,6 +212,18 @@ public RequestBody resolveRequestBody(RequestBody requestBody){
return requestBody;
}

public Parameter resolveParameter(Parameter parameter){
String $ref = parameter.get$ref();
RefFormat refFormat = computeRefFormat($ref);
if (!isAnExternalRefFormat(refFormat)){
if (parameters != null && !parameters.isEmpty()) {
String referenceKey = computeDefinitionName($ref);
return parameters.getOrDefault(referenceKey, parameter);
}
}
return parameter;
}

public Schema resolveSchema(Schema schema) {
if(schema.get$ref() != null) {
String ref= schema.get$ref();
Expand Down
Expand Up @@ -1538,6 +1538,17 @@ public void shouldParseRequestBody() {
assertEquals(actualPathContent, actualComponentContent);
}

@Test
public void shouldParseParameters() {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolveFully(true);
OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_877.yaml", null, parseOptions);
Parameter parameter = openAPI.getPaths().get("/adopt").getGet().getParameters().get(0);
assertNotNull(parameter);
assertEquals(parameter.getIn(), "path");
assertEquals(parameter.getName(), "playerId");
}

@Test
public void testIssue884() {
ParseOptions parseOptions = new ParseOptions();
Expand All @@ -1547,6 +1558,7 @@ public void testIssue884() {
String operationId = links.get("userRepository").getOperationId();
assertEquals(operationId, "getRepository");
}

@Test
public void testLinkIssue() {
ParseOptions parseOptions = new ParseOptions();
Expand All @@ -1557,8 +1569,6 @@ public void testLinkIssue() {
assertEquals(requestBody, "$response.body#/slug");
}



private static int getDynamicPort() {
return new Random().ints(10000, 20000).findFirst().getAsInt();
}
Expand Down
27 changes: 27 additions & 0 deletions modules/swagger-parser-v3/src/test/resources/issue_877.yaml
@@ -0,0 +1,27 @@
openapi: 3.0.0
servers:
- url: 'http://localhost:8000/v2/api'
info:
version: 1.0.0
title: Swagger Petstore
paths:
/adopt:
get:
tags:
- pet
summary: Find pet by ID
description: It gets pets
operationId: getPetById
parameters:
- $ref: '#/components/parameters/limit'
responses:
'200':
description: successful operation
components:
parameters:
limit:
name: playerId
in: path
required: true
schema:
type: string

0 comments on commit b4a9ca3

Please sign in to comment.