REST API: Support more JSON Schemas when filtering a response by context.

The array type, multi-types, and the additional properties keyword are now supported. Additionally, the filter recurses to an infinite depth.

Fixes #48819.


git-svn-id: https://develop.svn.wordpress.org/trunk@47758 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs
2020-05-04 02:44:44 +00:00
parent 9a903f7e73
commit 8153d15f09
3 changed files with 382 additions and 27 deletions
+320
View File
@@ -975,4 +975,324 @@ class Tests_REST_API extends WP_UnitTestCase {
array( array( 'https://api.w.org/term', 'https://api.w.org/attachment' ), array( 'https://api.w.org/term', 'https://api.w.org/attachment' ) ),
);
}
/**
* @ticket 48819
*
* @dataProvider _dp_rest_filter_response_by_context
*/
public function test_rest_filter_response_by_context( $schema, $data, $expected ) {
$this->assertEquals( $expected, rest_filter_response_by_context( $data, $schema, 'view' ) );
}
public function _dp_rest_filter_response_by_context() {
return array(
'default' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'first' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'second' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
array(
'first' => 'a',
'second' => 'b',
),
array( 'first' => 'a' ),
),
'keeps missing context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'first' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'second' => array(
'type' => 'string',
),
),
),
array(
'first' => 'a',
'second' => 'b',
),
array(
'first' => 'a',
'second' => 'b',
),
),
'removes empty context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'first' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'second' => array(
'type' => 'string',
'context' => array(),
),
),
),
array(
'first' => 'a',
'second' => 'b',
),
array( 'first' => 'a' ),
),
'nested properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'parent' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'child' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'hidden' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
array(
'parent' => array(
'child' => 'hi',
'hidden' => 'there',
),
),
array( 'parent' => array( 'child' => 'hi' ) ),
),
'grand child properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'parent' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'child' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'grand' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'hidden' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
),
),
array(
'parent' => array(
'child' => array(
'grand' => 'hi',
),
),
),
array( 'parent' => array( 'child' => array( 'grand' => 'hi' ) ) ),
),
'array' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'arr' => array(
'type' => 'array',
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'visible' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'hidden' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
),
array(
'arr' => array(
array(
'visible' => 'hi',
'hidden' => 'there',
),
),
),
array( 'arr' => array( array( 'visible' => 'hi' ) ) ),
),
'additional properties' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'additional' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
'additionalProperties' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
array(
'additional' => array(
'a' => '1',
'b' => '2',
'c' => '3',
),
),
array( 'additional' => array( 'a' => '1' ) ),
),
'multiple types object' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'multi' => array(
'type' => array( 'object', 'string' ),
'context' => array( 'view', 'edit' ),
'properties' => array(
'a' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'b' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
array(
'multi' => array(
'a' => '1',
'b' => '2',
),
),
array( 'multi' => array( 'a' => '1' ) ),
),
'multiple types array' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'multi' => array(
'type' => array( 'array', 'string' ),
'context' => array( 'view', 'edit' ),
'items' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'visible' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'hidden' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
),
array(
'multi' => array(
array(
'visible' => '1',
'hidden' => '2',
),
),
),
array( 'multi' => array( array( 'visible' => '1' ) ) ),
),
'grand child properties does not traverses missing context' => array(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'type' => 'object',
'properties' => array(
'parent' => array(
'type' => 'object',
'context' => array( 'view', 'edit' ),
'properties' => array(
'child' => array(
'type' => 'object',
'properties' => array(
'grand' => array(
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'hidden' => array(
'type' => 'string',
'context' => array( 'edit' ),
),
),
),
),
),
),
),
array(
'parent' => array(
'child' => array(
'grand' => 'hi',
'hidden' => 'there',
),
),
),
array(
'parent' => array(
'child' => array(
'grand' => 'hi',
'hidden' => 'there',
),
),
),
),
);
}
}