Make WordPress Core


Ignore:
Timestamp:
03/11/2025 02:17:41 PM (12 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Fix fatal error when making HEAD requests with _fields filter.

In [59889] the REST API controllers were adjusted to perform less work when responding to HEAD requests. The WP_REST_Response body would now be null, which caused issues with filters that expected the response body to be an array.

This commit sets the response body to be an empty array when preparing the response instead. The body will still be discarded, but this provides better backward comppatibility with code that assumes an array will be used.

See #56481.
Props antonvlasenko, timothyblynjacobs, mamaduka, wildworks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/wpRestTemplateRevisionsController.php

    r59899 r59970  
    438438        $response = rest_get_server()->dispatch( $request );
    439439        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    440         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     440        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    441441    }
    442442
     
    628628        $response    = rest_get_server()->dispatch( $request );
    629629        $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
    630         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     630        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    631631    }
    632632
     
    641641            'template parts' => array( 'template_part_post', 'template-parts', self::TEST_THEME . '//' . self::TEMPLATE_PART_NAME ),
    642642        );
     643    }
     644
     645    /**
     646     * @dataProvider data_get_item_with_data_provider
     647     * @covers       WP_REST_Template_Revisions_Controller::get_item
     648     * @ticket 56922
     649     *
     650     * @param string $parent_post_property_name A class property name that contains the parent post object.
     651     * @param string $rest_base Base part of the REST API endpoint to test.
     652     * @param string $template_id Template ID to use in the test.
     653     */
     654    public function test_get_item_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     655        wp_set_current_user( self::$admin_id );
     656
     657        $parent_post = self::$$parent_post_property_name;
     658
     659        $revisions   = wp_get_post_revisions( $parent_post, array( 'fields' => 'ids' ) );
     660        $revision_id = array_shift( $revisions );
     661
     662        $request = new WP_REST_Request(
     663            'HEAD',
     664            '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions/' . $revision_id
     665        );
     666        $request->set_param( '_fields', 'id' );
     667        $server   = rest_get_server();
     668        $response = $server->dispatch( $request );
     669        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     670        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     671        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     672
     673        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     674    }
     675
     676    /**
     677     * @dataProvider data_get_items_with_data_provider
     678     * @covers       WP_REST_Template_Revisions_Controller::get_items
     679     * @ticket 56922
     680     *
     681     * @param string $parent_post_property_name A class property name that contains the parent post object.
     682     * @param string $rest_base Base part of the REST API endpoint to test.
     683     * @param string $template_id Template ID to use in the test.
     684     */
     685    public function test_get_items_head_request_with_specified_fields_returns_success_response( $parent_post_property_name, $rest_base, $template_id ) {
     686        wp_set_current_user( self::$admin_id );
     687        $parent_post = self::$$parent_post_property_name;
     688
     689        $request = new WP_REST_Request(
     690            'HEAD',
     691            '/wp/v2/' . $rest_base . '/' . $template_id . '/revisions'
     692        );
     693
     694        $request->set_param( '_fields', 'id' );
     695        $server   = rest_get_server();
     696        $response = $server->dispatch( $request );
     697        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     698        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     699        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     700
     701        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    643702    }
    644703
Note: See TracChangeset for help on using the changeset viewer.