REST API: Filter responses based on the _fields parameter, before data is processed.

Historically, the REST API would generate the entire response object, including running expensive filters, then it would apply the `_fields` parameter, discarding the fields that weren't specificed.

This change causes `_fields` to be applied earlier, so that only requested fields are processed.

Props danielbachhuber.
See #43874.



git-svn-id: https://develop.svn.wordpress.org/trunk@43087 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-05-02 01:24:30 +00:00
parent 1a4e28818f
commit 4ac3f4c13a
22 changed files with 526 additions and 167 deletions

View File

@@ -1178,6 +1178,27 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
$this->check_post_data( $attachment, $data, 'embed', $response->get_links() );
}
public function test_prepare_item_limit_fields() {
$attachment_id = $this->factory->attachment->create_object(
$this->test_file, 0, array(
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
'post_author' => self::$editor_id,
)
);
wp_set_current_user( self::$editor_id );
$endpoint = new WP_REST_Attachments_Controller( 'post' );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/media/%d', $attachment_id ) );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,slug' );
$obj = get_post( $attachment_id );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
'id',
'slug',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -882,6 +882,18 @@ class WP_Test_REST_Categories_Controller extends WP_Test_REST_Controller_Testcas
$this->check_taxonomy_term( $term, $data, $response->get_links() );
}
public function test_prepare_item_limit_fields() {
$request = new WP_REST_Request;
$endpoint = new WP_REST_Terms_Controller( 'category' );
$request->set_param( '_fields', 'id,name' );
$term = get_term( 1, 'category' );
$response = $endpoint->prepare_item_for_response( $term, $request );
$this->assertEquals( array(
'id',
'name',
), array_keys( $response->get_data() ) );
}
public function test_prepare_taxonomy_term_child() {
$child = $this->factory->category->create(
array(

View File

@@ -847,6 +847,20 @@ class WP_Test_REST_Comments_Controller extends WP_Test_REST_Controller_Testcase
$this->check_comment_data( $data, 'edit', $response->get_links() );
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$admin_id );
$endpoint = new WP_REST_Comments_Controller;
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,status' );
$obj = get_comment( self::$approved_id );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
'id',
'status',
), array_keys( $response->get_data() ) );
}
public function test_get_comment_author_avatar_urls() {
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', self::$approved_id ) );

View File

@@ -200,4 +200,27 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
$this->assertEquals( 'a', $args['somedefault']['default'] );
}
public function test_get_fields_for_response() {
$controller = new WP_REST_Test_Controller();
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );
$fields = $controller->get_fields_for_response( $request );
$this->assertEquals( array(
'somestring',
'someinteger',
'someboolean',
'someurl',
'somedate',
'someemail',
'someenum',
'someargoptions',
'somedefault',
), $fields );
$request->set_param( '_fields', 'somestring,someinteger' );
$fields = $controller->get_fields_for_response( $request );
$this->assertEquals( array(
'somestring',
'someinteger',
), $fields );
}
}

View File

@@ -451,6 +451,26 @@ class WP_Test_REST_Pages_Controller extends WP_Test_REST_Post_Type_Controller_Te
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$editor_id );
$page_id = $this->factory->post->create(
array(
'post_status' => 'publish',
'post_type' => 'page',
)
);
$endpoint = new WP_REST_Posts_Controller( 'page' );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/pages/%d', $page_id ) );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,slug' );
$obj = get_post( $page_id );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
'id',
'slug',
), array_keys( $response->get_data() ) );
}
public function test_get_pages_params() {
$this->factory->post->create_many(
8, array(

View File

@@ -131,6 +131,19 @@ class WP_Test_REST_Post_Statuses_Controller extends WP_Test_REST_Controller_Test
$this->check_post_status_obj( $obj, $data->get_data(), $data->get_links() );
}
public function test_prepare_item_limit_fields() {
$obj = get_post_status_object( 'publish' );
$request = new WP_REST_Request;
$endpoint = new WP_REST_Post_Statuses_Controller;
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,name' );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
// 'id' doesn't exist in this context.
'name',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/statuses' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -123,6 +123,19 @@ class WP_Test_REST_Post_Types_Controller extends WP_Test_REST_Controller_Testcas
$this->check_post_type_obj( 'edit', $obj, $response->get_data(), $response->get_links() );
}
public function test_prepare_item_limit_fields() {
$obj = get_post_type_object( 'post' );
$request = new WP_REST_Request;
$endpoint = new WP_REST_Post_Types_Controller;
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,name' );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
// 'id' doesn't exist in this context.
'name',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -1499,6 +1499,20 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
$this->check_get_post_response( $response, 'edit' );
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$editor_id );
$endpoint = new WP_REST_Posts_Controller( 'post' );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,slug' );
$obj = get_post( self::$post_id );
$response = $endpoint->prepare_item_for_response( $obj, $request );
$this->assertEquals( array(
'id',
'slug',
), array_keys( $response->get_data() ) );
}
public function test_create_item() {
wp_set_current_user( self::$editor_id );

View File

@@ -237,6 +237,20 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase
$this->check_get_revision_response( $response, $this->revision_1 );
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$endpoint = new WP_REST_Revisions_Controller( 'post' );
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,slug' );
$revision = get_post( $this->revision_id1 );
$response = $endpoint->prepare_item_for_response( $revision, $request );
$this->assertEquals( array(
'id',
'slug',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id . '/revisions' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -968,6 +968,18 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$this->check_taxonomy_term( $term, $data, $response->get_links() );
}
public function test_prepare_item_limit_fields() {
$request = new WP_REST_Request;
$endpoint = new WP_REST_Terms_Controller( 'post_tag' );
$request->set_param( '_fields', 'id,name' );
$term = get_term_by( 'id', $this->factory->tag->create(), 'post_tag' );
$response = $endpoint->prepare_item_for_response( $term, $request );
$this->assertEquals( array(
'id',
'name',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -162,6 +162,19 @@ class WP_Test_REST_Taxonomies_Controller extends WP_Test_REST_Controller_Testcas
$this->check_taxonomy_object( 'edit', $tax, $response->get_data(), $response->get_links() );
}
public function test_prepare_item_limit_fields() {
$tax = get_taxonomy( 'category' );
$request = new WP_REST_Request;
$endpoint = new WP_REST_Taxonomies_Controller;
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,name' );
$response = $endpoint->prepare_item_for_response( $tax, $request );
$this->assertEquals( array(
// 'id' doesn't exist in this context.
'name',
), array_keys( $response->get_data() ) );
}
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/taxonomies' );
$response = rest_get_server()->dispatch( $request );

View File

@@ -849,6 +849,19 @@ class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
$this->check_get_user_response( $data, 'edit' );
}
public function test_prepare_item_limit_fields() {
wp_set_current_user( self::$user );
$request = new WP_REST_Request;
$request->set_param( 'context', 'edit' );
$request->set_param( '_fields', 'id,name' );
$user = get_user_by( 'id', get_current_user_id() );
$response = $this->endpoint->prepare_item_for_response( $user, $request );
$this->assertEquals( array(
'id',
'name',
), array_keys( $response->get_data() ) );
}
public function test_get_user_avatar_urls() {
wp_set_current_user( self::$user );