REST API: Fix error in _fields filtering logic where only one of several requested sibling properties would be included.
Props kadamwhite, TimothyBlynJacobs. Fixes #48266. git-svn-id: https://develop.svn.wordpress.org/trunk@46456 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
1bcd41b7c1
commit
70b408c8fe
|
@ -744,7 +744,11 @@ function rest_filter_response_fields( $response, $server, $request ) {
|
|||
$ref = &$fields_as_keyed;
|
||||
while ( count( $parts ) > 1 ) {
|
||||
$next = array_shift( $parts );
|
||||
$ref[ $next ] = array();
|
||||
if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) {
|
||||
// Skip any sub-properties if their parent prop is already marked for inclusion.
|
||||
break 2;
|
||||
}
|
||||
$ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
|
||||
$ref = &$ref[ $next ];
|
||||
}
|
||||
$last = array_shift( $parts );
|
||||
|
|
|
@ -560,6 +560,102 @@ class Tests_REST_API extends WP_UnitTestCase {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that specifying a single top-level key in _fields includes that field and all children.
|
||||
*
|
||||
* @ticket 48266
|
||||
*/
|
||||
public function test_rest_filter_response_fields_top_level_key() {
|
||||
$response = new WP_REST_Response();
|
||||
|
||||
$response->set_data(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request = array(
|
||||
'_fields' => 'meta',
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
),
|
||||
$response->get_data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that a top-level key in _fields supersedes any specified children of that field.
|
||||
*
|
||||
* @ticket 48266
|
||||
*/
|
||||
public function test_rest_filter_response_fields_child_after_parent() {
|
||||
$response = new WP_REST_Response();
|
||||
|
||||
$response->set_data(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request = array(
|
||||
'_fields' => 'meta,meta.key1',
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
),
|
||||
$response->get_data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that specifying two sibling properties in _fields causes both to be included.
|
||||
*
|
||||
* @ticket 48266
|
||||
*/
|
||||
public function test_rest_filter_response_fields_include_all_specified_siblings() {
|
||||
$response = new WP_REST_Response();
|
||||
|
||||
$response->set_data(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request = array(
|
||||
'_fields' => 'meta.key1,meta.key2',
|
||||
);
|
||||
|
||||
$response = rest_filter_response_fields( $response, null, $request );
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'meta' => array(
|
||||
'key1' => 1,
|
||||
'key2' => 2,
|
||||
),
|
||||
),
|
||||
$response->get_data()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 42094
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue