mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-05 21:24:35 +00:00
REST API: Don't add fields to object when not included in ?_fields=.
In [43087], we improved REST API performance by only rendering the fields specified in the request. Similarly, any fields registered with `register_rest_field()` should only be rendered when included in `?_fields=`. Props dlh, danielbachhuber. Merges [43736] to trunk. Fixes #45099. git-svn-id: https://develop.svn.wordpress.org/trunk@43986 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -231,4 +231,45 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
|
||||
$fields
|
||||
);
|
||||
}
|
||||
|
||||
public function test_add_additional_fields_to_object_respects_fields_param() {
|
||||
$controller = new WP_REST_Test_Controller();
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
|
||||
$schema = $controller->get_item_schema();
|
||||
$field = 'somefield';
|
||||
|
||||
$listener = new MockAction();
|
||||
$method = 'action';
|
||||
|
||||
register_rest_field(
|
||||
$schema['title'],
|
||||
$field,
|
||||
array(
|
||||
'get_callback' => array( $listener, $method ),
|
||||
'schema' => array(
|
||||
'type' => 'string',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$item = array();
|
||||
|
||||
$controller->prepare_item_for_response( $item, $request );
|
||||
|
||||
$first_call_count = $listener->get_call_count( $method );
|
||||
|
||||
$this->assertTrue( $first_call_count > 0 );
|
||||
|
||||
$request->set_param( '_fields', 'somestring' );
|
||||
|
||||
$controller->prepare_item_for_response( $item, $request );
|
||||
|
||||
$this->assertSame( $first_call_count, $listener->get_call_count( $method ) );
|
||||
|
||||
$request->set_param( '_fields', $field );
|
||||
|
||||
$controller->prepare_item_for_response( $item, $request );
|
||||
|
||||
$this->assertTrue( $listener->get_call_count( $method ) > $first_call_count );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user