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 3c465babd0..898f8658ed 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -669,7 +669,12 @@ class WP_REST_Request implements ArrayAccess { return true; } - $params = json_decode( $this->get_body(), true ); + $body = $this->get_body(); + if ( empty( $body ) ) { + return true; + } + + $params = json_decode( $body, true ); /* * Check for a parsing error. diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index de44844bff..777a4d0bd1 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -10,6 +10,8 @@ * @group restapi */ class Tests_REST_Request extends WP_UnitTestCase { + public $request; + public function setUp() { parent::setUp(); @@ -414,6 +416,19 @@ class Tests_REST_Request extends WP_UnitTestCase { $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] ); } + + public function test_has_valid_params_empty_json_no_error() { + if ( version_compare( PHP_VERSION, '5.3', '<' ) ) { + return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' ); + } + + $this->request->set_header( 'Content-Type', 'application/json' ); + $this->request->set_body( '' ); + + $valid = $this->request->has_valid_params(); + $this->assertNotWPError( $valid ); + } + public function test_has_multiple_invalid_params_validate_callback() { $this->request->set_url_params( array( 'someinteger' => '123',