Skip to content

Commit 6dc1d06

Browse files
authored
Merge pull request #933 from Malligarjunan/Issue931
Fixed Issue 931
2 parents 1c3e9f8 + 814290e commit 6dc1d06

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import java.net.URISyntaxException;
5454
import java.net.URL;
5555
import java.util.*;
56+
import java.util.regex.Pattern;
5657
import java.util.stream.Collectors;
5758
import java.util.stream.Stream;
5859

@@ -1019,6 +1020,11 @@ public Map<String, Link> getLinks(ObjectNode obj, String location, ParseResult r
10191020

10201021
Set<String> linkKeys = getKeys(obj);
10211022
for(String linkName : linkKeys) {
1023+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1024+
linkName)) {
1025+
result.warning(location, "Link name "+ linkName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1026+
}
1027+
10221028
JsonNode linkValue = obj.get(linkName);
10231029
if (!linkValue.getNodeType().equals(JsonNodeType.OBJECT)) {
10241030
result.invalidType(location, linkName, "object", linkValue);
@@ -1128,6 +1134,10 @@ public Map <String,Callback> getCallbacks(ObjectNode node, String location, Pars
11281134
Map<String, Callback> callbacks = new LinkedHashMap<>();
11291135
Set<String> keys = getKeys(node);
11301136
for(String key : keys) {
1137+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1138+
key)) {
1139+
result.warning(location, "Callback key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1140+
}
11311141
Callback callback = getCallback((ObjectNode) node.get(key), location, result);
11321142
if (callback != null) {
11331143
callbacks.put(key, callback);
@@ -1307,6 +1317,11 @@ public Map<String, Parameter> getParameters(ObjectNode obj, String location, Par
13071317

13081318
Set<String> parameterKeys = getKeys(obj);
13091319
for(String parameterName : parameterKeys) {
1320+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1321+
parameterName)) {
1322+
result.warning(location, "Parameter name "+ parameterName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1323+
}
1324+
13101325
JsonNode parameterValue = obj.get(parameterName);
13111326
if (parameterValue.getNodeType().equals(JsonNodeType.OBJECT)) {
13121327
ObjectNode parameterObj = (ObjectNode) parameterValue;
@@ -1485,6 +1500,10 @@ public Map<String, Header> getHeaders(ObjectNode obj, String location, ParseResu
14851500

14861501
Set<String> headerKeys = getKeys(obj);
14871502
for(String headerName : headerKeys) {
1503+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1504+
headerName)) {
1505+
result.warning(location, "Header name "+ headerName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1506+
}
14881507
JsonNode headerValue = obj.get(headerName);
14891508
if (!headerValue.getNodeType().equals(JsonNodeType.OBJECT)) {
14901509
result.invalidType(location, headerName, "object", headerValue);
@@ -1627,6 +1646,10 @@ public Map<String, SecurityScheme> getSecuritySchemes(ObjectNode obj, String loc
16271646

16281647
Set<String> securitySchemeKeys = getKeys(obj);
16291648
for(String securitySchemeName : securitySchemeKeys) {
1649+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1650+
securitySchemeName)) {
1651+
result.warning(location, "SecurityScheme name "+ securitySchemeName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1652+
}
16301653
JsonNode securitySchemeValue = obj.get(securitySchemeName);
16311654
if (!securitySchemeValue.getNodeType().equals(JsonNodeType.OBJECT)) {
16321655
result.invalidType(location, securitySchemeName, "object", securitySchemeValue);
@@ -1855,6 +1878,10 @@ public Map<String, Schema> getSchemas(ObjectNode obj, String location, ParseResu
18551878

18561879
Set<String> schemaKeys = getKeys(obj);
18571880
for (String schemaName : schemaKeys) {
1881+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
1882+
schemaName)) {
1883+
result.warning(location, "Schema name "+ schemaName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
1884+
}
18581885
JsonNode schemaValue = obj.get(schemaName);
18591886
if (!schemaValue.getNodeType().equals(JsonNodeType.OBJECT)) {
18601887
result.invalidType(location, schemaName, "object", schemaValue);
@@ -2226,6 +2253,11 @@ public Map<String, Example> getExamples(ObjectNode obj, String location, ParseRe
22262253

22272254
Set<String> exampleKeys = getKeys(obj);
22282255
for(String exampleName : exampleKeys) {
2256+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2257+
exampleName)) {
2258+
result.warning(location, "Example name "+ exampleName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2259+
}
2260+
22292261
JsonNode exampleValue = obj.get(exampleName);
22302262
if (!exampleValue.getNodeType().equals(JsonNodeType.OBJECT)) {
22312263
result.invalidType(location, exampleName, "object", exampleValue);
@@ -2355,6 +2387,11 @@ public ApiResponses getResponses(ObjectNode node, String location, ParseResult r
23552387
Set<String> keys = getKeys(node);
23562388

23572389
for (String key : keys) {
2390+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2391+
key)) {
2392+
result.warning(location, "Response key "+ key + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2393+
}
2394+
23582395
if (key.startsWith("x-")) {
23592396
Map <String,Object> extensions = getExtensions(node);
23602397
if(extensions != null && extensions.size() > 0) {
@@ -2588,6 +2625,10 @@ public Map<String, RequestBody> getRequestBodies(ObjectNode obj, String location
25882625

25892626
Set<String> bodyKeys = getKeys(obj);
25902627
for(String bodyName : bodyKeys) {
2628+
if (!Pattern.matches("^[a-zA-Z0-9\\.\\-_]+$",
2629+
bodyName)) {
2630+
result.warning(location, "RequestBody name "+ bodyName + " doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$");
2631+
}
25912632
JsonNode bodyValue = obj.get(bodyName);
25922633
if (!bodyValue.getNodeType().equals(JsonNodeType.OBJECT)) {
25932634
result.invalidType(location, bodyName, "object", bodyValue);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,17 @@ public void testIssue915() {
15971597
assertEquals(description, "The number of allowed requests in the current period");
15981598
}
15991599

1600+
@Test
1601+
public void testIssue931() {
1602+
ParseOptions options = new ParseOptions();
1603+
options.setResolve(true);
1604+
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("Issue_931.json", null, options);
1605+
assertNotNull(result.getOpenAPI());
1606+
assertTrue(result.getMessages().size() > 0);
1607+
assertEquals(result.getMessages().get(0).contains("doesn't adhere to regular expression ^[a-zA-Z0-9\\.\\-_]+$"), true);
1608+
1609+
}
1610+
16001611
public void shouldParseParameters() {
16011612
ParseOptions parseOptions = new ParseOptions();
16021613
parseOptions.setResolveFully(true);
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"version": "1.0.0",
5+
"title": "Swagger Petstore",
6+
"license": {
7+
"name": "MIT"
8+
}
9+
},
10+
"servers": [
11+
{
12+
"url": "http://petstore.swagger.io/v1"
13+
}
14+
],
15+
"paths": {
16+
"/pets": {
17+
"get": {
18+
"summary": "List all pets",
19+
"operationId": "listPets",
20+
"tags": [
21+
"pets"
22+
],
23+
"parameters": [
24+
{
25+
"name": "limit",
26+
"in": "query",
27+
"description": "How many items to return at one time (max 100)",
28+
"required": false,
29+
"schema": {
30+
"type": "integer",
31+
"format": "int32"
32+
}
33+
}
34+
],
35+
"responses": {
36+
"200": {
37+
"description": "A paged array of pets",
38+
"headers": {
39+
"x-next": {
40+
"description": "A link to the next page of responses",
41+
"schema": {
42+
"$ref":"#/components/schemas/Pets"
43+
}
44+
}
45+
},
46+
"content": {
47+
"application/json": {
48+
"schema": {
49+
"$ref": "#/components/schemas/Pets"
50+
}
51+
}
52+
}
53+
},
54+
"default": {
55+
"description": "unexpected error",
56+
"content": {
57+
"application/json": {
58+
"schema": {
59+
"$ref": "#/components/schemas/Error"
60+
}
61+
}
62+
}
63+
}
64+
}
65+
},
66+
"post": {
67+
"summary": "Create a pet",
68+
"operationId": "createPets",
69+
"tags": [
70+
"pets"
71+
],
72+
"requestBody":{
73+
"content":{
74+
"application/json": {
75+
"schema":{
76+
"type":"string",
77+
"default":1
78+
}
79+
}
80+
}
81+
82+
},
83+
"responses": {
84+
"201": {
85+
"description": "Null response"
86+
}
87+
}
88+
}
89+
},
90+
"/pets/{petId}": {
91+
"get": {
92+
"summary": "Info for a specific pet",
93+
"operationId": "showPetById",
94+
"tags": [
95+
"pets"
96+
],
97+
"parameters": [
98+
{
99+
"name": "petId",
100+
"in": "path",
101+
"required": true,
102+
"description": "The id of the pet to retrieve",
103+
"schema": {
104+
"type": "string"
105+
}
106+
}
107+
],
108+
"responses": {
109+
"200": {
110+
"description": "Expected response to a valid request",
111+
"content": {
112+
"application/json": {
113+
"schema": {
114+
"$ref": "#/components/schemas/Pets"
115+
}
116+
}
117+
}
118+
},
119+
"default": {
120+
"description": "unexpected error",
121+
"content": {
122+
"application/json": {
123+
"schema": {
124+
"$ref": "#/components/schemas/Error"
125+
}
126+
}
127+
}
128+
}
129+
}
130+
}
131+
}
132+
},
133+
"components": {
134+
"schemas": {
135+
"!m": {
136+
"required": [
137+
"id",
138+
"name"
139+
],
140+
"properties": {
141+
"id": {
142+
"type": "integer",
143+
"format": "int64"
144+
},
145+
"name": {
146+
"type": "string"
147+
},
148+
"tag": {
149+
"type": "string"
150+
}
151+
}
152+
},
153+
"Pets": {
154+
"type": "array",
155+
"items": {
156+
"$ref": "#/components/schemas/Pet"
157+
}
158+
},
159+
"Error": {
160+
"required": [
161+
"code",
162+
"message"
163+
],
164+
"properties": {
165+
"code": {
166+
"type": "integer",
167+
"format": "int32"
168+
},
169+
"message": {
170+
"type": "string"
171+
}
172+
}
173+
}
174+
}
175+
}
176+
}

0 commit comments

Comments
 (0)