From 5d924daeb3c9d2c99c0c4a0c8b6bf344930963ef Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Wed, 2 Nov 2016 05:52:12 +0000 Subject: [PATCH] REST API: Change method of merging parameters. `array_merge()` incorrectly reindexes numeric parameters, causing things like `{"123": true}` to be "dropped". Props sswells, joehoyle. Fixes #38306. git-svn-id: https://develop.svn.wordpress.org/trunk@39087 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api/class-wp-rest-request.php | 6 +++++- tests/phpunit/tests/rest-api/rest-request.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index 9c5410a0ec..23156a31b0 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -451,7 +451,11 @@ class WP_REST_Request implements ArrayAccess { $params = array(); foreach ( $order as $type ) { - $params = array_merge( $params, (array) $this->params[ $type ] ); + // array_merge / the "+" operator will mess up + // numeric keys, so instead do a manual foreach. + foreach ( (array) $this->params[ $type ] as $key => $value ) { + $params[ $key ] = $value; + } } return $params; diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index 925853c8cd..47d4c32c8f 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -286,6 +286,18 @@ class Tests_REST_Request extends WP_UnitTestCase { $this->assertEquals( $expected, $this->request->get_params() ); } + public function test_parameter_merging_with_numeric_keys() { + $this->request->set_query_params( array( + '1' => 'hello', + '2' => 'goodbye', + ) ); + $expected = array( + '1' => 'hello', + '2' => 'goodbye', + ); + $this->assertEquals( $expected, $this->request->get_params() ); + } + public function test_sanitize_params() { $this->request->set_url_params( array( 'someinteger' => '123',