Make WordPress Core


Ignore:
Timestamp:
03/11/2025 02:17:41 PM (3 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-post-types-controller.php

    r59899 r59970  
    112112            return null;
    113113        }
    114         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     114        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     115    }
     116
     117    /**
     118     * @dataProvider data_head_request_with_specified_fields_returns_success_response
     119     * @ticket 56481
     120     *
     121     * @param string $path The path to test.
     122     */
     123    public function test_head_request_with_specified_fields_returns_success_response( $path ) {
     124        $request = new WP_REST_Request( 'HEAD', $path );
     125        $request->set_param( '_fields', 'slug' );
     126        $server   = rest_get_server();
     127        $response = $server->dispatch( $request );
     128        add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
     129        $response = apply_filters( 'rest_post_dispatch', $response, $server, $request );
     130        remove_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10 );
     131
     132        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
     133    }
     134
     135    /**
     136     * Data provider intended to provide paths for testing HEAD requests.
     137     *
     138     * @return array
     139     */
     140    public static function data_head_request_with_specified_fields_returns_success_response() {
     141        return array(
     142            'get_item request'  => array( '/wp/v2/types/post' ),
     143            'get_items request' => array( '/wp/v2/types' ),
     144        );
    115145    }
    116146
     
    325355        $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' );
    326356        $this->assertSame( 0, $filter->get_call_count(), 'The "' . $hook_name . '" filter was called when it should not be for HEAD requests.' );
    327         $this->assertNull( $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
     357        $this->assertSame( array(), $response->get_data(), 'The server should not generate a body in response to a HEAD request.' );
    328358    }
    329359
Note: See TracChangeset for help on using the changeset viewer.