Make WordPress Core

Ticket #39150: 39150.diff

File 39150.diff, 1.7 KB (added by JPry, 9 years ago)

Fix + tests

  • src/wp-includes/rest-api/class-wp-rest-request.php

    diff --git src/wp-includes/rest-api/class-wp-rest-request.php src/wp-includes/rest-api/class-wp-rest-request.php
    index 3c465babd..0022eae7e 100644
    class WP_REST_Request implements ArrayAccess { 
    669669                        return true;
    670670                }
    671671
    672                 $params = json_decode( $this->get_body(), true );
     672                // Allow an empty body. See https://core.trac.wordpress.org/ticket/39150.
     673                $body = $this->get_body();
     674                if ( empty( $body ) ) {
     675                        return true;
     676                }
     677
     678                $params = json_decode( $body, true );
    673679
    674680                /*
    675681                 * Check for a parsing error.
  • tests/phpunit/tests/rest-api/rest-request.php

    diff --git tests/phpunit/tests/rest-api/rest-request.php tests/phpunit/tests/rest-api/rest-request.php
    index de44844bf..5d9f2e1fc 100644
     
    1010 * @group restapi
    1111 */
    1212class Tests_REST_Request extends WP_UnitTestCase {
     13
     14        /** @var  WP_REST_Request */
     15        public $request;
     16
    1317        public function setUp() {
    1418                parent::setUp();
    1519
    class Tests_REST_Request extends WP_UnitTestCase { 
    414418                $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
    415419        }
    416420
     421
     422        public function test_has_valid_params_empty_json_no_error() {
     423                if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
     424                        return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' );
     425                }
     426
     427                $this->request->set_header( 'Content-Type', 'application/json' );
     428                $this->request->set_body( '' );
     429
     430                $valid = $this->request->has_valid_params();
     431                $this->assertNotWPError( $valid );
     432        }
     433
    417434        public function test_has_multiple_invalid_params_validate_callback() {
    418435                $this->request->set_url_params( array(
    419436                        'someinteger' => '123',