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-users-controller.php

    r59899 r59970  
    255255
    256256        if ( 'HEAD' === $method ) {
    257             $this->assertNull( $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
     257            $this->assertSame( array(), $response->get_data(), 'Expected null response data for HEAD request, but received non-null data.' );
    258258            return null;
    259259        }
     
    32523252            return null;
    32533253        }
    3254         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     3254        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    32553255    }
    32563256
     
    32733273        $this->assertSame( 200, $response->get_status() );
    32743274        if ( $is_head_request ) {
    3275             $this->assertNull( $response->get_data() );
     3275            $this->assertSame( array(), $response->get_data() );
    32763276        } else {
    32773277            $this->assertNotEmpty( $response->get_data() );
     
    33053305        // Assert that the SQL query only fetches the id column.
    33063306        $this->assertMatchesRegularExpression( $pattern, $query->request, 'The SQL query does not match the expected string.' );
     3307    }
     3308
     3309    /**
     3310     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     3311     * @ticket 56481
     3312     *
     3313     * @param string $path The path to test.
     3314     */
     3315    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     3316        $user_id = self::factory()->user->create();
     3317        wp_set_current_user( self::$user );
     3318
     3319        $request = new WP_REST_Request( 'HEAD', sprintf( $path, $user_id ) );
     3320        $request->set_param( '_fields', 'id' );
     3321        $server   = rest_get_server();
     3322        $response = $server->dispatch( $request );
     3323        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     3324        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     3325        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     3326
     3327        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     3328    }
     3329
     3330    /**
     3331     * Data provider intended to provide paths for testing HEAD requests.
     3332     *
     3333     * @return array
     3334     */
     3335    public static function data_head_request_with_specified_fields_returns_success_response() {
     3336        return array(
     3337            'get_item request'  => array( '/wp/v2/users/%d' ),
     3338            'get_items request' => array( '/wp/v2/users' ),
     3339        );
    33073340    }
    33083341
Note: See TracChangeset for help on using the changeset viewer.