Skip to content

Commit ba13b58

Browse files
author
Chris Wiechmann
committed
Now query parameters are decoded by default
1 parent bf98c4f commit ba13b58

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.6.0] 2022-06-24
8+
### Added
9+
- Now query parameters are by default decoded before send to the validator (e.g. otr%C3%B3s -> otrós)
10+
711
## [1.5.0] 2022-06-23
812
### Added
913
- If the Content-Type header is duplicated, only the first header is taken and all others are deleted.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def invoke(msg)
3939
// https://github.com/Axway-API-Management-Plus/openapi-validator/issues/2
4040
// validator.getExposurePath2SpecifiedPathMap().setMaxSize(5000);
4141
42+
// By default query parameters are decoded by default. You may turn this off for each
43+
// individual validator instance
44+
// validator.setDecodeQueryParams(false);
45+
4246
// Get required parameters for the validation
4347
def payload = bodyAsString(msg.get('content.body'));
4448
def path = msg.get("http.request.path");

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.axway.apim.openapi.validator;
22

33
import java.net.URI;
4+
import java.net.URLDecoder;
5+
import java.nio.charset.StandardCharsets;
46
import java.util.ArrayList;
57
import java.util.Collection;
68
import java.util.Collections;
@@ -36,6 +38,8 @@ public class OpenAPIValidator
3638

3739
private int payloadLogMaxLength = 40;
3840

41+
private boolean decodeQueryParams = true;
42+
3943
public static synchronized OpenAPIValidator getInstance(String openAPISpec) {
4044
int hashCode = openAPISpec.hashCode();
4145
if(instances.containsKey(hashCode)) {
@@ -219,6 +223,9 @@ public Collection<String> getQueryParameters() {
219223
public Collection<String> getQueryParameterValues(String name) {
220224
if(queryParams==null) return Collections.emptyList();
221225
ArrayList<String> values = queryParams.getHeaderValues(name);
226+
if(decodeQueryParams) {
227+
values.replaceAll(headerValue -> URLDecoder.decode(headerValue, StandardCharsets.UTF_8));
228+
}
222229
return (Collection<String>) ((values == null) ? Collections.emptyList() : values);
223230
}
224231

@@ -297,4 +304,8 @@ public void setMaxSize(int maxSize) {
297304
public MaxSizeHashMap<String, Object> getExposurePath2SpecifiedPathMap() {
298305
return exposurePath2SpecifiedPathMap;
299306
}
307+
308+
public void setDecodeQueryParams(boolean decodeQueryParams) {
309+
this.decodeQueryParams = decodeQueryParams;
310+
}
300311
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,31 @@ public void validResponsewithoutBody() throws IOException
232232

233233
Assert.assertFalse(validator.isValidResponse(null, verb, path, status, headers), "Request should be not valid!");
234234
}
235+
236+
@Test
237+
public void testSpecialCharsQueryParam() throws IOException
238+
{
239+
String swagger = Files.readFile(this.getClass().getClassLoader().getResourceAsStream(TEST_PACKAGE + "PetstoreSwagger2.0.json"));
240+
OpenAPIValidator validator = OpenAPIValidator.getInstance(swagger);
241+
242+
String path = "/user/login";
243+
String verb = "GET";
244+
HeaderSet headers = new HeaderSet();
245+
headers.addHeader("Content-Type", "application/json");
246+
QueryStringHeaderSet queryParams = new QueryStringHeaderSet();
247+
queryParams.addHeader("username", "otr%C3%B3s");
248+
queryParams.addHeader("password", "otrós");
249+
250+
Assert.assertTrue(validator.isValidRequest(null, verb, path, queryParams, headers));
251+
252+
validator.setDecodeQueryParams(false);
253+
254+
QueryStringHeaderSet queryParams2 = new QueryStringHeaderSet();
255+
queryParams2.addHeader("username", "otr%C3%B3s");
256+
queryParams2.addHeader("password", "otrós");
257+
258+
Assert.assertFalse(validator.isValidRequest(null, verb, path, queryParams2, headers));
259+
}
260+
261+
235262
}

src/test/resources/com/axway/apim/openapi/validator/PetstoreSwagger2.0.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,14 +738,18 @@
738738
"in": "query",
739739
"description": "The user name for login",
740740
"required": true,
741-
"type": "string"
741+
"type": "string",
742+
"minLength": 2,
743+
"maxLength": 5
742744
},
743745
{
744746
"name": "password",
745747
"in": "query",
746748
"description": "The password for login in clear text",
747749
"required": true,
748-
"type": "string"
750+
"type": "string",
751+
"minLength": 2,
752+
"maxLength": 5
749753
}
750754
],
751755
"responses": {

0 commit comments

Comments
 (0)