Skip to content

Fix false boolean query string encoding #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/Gitlab/HttpClient/Message/QueryStringBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class QueryStringBuilder
public static function build($query)
{
if (!is_array($query)) {
return rawurlencode($query);
return static::rawurlencode($query);
}
return implode('&', array_map(function ($value, $key) {
return static::encode($value, $key);
Expand All @@ -32,7 +32,7 @@ public static function build($query)
private static function encode($query, $prefix)
{
if (!is_array($query)) {
return rawurlencode($prefix).'='.rawurlencode($query);
return static::rawurlencode($prefix).'='.static::rawurlencode($query);
}

$isIndexedArray = static::isIndexedArray($query);
Expand All @@ -57,4 +57,20 @@ public static function isIndexedArray(array $query)

return array_keys($query) === range(0, count($query) - 1);
}

/**
* Encode a value like rawurlencode, but return "0" when false is given.
*
* @param mixed $value
*
* @return string
*/
private static function rawurlencode($value)
{
if ($value === false) {
return '0';
}

return rawurlencode($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public function queryStringProvider()
'iids%5B0%5D=88&iids%5B2%5D=86'
];

//Boolean encoding
yield [
['push_events' => false, 'merge_requests_events' => 1],
'push_events=0&merge_requests_events=1'
];

//A deeply nested array.
yield [
[
Expand Down