diff --git a/core/lib/Drupal/Component/Utility/UrlHelper.php b/core/lib/Drupal/Component/Utility/UrlHelper.php
index 3b32dc9625e69330049cd9bf79b0ef74d8622cf6..9a5e76ba735b6af48318c1297dde4b5d5b7e5b2d 100644
--- a/core/lib/Drupal/Component/Utility/UrlHelper.php
+++ b/core/lib/Drupal/Component/Utility/UrlHelper.php
@@ -45,13 +45,14 @@ public static function buildQuery(array $query, $parent = '') {
     foreach ($query as $key => $value) {
       $key = ($parent ? $parent . rawurlencode('[' . $key . ']') : rawurlencode($key));
 
+      if ($value === NULL) {
+        continue;
+      }
       // Recurse into children.
       if (is_array($value)) {
-        $params[] = static::buildQuery($value, $key);
-      }
-      // If a query parameter value is NULL, only append its key.
-      elseif (!isset($value)) {
-        $params[] = $key;
+        if ($children_query = static::buildQuery($value, $key)) {
+          $params[] = $children_query;
+        }
       }
       else {
         // For better readability of paths in query strings, we decode slashes.
diff --git a/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php b/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
index 158e626c821301afe77e48226b5681bd33a93f7d..9f96cf12f307b6e91e8f9c6cee675901e2273d91 100644
--- a/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/UrlHelperTest.php
@@ -29,7 +29,8 @@ public static function providerTestBuildQuery() {
       [[' &#//+%20@۞' => 'a'], '%20%26%23%2F%2F%2B%2520%40%DB%9E=a', 'Key was properly encoded.'],
       [['a' => '1', 'b' => '2', 'c' => '3'], 'a=1&b=2&c=3', 'Multiple values were properly concatenated.'],
       [['a' => ['b' => '2', 'c' => '3'], 'd' => 'foo'], 'a%5Bb%5D=2&a%5Bc%5D=3&d=foo', 'Nested array was properly encoded.'],
-      [['foo' => NULL], 'foo', 'Simple parameters are properly added.'],
+      [['foo' => NULL], '', 'Simple parameters are properly added.'],
+      [['a' => '1', 'b' => [], 'c' => []], 'a=1', 'Empty key ignored.'],
     ];
   }