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/rest-sidebars-controller.php

    r59899 r59970  
    172172        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    173173        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    174         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     174        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    175175    }
    176176
     
    568568            return null;
    569569        }
    570         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     570        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     571    }
     572
     573    /**
     574     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     575     * @ticket 56481
     576     *
     577     * @param string $path The path to test.
     578     */
     579    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     580        $this->setup_sidebar(
     581            'sidebar-1',
     582            array(
     583                'name' => 'Test sidebar',
     584            )
     585        );
     586
     587        $request = new WP_REST_Request( 'HEAD', $path );
     588        // This endpoint doesn't seem to support _fields param, but we need to set it to reproduce the fatal error.
     589        $request->set_param( '_fields', 'name' );
     590        $server   = rest_get_server();
     591        $response = $server->dispatch( $request );
     592        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     593        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     594        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     595
     596        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     597    }
     598
     599    /**
     600     * Data provider intended to provide paths for testing HEAD requests.
     601     *
     602     * @return array
     603     */
     604    public static function data_head_request_with_specified_fields_returns_success_response() {
     605        return array(
     606
     607            'get_item request'  => array( '/wp/v2/sidebars/sidebar-1' ),
     608            'get_items request' => array( '/wp/v2/sidebars' ),
     609        );
    571610    }
    572611
Note: See TracChangeset for help on using the changeset viewer.