Skip to content

Commit

Permalink
HttpRequestMetaData#hasQueryParameter(String) implementaiton fix
Browse files Browse the repository at this point in the history
Motivation:
HttpRequestMetaData#hasQueryParameter(String) returns false when there is
a query parameter with no value. This is incorrect and it should return true.
  • Loading branch information
Scottmitch committed Sep 19, 2024
1 parent 57f8454 commit 48fdf80
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ public Set<String> queryParametersKeys() {
return lazyParseQueryString().keys();
}

@Override
public boolean hasQueryParameter(final String key) {
return lazyParseQueryString().contains(key);
}

@Override
public boolean hasQueryParameter(final String key, final String value) {
return lazyParseQueryString().contains(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public HttpQuery set(final String key, final String... values) {
return this;
}

boolean contains(final String key) {
return params.get(key) != null;
}

public boolean contains(final String key, final String value) {
final Iterator<String> values = valuesIterator(key);
while (values.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ public interface HttpRequestMetaData extends HttpMetaData {
* @return {@code true} if {@code key} exists.
*/
default boolean hasQueryParameter(final String key) {
// FIXME: 0.43 - remove default, force implementations to implement.
// null value support was added and this method is now incorrect as default
return queryParameter(key) != null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,9 @@ void testOneEmptyQueryParam() {
assertEquals("/foo?bar", fixture.requestTarget());
assertEquals("bar", fixture.rawQuery());
assertNull(fixture.queryParameter("bar"));
assertTrue(fixture.hasQueryParameter("bar"));
assertNull(fixture.queryParameter("nothing"));
assertFalse(fixture.hasQueryParameter("nothing"));

assertEquals(singletonList("bar"), iteratorAsList(fixture.queryParametersKeys().iterator()));
Iterator<Entry<String, String>> itr = fixture.queryParameters().iterator();
Expand All @@ -682,8 +684,11 @@ private void testTwoEmptyQueryParams(String v1, String v2) {
assertEquals(requestTarget, fixture.requestTarget());
assertEquals(rawQuery, fixture.rawQuery());
assertEquals(v1, fixture.queryParameter("bar"));
assertTrue(fixture.hasQueryParameter("bar"));
assertEquals(v2, fixture.queryParameter("baz"));
assertTrue(fixture.hasQueryParameter("baz"));
assertNull(fixture.queryParameter("nothing"));
assertFalse(fixture.hasQueryParameter("nothing"));

assertEquals(asList("bar", "baz"), iteratorAsList(fixture.queryParametersKeys().iterator()));
Iterator<Entry<String, String>> itr = fixture.queryParameters().iterator();
Expand Down Expand Up @@ -716,9 +721,13 @@ private void testThreeEmptyQueryParams(String v1, String v2, String v3) {
assertEquals(requestTarget, fixture.requestTarget());
assertEquals(rawQuery, fixture.rawQuery());
assertEquals(v1, fixture.queryParameter("bar"));
assertTrue(fixture.hasQueryParameter("bar"));
assertEquals(v2, fixture.queryParameter("baz"));
assertTrue(fixture.hasQueryParameter("baz"));
assertEquals(v3, fixture.queryParameter("zap"));
assertTrue(fixture.hasQueryParameter("zap"));
assertNull(fixture.queryParameter("nothing"));
assertFalse(fixture.hasQueryParameter("nothing"));

assertEquals(asList("bar", "baz", "zap"), iteratorAsList(fixture.queryParametersKeys().iterator()));
Iterator<Entry<String, String>> itr = fixture.queryParameters().iterator();
Expand Down

0 comments on commit 48fdf80

Please sign in to comment.