Skip to content

Commit 6b40743

Browse files
committed
adding validation to only use the regex when the object is under components - refs #1309
1 parent 512d574 commit 6b40743

File tree

1 file changed

+64
-48
lines changed

1 file changed

+64
-48
lines changed

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

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,17 @@ 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) {
@@ -265,27 +265,27 @@ public Components getComponents(ObjectNode obj, String location, ParseResult res
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

@@ -1065,7 +1065,7 @@ public Encoding getEncoding(ObjectNode node, String location, ParseResult result
10651065
}
10661066
ObjectNode headersObject = getObject("headers", node, false, location, result);
10671067
if (headersObject!= null){
1068-
encoding.setHeaders(getHeaders(headersObject, location, result));
1068+
encoding.setHeaders(getHeaders(headersObject, location, result, false));
10691069
}
10701070

10711071
Map <String,Object> extensions = getExtensions(node);
@@ -1083,7 +1083,7 @@ public Encoding getEncoding(ObjectNode node, String location, ParseResult result
10831083
return encoding;
10841084
}
10851085

1086-
public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result) {
1086+
public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
10871087
if (obj == null) {
10881088
return null;
10891089
}
@@ -1092,9 +1092,11 @@ public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult r
10921092

10931093
Set<String> linkKeys = getKeys(obj);
10941094
for(String linkName : linkKeys) {
1095-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1096-
linkName)) {
1097-
result.warning(location, "Link name "+ linkName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1095+
if(underComponents) {
1096+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1097+
linkName)) {
1098+
result.warning(location, "Link name " + linkName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1099+
}
10981100
}
10991101

11001102
JsonNode linkValue = obj.get(linkName);
@@ -1158,7 +1160,7 @@ public Link getLink(ObjectNode linkNode, String location, ParseResult result) {
11581160

11591161
ObjectNode headerObject = getObject("headers",linkNode,false,location,result);
11601162
if (headerObject!= null) {
1161-
link.setHeaders(getHeaders(headerObject, location, result));
1163+
link.setHeaders(getHeaders(headerObject, location, result, false));
11621164
}
11631165

11641166
ObjectNode serverObject = getObject("server",linkNode,false,location,result);
@@ -1199,16 +1201,18 @@ private Map<String,String> getLinkParameters(ObjectNode parametersObject, String
11991201
return linkParameters;
12001202
}
12011203

1202-
public Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result){
1204+
public Map <String,Callback> getCallbacks(ObjectNode node, String location, ParseResult result, boolean underComponents){
12031205
if (node == null) {
12041206
return null;
12051207
}
12061208
Map<String, Callback> callbacks = new LinkedHashMap<>();
12071209
Set<String> keys = getKeys(node);
12081210
for(String key : keys) {
1209-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1210-
key)) {
1211-
result.warning(location, "Callback key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1211+
if(underComponents) {
1212+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1213+
key)) {
1214+
result.warning(location, "Callback key " + key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1215+
}
12121216
}
12131217
Callback callback = getCallback((ObjectNode) node.get(key), location, result);
12141218
if (callback != null) {
@@ -1381,7 +1385,7 @@ else if(!v.isValueNode()) {
13811385
return value;
13821386
}
13831387

1384-
public Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result) {
1388+
public Map<String, Parameter> getParameters(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
13851389
if (obj == null) {
13861390
return null;
13871391
}
@@ -1391,9 +1395,11 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
13911395

13921396
Set<String> parameterKeys = getKeys(obj);
13931397
for(String parameterName : parameterKeys) {
1394-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1395-
parameterName)) {
1396-
result.warning(location, "Parameter name "+ parameterName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1398+
if(underComponents) {
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\\.\\-_]+$");
1402+
}
13971403
}
13981404

13991405
JsonNode parameterValue = obj.get(parameterName);
@@ -1599,17 +1605,19 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15991605
}
16001606

16011607

1602-
public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result) {
1608+
public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
16031609
if (obj == null) {
16041610
return null;
16051611
}
16061612
Map<String, Header> headers = new LinkedHashMap<>();
16071613

16081614
Set<String> headerKeys = getKeys(obj);
16091615
for(String headerName : headerKeys) {
1610-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1611-
headerName)) {
1612-
result.warning(location, "Header name "+ headerName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1616+
if(underComponents) {
1617+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1618+
headerName)) {
1619+
result.warning(location, "Header name " + headerName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1620+
}
16131621
}
16141622
JsonNode headerValue = obj.get(headerName);
16151623
if (!headerValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1750,17 +1758,19 @@ public Object getAnyExample(String nodeKey,ObjectNode node, String location, Par
17501758
return null;
17511759
}
17521760

1753-
public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result) {
1761+
public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
17541762
if (obj == null) {
17551763
return null;
17561764
}
17571765
Map<String, SecurityScheme> securitySchemes = new LinkedHashMap<>();
17581766

17591767
Set<String> securitySchemeKeys = getKeys(obj);
17601768
for(String securitySchemeName : securitySchemeKeys) {
1761-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1762-
securitySchemeName)) {
1763-
result.warning(location, "SecurityScheme name "+ securitySchemeName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1769+
if(underComponents) {
1770+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1771+
securitySchemeName)) {
1772+
result.warning(location, "SecurityScheme name " + securitySchemeName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1773+
}
17641774
}
17651775
JsonNode securitySchemeValue = obj.get(securitySchemeName);
17661776
if (!securitySchemeValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -1982,17 +1992,19 @@ public OAuthFlow getOAuthFlow(String oAuthFlowType, ObjectNode node, String loca
19821992
return oAuthFlow;
19831993
}
19841994

1985-
public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result) {
1995+
public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
19861996
if (obj == null) {
19871997
return null;
19881998
}
19891999
Map<String, Schema> schemas = new LinkedHashMap<>();
19902000

19912001
Set<String> schemaKeys = getKeys(obj);
19922002
for (String schemaName : schemaKeys) {
1993-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1994-
schemaName)) {
1995-
result.warning(location, "Schema name "+ schemaName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2003+
if(underComponents) {
2004+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2005+
schemaName)) {
2006+
result.warning(location, "Schema name " + schemaName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2007+
}
19962008
}
19972009
JsonNode schemaValue = obj.get(schemaName);
19982010
if (!schemaValue.getNodeType().equals(JsonNodeType.OBJECT)) {
@@ -2521,15 +2533,15 @@ private byte[] toBytes( String byteString) {
25212533

25222534

25232535

2524-
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean UnderComponents) {
2536+
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
25252537
if (obj == null) {
25262538
return null;
25272539
}
25282540
Map<String, Example> examples = new LinkedHashMap<>();
25292541

25302542
Set<String> exampleKeys = getKeys(obj);
25312543
for(String exampleName : exampleKeys) {
2532-
if(UnderComponents) {
2544+
if(underComponents) {
25332545
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
25342546
exampleName)) {
25352547
result.warning(location, "Example name " + exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
@@ -2656,7 +2668,7 @@ public void setStyle(String value, Parameter parameter, String location, ObjectN
26562668
}
26572669
}
26582670

2659-
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result) {
2671+
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result, boolean underComponents) {
26602672
if (node == null) {
26612673
return null;
26622674
}
@@ -2665,9 +2677,11 @@ public ApiResponses getResponses(ObjectNode node, String location, ParseResult r
26652677
Set<String> keys = getKeys(node);
26662678

26672679
for (String key : keys) {
2668-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2669-
key)) {
2670-
result.warning(location, "Response key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2680+
if(underComponents) {
2681+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2682+
key)) {
2683+
result.warning(location, "Response key " + key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2684+
}
26712685
}
26722686

26732687
if (key.startsWith("x-")) {
@@ -2719,15 +2733,15 @@ public ApiResponse getResponse(ObjectNode node, String location, ParseResult res
27192733

27202734
ObjectNode headerObject = getObject("headers", node, false, location, result);
27212735
if (headerObject != null) {
2722-
Map<String, Header> headers = getHeaders(headerObject, location, result);
2736+
Map<String, Header> headers = getHeaders(headerObject, location, result, false);
27232737
if (headers != null && headers.size() > 0) {
27242738
apiResponse.setHeaders(headers);
27252739
}
27262740
}
27272741

27282742
ObjectNode linksObj = getObject("links", node, false, location, result);
27292743
if (linksObj != null) {
2730-
Map<String,Link> links = getLinks(linksObj, location, result);
2744+
Map<String,Link> links = getLinks(linksObj, location, result, false);
27312745
if(links != null && links.size() > 0) {
27322746
apiResponse.setLinks(links);
27332747
}
@@ -2813,13 +2827,13 @@ public Operation getOperation(ObjectNode obj, String location, ParseResult resul
28132827
}
28142828

28152829
ObjectNode responsesNode = getObject("responses", obj, true, location, result);
2816-
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result);
2830+
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result, false);
28172831
if(responses != null) {
28182832
operation.setResponses(responses);
28192833
}
28202834

28212835
ObjectNode callbacksNode = getObject("callbacks", obj, false, location, result);
2822-
Map<String,Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result);
2836+
Map<String,Callback> callbacks = getCallbacks(callbacksNode, String.format("%s.%s", location, "callbacks"), result, false);
28232837
if(callbacks != null){
28242838
operation.setCallbacks(callbacks);
28252839
}
@@ -2895,17 +2909,19 @@ public List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, St
28952909

28962910
}
28972911

2898-
public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result) {
2912+
public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location, ParseResult result, boolean underComponents) {
28992913
if (obj == null) {
29002914
return null;
29012915
}
29022916
Map<String, RequestBody> bodies = new LinkedHashMap<>();
29032917

29042918
Set<String> bodyKeys = getKeys(obj);
29052919
for(String bodyName : bodyKeys) {
2906-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2907-
bodyName)) {
2908-
result.warning(location, "RequestBody name "+ bodyName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2920+
if(underComponents) {
2921+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2922+
bodyName)) {
2923+
result.warning(location, "RequestBody name " + bodyName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2924+
}
29092925
}
29102926
JsonNode bodyValue = obj.get(bodyName);
29112927
if (!bodyValue.getNodeType().equals(JsonNodeType.OBJECT)) {

0 commit comments

Comments
 (0)