Skip to content

Commit 6b1a28d

Browse files
miklishstevehu
authored andcommitted
Case insensitive lookup for deserialization type for header parameters (#123)
* (issue #122) Make parameter deserializer do a case insensitive lookup for deserialization type for header parameters * add unit tests to check that headers with arbitrary case are properly deserialized (but not cookie or query params) * add unit tests to check that headers with arbitrary case are properly deserialized (but not cookie or query params)
1 parent 7d4a7da commit 6b1a28d

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

openapi-meta/src/main/java/com/networknt/openapi/parameter/ParameterDeserializer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ static void deserialize(HttpServerExchange exchange, OpenApiOperation openApiOpe
5858
}
5959

6060
default boolean isApplicable(HttpServerExchange exchange, Parameter parameter, Set<String> candidateParams) {
61-
return candidateParams.contains(parameter.getName());
61+
// HTTP header names are case insensitive (RFC 7230, https://tools.ietf.org/html/rfc7230#section-3.2)
62+
if(ParameterType.of(parameter.getIn()) == ParameterType.HEADER)
63+
return candidateParams.stream().anyMatch(s->parameter.getName().equalsIgnoreCase(s));
64+
else
65+
return candidateParams.contains(parameter.getName());
6266
}
6367

6468
default void deserialize(HttpServerExchange exchange, Parameter parameter, Set<String> candidateParams) {

openapi-meta/src/test/java/com/networknt/openapi/parameter/IntegrationTest.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class IntegrationTest {
4242
private static final Logger logger = LoggerFactory.getLogger(OpenApiHandlerTest.class);
4343
private static final String EXPECTED_ARRAY_RESULT="3-4-5";
4444
private static final String EXPECTED_MAP_RESULT="id-name-001-Dog";
45+
private static final String EXPECTED_NEGATIVE_RESULT = "failed";
4546

4647
private static Undertow server = null;
4748

@@ -294,6 +295,12 @@ static RoutingHandler setupRoutings() {
294295
public void test_array_default_query_param_deserialization() throws Exception {
295296
runTest("/pets?limit=3&limit=4&limit=5", EXPECTED_ARRAY_RESULT);
296297
}
298+
299+
@Test
300+
/** negative test: query params are case sensitive */
301+
public void test_mixed_case_array_default_query_param_deserialization() throws Exception {
302+
runTest("/pets?LIMIT=3&LIMIT=4&LIMIT=5", EXPECTED_NEGATIVE_RESULT);
303+
}
297304

298305
@Test
299306
public void test_array_no_explode_query_param_deserialization() throws Exception {
@@ -392,6 +399,14 @@ public void test_array_header_param_deserialization() throws Exception {
392399

393400
runTest("/pets_header_array", EXPECTED_ARRAY_RESULT, headers, Collections.emptyMap());
394401
}
402+
403+
@Test
404+
public void test_array_mixed_case_header_param_deserialization() throws Exception {
405+
Map<String, String> headers = new HashMap<>();
406+
headers.put("PeTiD", "3,4,5");
407+
408+
runTest("/pets_header_array", EXPECTED_ARRAY_RESULT, headers, Collections.emptyMap());
409+
}
395410

396411
@Test
397412
public void test_object_simple_explode_header_param_deserialization() throws Exception {
@@ -400,27 +415,50 @@ public void test_object_simple_explode_header_param_deserialization() throws Exc
400415

401416
runTest("/pets_header_obj_ep", EXPECTED_MAP_RESULT, headers, Collections.emptyMap());
402417
}
418+
419+
@Test
420+
public void test_object_simple_explode_mixed_case_header_param_deserialization() throws Exception {
421+
Map<String, String> headers = new HashMap<>();
422+
headers.put("PeTiD", "id=001,name=Dog");
423+
424+
runTest("/pets_header_obj_ep", EXPECTED_MAP_RESULT, headers, Collections.emptyMap());
425+
}
403426

404427
@Test
405428
public void test_object_simple_no_explode_header_param_deserialization() throws Exception {
406429
Map<String, String> headers = new HashMap<>();
407430
headers.put("petId", "id,001,name,Dog");
408431
runTest("/pets_header_obj_no_ep", EXPECTED_MAP_RESULT, headers, Collections.emptyMap());
409-
}
410-
411-
@Test
432+
}
433+
434+
@Test
435+
public void test_object_simple_no_explode_mixed_case_header_param_deserialization() throws Exception {
436+
Map<String, String> headers = new HashMap<>();
437+
headers.put("PeTiD", "id,001,name,Dog");
438+
runTest("/pets_header_obj_no_ep", EXPECTED_MAP_RESULT, headers, Collections.emptyMap());
439+
}
440+
441+
@Test
412442
public void test_array_cookie_param_deserialization() throws Exception {
413443
Map<String, String> cookies = new HashMap<>();
414444
cookies.put("petId", "3,4,5");
415445
runTest("/pets_cookie_array", EXPECTED_ARRAY_RESULT, Collections.emptyMap(), cookies);
416446
}
447+
448+
@Test
449+
/*** negative test: cookie params are case sensitive */
450+
public void test_array_mixed_case_cookie_param_deserialization() throws Exception {
451+
Map<String, String> cookies = new HashMap<>();
452+
cookies.put("petid", "3,4,5");
453+
runTest("/pets_cookie_array", EXPECTED_NEGATIVE_RESULT, Collections.emptyMap(), cookies);
454+
}
417455

418456
@Test
419457
public void test_object_simple_no_explode_cookie_param_deserialization() throws Exception {
420458
Map<String, String> cookies = new HashMap<>();
421459
cookies.put("petId", "id,001,name,Dog");
422460
runTest("/pets_cookie_obj_no_ep", EXPECTED_MAP_RESULT, Collections.emptyMap(), cookies);
423-
}
461+
}
424462

425463
public void runTest(String requestPath, String expectedValue, Map<String, String> headers, Map<String, String> cookies) throws Exception {
426464
final AtomicReference<ClientResponse> reference = new AtomicReference<>();

0 commit comments

Comments
 (0)