Skip to content

Commit 4eca80a

Browse files
authored
Merge branch 'master' into issue-1319
2 parents a5908b3 + 08120fd commit 4eca80a

File tree

3 files changed

+111
-55
lines changed

3 files changed

+111
-55
lines changed

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

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -246,46 +246,46 @@ public Components getComponents(ObjectNode obj, String location, ParseResult res
246246

247247
ObjectNode node = getObject("schemas",obj,false, location ,result);
248248
if(node != null) {
249-
components.setSchemas(getSchemas(node,String.format("%s.%s", location, "schemas"),result));
249+
components.setSchemas(getSchemas(node,String.format("%s.%s", location, "schemas"),result, true));
250250
}
251251

252252
node = getObject("responses",obj,false, location,result);
253253
if(node != null) {
254-
components.setResponses(getResponses(node, String.format("%s.%s", location, "responses"),result));
254+
components.setResponses(getResponses(node, String.format("%s.%s", location, "responses"),result, true));
255255
}
256256

257257
node = getObject("parameters",obj,false, location ,result);
258258
if(node != null) {
259-
components.setParameters(getParameters(node, String.format("%s.%s", location, "parameters"),result));
259+
components.setParameters(getParameters(node, String.format("%s.%s", location, "parameters"),result, true));
260260
}
261261
node = getObject("examples",obj,false,location,result);
262262
if(node != null) {
263-
components.setExamples(getExamples(node, String.format("%s.%s", location, "examples"),result));
263+
components.setExamples(getExamples(node, String.format("%s.%s", location, "examples"),result,true));
264264
}
265265

266266
node = getObject("requestBodies",obj,false,location,result);
267267
if(node != null) {
268-
components.setRequestBodies(getRequestBodies(node, String.format("%s.%s", location, "requestBodies"),result));
268+
components.setRequestBodies(getRequestBodies(node, String.format("%s.%s", location, "requestBodies"),result, true));
269269
}
270270

271271
node = getObject("headers",obj,false,location,result);
272272
if(node != null) {
273-
components.setHeaders(getHeaders(node, String.format("%s.%s", location, "headers"),result));
273+
components.setHeaders(getHeaders(node, String.format("%s.%s", location, "headers"),result, true));
274274
}
275275

276276
node = getObject("securitySchemes",obj,false,location,result);
277277
if(node != null) {
278-
components.setSecuritySchemes(getSecuritySchemes(node, String.format("%s.%s", location, "securitySchemes"),result));
278+
components.setSecuritySchemes(getSecuritySchemes(node, String.format("%s.%s", location, "securitySchemes"),result, true));
279279
}
280280

281281
node = getObject("links",obj,false,location,result);
282282
if(node != null) {
283-
components.setLinks(getLinks(node, String.format("%s.%s", location, "links"),result));
283+
components.setLinks(getLinks(node, String.format("%s.%s", location, "links"),result, true));
284284
}
285285

286286
node = getObject("callbacks",obj,false,location,result);
287287
if(node != null) {
288-
components.setCallbacks(getCallbacks(node, String.format("%s.%s", location, "callbacks"),result));
288+
components.setCallbacks(getCallbacks(node, String.format("%s.%s", location, "callbacks"),result, true));
289289
}
290290
components.setExtensions(new LinkedHashMap<>());
291291

@@ -997,7 +997,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
997997

998998
ObjectNode examplesObject = getObject("examples",contentNode,false,location,result);
999999
if(examplesObject!=null) {
1000-
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
1000+
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result, false));
10011001
}
10021002

10031003
Object example = getAnyExample("example",contentNode, location,result);
@@ -1070,7 +1070,7 @@ public Encoding getEncoding(ObjectNode node, String location, ParseResult result
10701070
}
10711071
ObjectNode headersObject = getObject("headers", node, false, location, result);
10721072
if (headersObject!= null){
1073-
encoding.setHeaders(getHeaders(headersObject, location, result));
1073+
encoding.setHeaders(getHeaders(headersObject, location, result, false));
10741074
}
10751075

10761076
Map <String,Object> extensions = getExtensions(node);
@@ -1088,7 +1088,7 @@ public Encoding getEncoding(ObjectNode node, String location, ParseResult result
10881088
return encoding;
10891089
}
10901090

1091-
public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result) {
1091+
public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
10921092
if (obj == null) {
10931093
return null;
10941094
}
@@ -1097,9 +1097,11 @@ public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult r
10971097

10981098
Set<String> linkKeys = getKeys(obj);
10991099
for(String linkName : linkKeys) {
1100-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1101-
linkName)) {
1102-
result.warning(location, "Link name "+ linkName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1100+
if(underComponents) {
1101+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1102+
linkName)) {
1103+
result.warning(location, "Link name " + linkName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1104+
}
11031105
}
11041106

11051107
JsonNode linkValue = obj.get(linkName);
@@ -1163,7 +1165,7 @@ public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
11631165

11641166
ObjectNode headerObject = getObject("headers",linkNode,false,location,result);
11651167
if (headerObject!= null) {
1166-
link.setHeaders(getHeaders(headerObject, location, result));
1168+
link.setHeaders(getHeaders(headerObject, location, result, false));
11671169
}
11681170

11691171
ObjectNode serverObject = getObject("server",linkNode,false,location,result);
@@ -1204,16 +1206,18 @@ private Map<String,String> getLinkParameters(ObjectNode parametersObject, String
12041206
return linkParameters;
12051207
}
12061208

1207-
public Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result){
1209+
public Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result, boolean underComponents){
12081210
if (node == null) {
12091211
return null;
12101212
}
12111213
Map<String, Callback> callbacks = new LinkedHashMap<>();
12121214
Set<String> keys = getKeys(node);
12131215
for(String key : keys) {
1214-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1215-
key)) {
1216-
result.warning(location, "Callback key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1216+
if(underComponents) {
1217+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1218+
key)) {
1219+
result.warning(location, "Callback key " + key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1220+
}
12171221
}
12181222
Callback callback = getCallback((ObjectNode) node.get(key), location, result);
12191223
if (callback != null) {
@@ -1386,7 +1390,7 @@ else if(!v.isValueNode()) {
13861390
return value;
13871391
}
13881392

1389-
public Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result) {
1393+
public Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
13901394
if (obj == null) {
13911395
return null;
13921396
}
@@ -1396,9 +1400,11 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
13961400

13971401
Set<String> parameterKeys = getKeys(obj);
13981402
for(String parameterName : parameterKeys) {
1399-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1400-
parameterName)) {
1401-
result.warning(location, "Parameter name "+ parameterName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1403+
if(underComponents) {
1404+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1405+
parameterName)) {
1406+
result.warning(location, "Parameter name " + parameterName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1407+
}
14021408
}
14031409

14041410
JsonNode parameterValue = obj.get(parameterName);
@@ -1573,7 +1579,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15731579

15741580
ObjectNode examplesObject = getObject("examples",obj,false,location,result);
15751581
if(examplesObject!=null) {
1576-
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
1582+
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result,false));
15771583
}
15781584

15791585
Object example = getAnyExample("example", obj, location,result);
@@ -1607,17 +1613,19 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
16071613
}
16081614

16091615

1610-
public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result) {
1616+
public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
16111617
if (obj == null) {
16121618
return null;
16131619
}
16141620
Map<String, Header> headers = new LinkedHashMap<>();
16151621

16161622
Set<String> headerKeys = getKeys(obj);
16171623
for(String headerName : headerKeys) {
1618-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1619-
headerName)) {
1620-
result.warning(location, "Header name "+ headerName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1624+
if(underComponents) {
1625+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1626+
headerName)) {
1627+
result.warning(location, "Header name " + headerName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1628+
}
16211629
}
16221630
JsonNode headerValue = obj.get(headerName);
16231631
if (!headerValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1691,7 +1699,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
16911699

16921700
ObjectNode examplesObject = getObject("examples",headerNode,false,location,result);
16931701
if(examplesObject!=null) {
1694-
header.setExamples(getExamples(examplesObject, location, result));
1702+
header.setExamples(getExamples(examplesObject, location, result, false));
16951703
}
16961704

16971705
Object example = getAnyExample("example", headerNode, location,result);
@@ -1758,17 +1766,19 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
17581766
return null;
17591767
}
17601768

1761-
public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result) {
1769+
public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
17621770
if (obj == null) {
17631771
return null;
17641772
}
17651773
Map<String, SecurityScheme> securitySchemes = new LinkedHashMap<>();
17661774

17671775
Set<String> securitySchemeKeys = getKeys(obj);
17681776
for(String securitySchemeName : securitySchemeKeys) {
1769-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1770-
securitySchemeName)) {
1771-
result.warning(location, "SecurityScheme name "+ securitySchemeName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1777+
if(underComponents) {
1778+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1779+
securitySchemeName)) {
1780+
result.warning(location, "SecurityScheme name " + securitySchemeName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1781+
}
17721782
}
17731783
JsonNode securitySchemeValue = obj.get(securitySchemeName);
17741784
if (!securitySchemeValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1990,17 +2000,19 @@ public OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String loca
19902000
return oAuthFlow;
19912001
}
19922002

1993-
public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result) {
2003+
public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
19942004
if (obj == null) {
19952005
return null;
19962006
}
19972007
Map<String, Schema> schemas = new LinkedHashMap<>();
19982008

19992009
Set<String> schemaKeys = getKeys(obj);
20002010
for (String schemaName : schemaKeys) {
2001-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2002-
schemaName)) {
2003-
result.warning(location, "Schema name "+ schemaName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2011+
if(underComponents) {
2012+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2013+
schemaName)) {
2014+
result.warning(location, "Schema name " + schemaName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2015+
}
20042016
}
20052017
JsonNode schemaValue = obj.get(schemaName);
20062018
if (!schemaValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -2529,17 +2541,19 @@ private byte[] toBytes( String byteString) {
25292541

25302542

25312543

2532-
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result) {
2544+
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
25332545
if (obj == null) {
25342546
return null;
25352547
}
25362548
Map<String, Example> examples = new LinkedHashMap<>();
25372549

25382550
Set<String> exampleKeys = getKeys(obj);
25392551
for(String exampleName : exampleKeys) {
2540-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2541-
exampleName)) {
2542-
result.warning(location, "Example name "+ exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2552+
if(underComponents) {
2553+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2554+
exampleName)) {
2555+
result.warning(location, "Example name " + exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2556+
}
25432557
}
25442558

25452559
JsonNode exampleValue = obj.get(exampleName);
@@ -2662,7 +2676,7 @@ public void setStyle(String value, Parameter parameter, String location, ObjectN
26622676
}
26632677
}
26642678

2665-
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result) {
2679+
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result, boolean underComponents) {
26662680
if (node == null) {
26672681
return null;
26682682
}
@@ -2671,9 +2685,11 @@ public ApiResponses getResponses(ObjectNode node, String location, ParseResult r
26712685
Set<String> keys = getKeys(node);
26722686

26732687
for (String key : keys) {
2674-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2675-
key)) {
2676-
result.warning(location, "Response key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2688+
if(underComponents) {
2689+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2690+
key)) {
2691+
result.warning(location, "Response key " + key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2692+
}
26772693
}
26782694

26792695
if (key.startsWith("x-")) {
@@ -2725,15 +2741,15 @@ public ApiResponse getResponse(ObjectNode node, String location, ParseResult res
27252741

27262742
ObjectNode headerObject = getObject("headers", node, false, location, result);
27272743
if (headerObject != null) {
2728-
Map<String, Header> headers = getHeaders(headerObject, location, result);
2744+
Map<String, Header> headers = getHeaders(headerObject, location, result, false);
27292745
if (headers != null && headers.size() > 0) {
27302746
apiResponse.setHeaders(headers);
27312747
}
27322748
}
27332749

27342750
ObjectNode linksObj = getObject("links", node, false, location, result);
27352751
if (linksObj != null) {
2736-
Map<String,Link> links = getLinks(linksObj, location, result);
2752+
Map<String,Link> links = getLinks(linksObj, location, result, false);
27372753
if(links != null && links.size() > 0) {
27382754
apiResponse.setLinks(links);
27392755
}
@@ -2819,13 +2835,13 @@ public Operation getOperation(ObjectNode obj, String location, ParseResult resul
28192835
}
28202836

28212837
ObjectNode responsesNode = getObject("responses", obj, true, location, result);
2822-
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result);
2838+
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result, false);
28232839
if(responses != null) {
28242840
operation.setResponses(responses);
28252841
}
28262842

28272843
ObjectNode callbacksNode = getObject("callbacks", obj, false, location, result);
2828-
Map<String,Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result);
2844+
Map<String,Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result, false);
28292845
if(callbacks != null){
28302846
operation.setCallbacks(callbacks);
28312847
}
@@ -2901,17 +2917,19 @@ public List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, St
29012917

29022918
}
29032919

2904-
public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result) {
2920+
public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
29052921
if (obj == null) {
29062922
return null;
29072923
}
29082924
Map<String, RequestBody> bodies = new LinkedHashMap<>();
29092925

29102926
Set<String> bodyKeys = getKeys(obj);
29112927
for(String bodyName : bodyKeys) {
2912-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2913-
bodyName)) {
2914-
result.warning(location, "RequestBody name "+ bodyName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2928+
if(underComponents) {
2929+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2930+
bodyName)) {
2931+
result.warning(location, "RequestBody name " + bodyName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2932+
}
29152933
}
29162934
JsonNode bodyValue = obj.get(bodyName);
29172935
if (!bodyValue.getNodeType().equals(JsonNodeType.OBJECT)) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ public class OpenAPIV3ParserTest {
6666
protected int serverPort = getDynamicPort();
6767
protected WireMockServer wireMockServer;
6868

69+
@Test
70+
public void testIssue1309() {
71+
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
72+
ParseOptions options = new ParseOptions();
73+
options.setResolve(true);
74+
SwaggerParseResult parseResult = openApiParser.readLocation("issue-1309.yaml", null, options);
75+
76+
OpenAPI openAPI = parseResult.getOpenAPI();
77+
assertNotNull(openAPI);
78+
assertEquals(parseResult.getMessages().get(0),"attribute components.schemas.customer-not-found.examples is unexpected");
79+
}
80+
6981
@Test
7082
public void testIssue1316() {
7183
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
@@ -76,7 +88,6 @@ public void testIssue1316() {
7688
OpenAPI openAPI = parseResult.getOpenAPI();
7789
assertNotNull(openAPI);
7890
assertTrue(parseResult.getMessages().size() == 0);
79-
8091
}
8192

8293

0 commit comments

Comments
 (0)