Skip to content

Commit e301aa9

Browse files
authored
Merge pull request #928 from ymohdriz/branch_issue_911
Fix for issue 911
2 parents bda1181 + da17280 commit e301aa9

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,64 @@ public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
530530
}
531531
ObjectNode path = (ObjectNode) pathValue;
532532
PathItem pathObj = getPathItem(path,String.format("%s.'%s'", location,pathName), result);
533+
String[] eachPart = pathName.split("/");
534+
Arrays.stream(eachPart)
535+
.filter(part -> part.startsWith("{") && part.endsWith("}") && part.length() > 2)
536+
.forEach(part -> {
537+
String pathParam = part.substring(1, part.length() - 1);
538+
boolean definedInPathLevel = isPathParamDefined(pathParam, pathObj.getParameters());
539+
if (!definedInPathLevel) {
540+
List<Operation> operationsInAPath = getAllOperationsInAPath(pathObj);
541+
operationsInAPath.forEach(operation -> {
542+
if (!isPathParamDefined(pathParam, operation.getParameters())) {
543+
result.warning(location + ".'" + pathName + "'"," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level");
544+
return;
545+
}
546+
});
547+
}
548+
});
533549
paths.put(pathName, pathObj);
534550
}
535551
}
536552
}
537553
return paths;
538554
}
539555

556+
private boolean isPathParamDefined(String pathParam, List<Parameter> parameters) {
557+
if (parameters == null || parameters.isEmpty()) {
558+
return false;
559+
} else {
560+
Parameter pathParamDefined = parameters.stream()
561+
.filter(parameter -> pathParam.equals(parameter.getName()) && "path".equals(parameter.getIn()))
562+
.findFirst()
563+
.orElse(null);
564+
if (pathParamDefined == null) {
565+
return false;
566+
}
567+
}
568+
return true;
569+
}
570+
571+
private void addToOperationsList(List<Operation> operationsList, Operation operation) {
572+
if (operation == null) {
573+
return;
574+
}
575+
operationsList.add(operation);
576+
}
577+
578+
public List<Operation> getAllOperationsInAPath(PathItem pathObj) {
579+
List<Operation> operations = new ArrayList<>();
580+
addToOperationsList(operations, pathObj.getGet());
581+
addToOperationsList(operations, pathObj.getPut());
582+
addToOperationsList(operations, pathObj.getPost());
583+
addToOperationsList(operations, pathObj.getPatch());
584+
addToOperationsList(operations, pathObj.getDelete());
585+
addToOperationsList(operations, pathObj.getTrace());
586+
addToOperationsList(operations, pathObj.getOptions());
587+
addToOperationsList(operations, pathObj.getHead());
588+
return operations;
589+
}
590+
540591
public PathItem getPathItem(ObjectNode obj, String location, ParseResult result) {
541592

542593

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.testng.annotations.Test;
88

99
import static org.testng.Assert.assertNotNull;
10+
import static org.testng.AssertJUnit.assertEquals;
1011

1112
public class OAIDeserializationTest {
1213
@Test
@@ -34,4 +35,11 @@ public void testDeserializeSimpleDefinition() throws Exception {
3435

3536
assertNotNull(result.getOpenAPI());
3637
}
38+
39+
@Test
40+
public void testIssue911() {
41+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue_911.yaml", null, null);
42+
assertEquals(result.getMessages().size(),1);
43+
assertNotNull(result.getOpenAPI());
44+
}
3745
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.0.0
2+
servers:
3+
- url: 'http://localhost:8000/v2/api'
4+
info:
5+
description: >-
6+
This is a sample server Petstore server. You can find out more about
7+
Swagger at http://swagger.io or on
8+
irc.freenode.net, #swagger. For this sample, you can use the api key
9+
"special-key" to test the authorization filters
10+
version: 1.0.0
11+
title: Swagger Petstore
12+
termsOfService: 'http://swagger.io/terms/'
13+
contact:
14+
15+
license:
16+
name: Apache 2.0
17+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
18+
externalDocs:
19+
description: Find more info here
20+
url: 'https://swagger.io'
21+
tags:
22+
- name: pet
23+
description: Pet Operations
24+
externalDocs:
25+
url: 'http://swagger.io'
26+
- name: user
27+
description: All about the Users
28+
paths:
29+
'/pet/{petId}/{pathParamNotDefined}':
30+
get:
31+
tags:
32+
- pet
33+
summary: Find pet by ID
34+
description: >-
35+
Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API
36+
error conditions
37+
operationId: getPetById
38+
responses:
39+
200:
40+
description: 200 ok
41+
parameters:
42+
- name: petId
43+
in: path
44+
description: ID of pet that needs to be fetched
45+
required: true
46+
schema:
47+
type: integer
48+
format: int64

modules/swagger-parser/src/test/resources/duplicateOperationId.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
},
77
"paths": {
88
"/pets/{id}": {
9+
"parameters" : [
10+
{
11+
"in": "path",
12+
"name": "id",
13+
"schema": {
14+
"type": "string"
15+
}
16+
}
17+
],
918
"get": {
1019
"operationId": "getPetsById",
1120
"responses": {

0 commit comments

Comments
 (0)