Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit d0aa5a4

Browse files
authored
Merge pull request swagger-api#935 from saileshsingh/bufFix3
Fix for swagger-api#934
2 parents a3d3a42 + 75fe13e commit d0aa5a4

5 files changed

Lines changed: 212 additions & 7 deletions

File tree

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class OpenAPIDeserializer {
9090
private static final String COOKIE_PARAMETER = "cookie";
9191
private static final String PATH_PARAMETER = "path";
9292
private static final String HEADER_PARAMETER = "header";
93-
93+
private Components components;
9494
private final Set<String> operationIDs = new HashSet<>();
9595

9696
public SwaggerParseResult deserialize(JsonNode rootNode) {
@@ -134,17 +134,19 @@ public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
134134
openAPI.setInfo(info);
135135
}
136136

137+
obj = getObject("components", rootNode, false, location, result);
138+
if (obj != null) {
139+
Components components = getComponents(obj, "components", result);
140+
openAPI.setComponents(components);
141+
this.components=components;
142+
}
143+
137144
obj = getObject("paths", rootNode, true, location, result);
138145
if (obj != null) {
139146
Paths paths = getPaths(obj, "paths", result);
140147
openAPI.setPaths(paths);
141148
}
142149

143-
obj = getObject("components", rootNode, false, location, result);
144-
if (obj != null) {
145-
Components components = getComponents(obj, "components", result);
146-
openAPI.setComponents(components);
147-
}
148150

149151
ArrayNode array = getArray("servers", rootNode, false, location, result);
150152
if (array != null && array.size() > 0) {
@@ -2040,6 +2042,25 @@ public Schema getSchema(ObjectNode node, String location, ParseResult result){
20402042
JsonNode ref = node.get("$ref");
20412043
if (ref != null) {
20422044
if (ref.getNodeType().equals(JsonNodeType.STRING)) {
2045+
2046+
if(location.startsWith("paths")){
2047+
try{
2048+
String components[]=ref.asText().split("#/components");
2049+
if((ref.asText().startsWith("#/components"))&&(components.length>1)) {
2050+
String[] childComponents = components[1].split("/");
2051+
String[] newChildComponents = Arrays.copyOfRange(childComponents, 1, childComponents.length);
2052+
boolean isValidComponent = ReferenceValidator.valueOf(newChildComponents[0])
2053+
.validateComponent(this.components,
2054+
newChildComponents[1]);
2055+
if (!isValidComponent) {
2056+
result.missing(location, ref.asText());
2057+
}
2058+
}
2059+
}catch (Exception e){
2060+
result.missing(location, ref.asText());
2061+
}
2062+
}
2063+
20432064
String mungedRef = mungedRef(ref.textValue());
20442065
if (mungedRef != null) {
20452066
schema.set$ref(mungedRef);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package io.swagger.v3.parser.util;
2+
3+
import io.swagger.v3.oas.models.Components;
4+
5+
6+
public enum ReferenceValidator {
7+
8+
schemas {
9+
@Override
10+
public boolean validateComponent(Components components,String reference) {
11+
return components.getSchemas().containsKey(reference);
12+
}
13+
},
14+
responses {
15+
@Override
16+
public boolean validateComponent(Components components,String reference) {
17+
return components.getResponses().containsKey(reference);
18+
}
19+
},
20+
parameters {
21+
@Override
22+
public boolean validateComponent(Components components,String reference) {
23+
return components.getParameters().containsKey(reference);
24+
}
25+
},
26+
examples {
27+
@Override
28+
public boolean validateComponent(Components components,String reference) {
29+
return components.getExamples().containsKey(reference);
30+
}
31+
},
32+
requestBodies {
33+
@Override
34+
public boolean validateComponent(Components components,String reference) {
35+
return components.getRequestBodies().containsKey(reference);
36+
}
37+
},
38+
headers {
39+
@Override
40+
public boolean validateComponent(Components components,String reference) {
41+
return components.getHeaders().containsKey(reference);
42+
}
43+
},
44+
securitySchemes {
45+
@Override
46+
public boolean validateComponent(Components components,String reference) {
47+
return components.getSecuritySchemes().containsKey(reference);
48+
}
49+
},
50+
links {
51+
@Override
52+
public boolean validateComponent(Components components,String reference) {
53+
return components.getLinks().containsKey(reference);
54+
}
55+
},
56+
callbacks {
57+
@Override
58+
public boolean validateComponent(Components components,String reference) {
59+
return components.getCallbacks().containsKey(reference);
60+
}
61+
};
62+
63+
64+
public abstract boolean validateComponent(Components components,String reference);
65+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public void testPetstore() throws Exception {
824824
SwaggerParseResult result = parser.readLocation("src/test/resources/petstore.yaml", null, options);
825825

826826
assertNotNull(result);
827-
assertTrue(result.getMessages().isEmpty());
827+
assertTrue(result.getMessages().size()==1);
828828

829829
OpenAPI openAPI = result.getOpenAPI();
830830
Map<String, Schema> definitions = openAPI.getComponents().getSchemas();

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ public void testIssue892() {
9393
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
9494
}
9595

96+
@Test
97+
public void testIssue934() {
98+
SwaggerParseResult result = new OpenAPIParser().readLocation("issue-934.yaml", null, null);
99+
100+
assertNotNull(result);
101+
assertEquals(result.getMessages().size(),1);
102+
assertNotNull(result.getOpenAPI());
103+
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
104+
}
105+
96106
@Test
97107
public void testIssue768() {
98108
ParseOptions options = new ParseOptions();
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
openapi: "3.0.1"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
200:
26+
description: An paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
default:
37+
description: unexpected error
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/Error"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
responses:
48+
201:
49+
description: Null response
50+
default:
51+
description: unexpected error
52+
content:
53+
application/json:
54+
schema:
55+
$ref: "#/components/schemas/Pets"
56+
/pets/{petId}:
57+
get:
58+
summary: Info for a specific pet
59+
operationId: showPetById
60+
tags:
61+
- pets
62+
parameters:
63+
- name: petId
64+
in: path
65+
required: true
66+
description: The id of the pet to retrieve
67+
schema:
68+
type: string
69+
responses:
70+
200:
71+
description: Expected response to a valid request
72+
content:
73+
application/json:
74+
schema:
75+
$ref: "#/components/schemas/APIResponses"
76+
default:
77+
description: unexpected error
78+
content:
79+
application/json:
80+
schema:
81+
$ref: './issue749-reference.yaml#/SomeId'
82+
components:
83+
schemas:
84+
Pet:
85+
required:
86+
- id
87+
- name
88+
properties:
89+
id:
90+
type: integer
91+
format: int64
92+
name:
93+
type: string
94+
tag:
95+
type: string
96+
Pets:
97+
type: array
98+
items:
99+
$ref: "#/components/schemas/Pet"
100+
Error:
101+
required:
102+
- code
103+
- message
104+
properties:
105+
code:
106+
type: integer
107+
format: int32
108+
message:
109+
type: string

0 commit comments

Comments
 (0)