Skip to content

Commit ec68123

Browse files
author
Chris Wiechmann
committed
Avoid StringIndexOutOfBoundsException
#3
1 parent 434690c commit ec68123

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/main/java/com/axway/apim/openapi/validator/OpenAPIValidator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ public boolean isValidRequest(String payload, String verb, String path, QueryStr
123123
if(validationReport.getMessages().toString().contains("No API path found that matches request")) {
124124
// Only cache the path, if a direct hit fails
125125
cachePath = true;
126-
// Remove the first path (e.g. /petstore) from the path repeat the process
127-
path = path.substring(path.indexOf("/", 1), path.length());
126+
/*
127+
* If no match was found in the API-Spec for the given path, then we remove the first part of the
128+
* path because the API might be exposed with a different path by the API-Manager than defined in the spec.
129+
* For example: /great-petstore/pet/31233 will not find anything in the first attempt, because the
130+
* API does not exist with /great-petstore in the API-Spec. This process is repeated at most 5 times.
131+
*/
132+
if(path.indexOf("/", 1)!=-1) {
133+
path = path.substring(path.indexOf("/", 1), path.length());
134+
}
128135
} else {
129136
break;
130137
}
@@ -257,6 +264,8 @@ protected boolean removeEldestEntry(Entry<K, V> eldest) {
257264
}
258265

259266
public void setMaxSize(int maxSize) {
267+
// Reset the cache if the maxSize is set
268+
clear();
260269
this.maxSize = maxSize;
261270
}
262271
}

src/test/java/com/axway/apim/openapi/validator/TestOpenAPIValidator.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ public void validRequestWithFullPathAndPathParameter() throws IOException
109109
path = "/api/petstore/v3/store/order/434312";
110110
Assert.assertTrue(validator.isValidRequest(null, verb, path, null, headers), "Request should be valid!");
111111

112-
Assert.assertEquals(validator.getExposurePath2SpecifiedPathMap().size(), 2, "Cached paths should be two as the size is limited");
112+
path = "/api/petstore/v3/store/order/978978";
113+
Assert.assertTrue(validator.isValidRequest(null, verb, path, null, headers), "Request should be valid!");
114+
115+
// Check the cache
116+
Assert.assertEquals(validator.getExposurePath2SpecifiedPathMap().size(), 2, "Cached paths should be two as the size is limited. "
117+
+ "Cached paths: " + validator.getExposurePath2SpecifiedPathMap().toString());
113118
}
114119

115120
@Test
@@ -118,7 +123,7 @@ public void invalidRequestWithFullPathAndPathParameter() throws IOException
118123
String swagger = Files.readFile(this.getClass().getClassLoader().getResourceAsStream(TEST_PACKAGE + "PetstoreSwagger2.0.json"));
119124
OpenAPIValidator validator = OpenAPIValidator.getInstance(swagger);
120125

121-
String path = "/api/petstore/v3/store/order/invalidPatameter";
126+
String path = "/api/petstore/v3/store/order/invalidParameter";
122127
String verb = "DELETE";
123128
HeaderSet headers = new HeaderSet();
124129
headers.addHeader("Content-Type", "application/json");
@@ -142,6 +147,20 @@ public void invalidRequestNoMatchToSpec() throws IOException
142147
Assert.assertFalse(validator.isValidRequest(null, verb, path, null, headers));
143148
}
144149

150+
@Test
151+
public void invalidNoMatch2SpecAtAll() throws IOException
152+
{
153+
String swagger = Files.readFile(this.getClass().getClassLoader().getResourceAsStream(TEST_PACKAGE + "PetstoreSwagger2.0.json"));
154+
OpenAPIValidator validator = OpenAPIValidator.getInstance(swagger);
155+
156+
String path = "/no/match";
157+
String verb = "GET";
158+
HeaderSet headers = new HeaderSet();
159+
headers.addHeader("Content-Type", "application/json");
160+
161+
Assert.assertFalse(validator.isValidRequest(null, verb, path, null, headers));
162+
}
163+
145164
@Test
146165
public void validRequestExternalURLSwagger20() throws IOException
147166
{
@@ -213,5 +232,4 @@ public void validResponsewithoutBody() throws IOException
213232

214233
Assert.assertFalse(validator.isValidResponse(null, verb, path, status, headers), "Request should be not valid!");
215234
}
216-
217235
}

0 commit comments

Comments
 (0)