Skip to content

Commit 08120fd

Browse files
authored
Merge pull request #1322 from swagger-api/issue-1309
Applying regular expression only to objects under components
2 parents e6255f8 + 6b40743 commit 08120fd

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

@@ -992,7 +992,7 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
992992

993993
ObjectNode examplesObject = getObject("examples",contentNode,false,location,result);
994994
if(examplesObject!=null) {
995-
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
995+
mediaType.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result, false));
996996
}
997997

998998
Object example = getAnyExample("example",contentNode, location,result);
@@ -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);
@@ -1565,7 +1571,7 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15651571

15661572
ObjectNode examplesObject = getObject("examples",obj,false,location,result);
15671573
if(examplesObject!=null) {
1568-
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result));
1574+
parameter.setExamples(getExamples(examplesObject, String.format("%s.%s", location, "examples"), result,false));
15691575
}
15701576

15711577
Object example = getAnyExample("example", obj, location,result);
@@ -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)) {
@@ -1683,7 +1691,7 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
16831691

16841692
ObjectNode examplesObject = getObject("examples",headerNode,false,location,result);
16851693
if(examplesObject!=null) {
1686-
header.setExamples(getExamples(examplesObject, location, result));
1694+
header.setExamples(getExamples(examplesObject, location, result, false));
16871695
}
16881696

16891697
Object example = getAnyExample("example", headerNode, location,result);
@@ -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,17 +2533,19 @@ private byte[] toBytes( String byteString) {
25212533

25222534

25232535

2524-
public Map<String, Example> getExamples(ObjectNode obj, String location, ParseResult result) {
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 (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2533-
exampleName)) {
2534-
result.warning(location, "Example name "+ exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2544+
if(underComponents) {
2545+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2546+
exampleName)) {
2547+
result.warning(location, "Example name " + exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2548+
}
25352549
}
25362550

25372551
JsonNode exampleValue = obj.get(exampleName);
@@ -2654,7 +2668,7 @@ public void setStyle(String value, Parameter parameter, String location, ObjectN
26542668
}
26552669
}
26562670

2657-
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result) {
2671+
public ApiResponses getResponses(ObjectNode node, String location, ParseResult result, boolean underComponents) {
26582672
if (node == null) {
26592673
return null;
26602674
}
@@ -2663,9 +2677,11 @@ public ApiResponses getResponses(ObjectNode node, String location, ParseResult r
26632677
Set<String> keys = getKeys(node);
26642678

26652679
for (String key : keys) {
2666-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2667-
key)) {
2668-
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+
}
26692685
}
26702686

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

27182734
ObjectNode headerObject = getObject("headers", node, false, location, result);
27192735
if (headerObject != null) {
2720-
Map<String, Header> headers = getHeaders(headerObject, location, result);
2736+
Map<String, Header> headers = getHeaders(headerObject, location, result, false);
27212737
if (headers != null && headers.size() > 0) {
27222738
apiResponse.setHeaders(headers);
27232739
}
27242740
}
27252741

27262742
ObjectNode linksObj = getObject("links", node, false, location, result);
27272743
if (linksObj != null) {
2728-
Map<String,Link> links = getLinks(linksObj, location, result);
2744+
Map<String,Link> links = getLinks(linksObj, location, result, false);
27292745
if(links != null && links.size() > 0) {
27302746
apiResponse.setLinks(links);
27312747
}
@@ -2811,13 +2827,13 @@ public Operation getOperation(ObjectNode obj, String location, ParseResult resul
28112827
}
28122828

28132829
ObjectNode responsesNode = getObject("responses", obj, true, location, result);
2814-
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result);
2830+
ApiResponses responses = getResponses(responsesNode, String.format("%s.%s", location, "responses"), result, false);
28152831
if(responses != null) {
28162832
operation.setResponses(responses);
28172833
}
28182834

28192835
ObjectNode callbacksNode = getObject("callbacks", obj, false, location, result);
2820-
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);
28212837
if(callbacks != null){
28222838
operation.setCallbacks(callbacks);
28232839
}
@@ -2893,17 +2909,19 @@ public List<SecurityRequirement> getSecurityRequirementsList(ArrayNode nodes, St
28932909

28942910
}
28952911

2896-
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) {
28972913
if (obj == null) {
28982914
return null;
28992915
}
29002916
Map<String, RequestBody> bodies = new LinkedHashMap<>();
29012917

29022918
Set<String> bodyKeys = getKeys(obj);
29032919
for(String bodyName : bodyKeys) {
2904-
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2905-
bodyName)) {
2906-
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+
}
29072925
}
29082926
JsonNode bodyValue = obj.get(bodyName);
29092927
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)