REST API: Support dot.nested hierarchical properties in _fields query parameter.

Enable clients to opt-in to receipt of one or more specific sub-properties within a response, and not other sub-properties.
Skip potentially expensive filtering and processing for post resources which were explicitly not requested.

Props kadamwhite, TimothyBlynJacobs, dlh.
Fixes #42094.


git-svn-id: https://develop.svn.wordpress.org/trunk@46184 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White
2019-09-19 14:04:51 +00:00
parent 5f1cbd7165
commit b926baa51d
6 changed files with 271 additions and 43 deletions
+65
View File
@@ -519,6 +519,71 @@ class Tests_REST_API extends WP_UnitTestCase {
);
}
/**
* Ensure that nested fields may be whitelisted with request['_fields'].
*
* @ticket 42094
*/
public function test_rest_filter_response_fields_nested_field_filter() {
$response = new WP_REST_Response();
$response->set_data(
array(
'a' => 0,
'b' => array(
'1' => 1,
'2' => 2,
),
'c' => 3,
'd' => array(
'4' => 4,
'5' => 5,
),
)
);
$request = array(
'_fields' => 'b.1,c,d.5',
);
$response = rest_filter_response_fields( $response, null, $request );
$this->assertEquals(
array(
'b' => array(
'1' => 1,
),
'c' => 3,
'd' => array(
'5' => 5,
),
),
$response->get_data()
);
}
/**
* @ticket 42094
*/
public function test_rest_is_field_included() {
$fields = array(
'id',
'title',
'content.raw',
'custom.property',
);
$this->assertTrue( rest_is_field_included( 'id', $fields ) );
$this->assertTrue( rest_is_field_included( 'title', $fields ) );
$this->assertTrue( rest_is_field_included( 'title.raw', $fields ) );
$this->assertTrue( rest_is_field_included( 'title.rendered', $fields ) );
$this->assertTrue( rest_is_field_included( 'content', $fields ) );
$this->assertTrue( rest_is_field_included( 'content.raw', $fields ) );
$this->assertTrue( rest_is_field_included( 'custom.property', $fields ) );
$this->assertFalse( rest_is_field_included( 'content.rendered', $fields ) );
$this->assertFalse( rest_is_field_included( 'type', $fields ) );
$this->assertFalse( rest_is_field_included( 'meta', $fields ) );
$this->assertFalse( rest_is_field_included( 'meta.value', $fields ) );
}
/**
* The get_rest_url function should return a URL consistently terminated with a "/",
* whether the blog is configured with pretty permalink support or not.