From e756de59e5fd9002dfa7f4ce421decc119510e28 Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Thu, 4 Jun 2020 16:41:47 +0000 Subject: [PATCH] REST API: Ensure that all properties of `get_endpoint_args_for_item_schema` are listed. * Add new var $valid_schema_properties to match rest_validate_value_from_schema() * Unit test to ensure all valid properties exists, and non-valid properties are ignored Fixes: #50301. Props: pentatonicfunk, TimothyBlynJacobs. git-svn-id: https://develop.svn.wordpress.org/trunk@47911 602fd350-edb4-49c9-b593-d223f7449a82 --- .../endpoints/class-wp-rest-controller.php | 23 +++++++++++--- .../tests/rest-api/rest-controller.php | 31 +++++++++++++++++++ .../tests/rest-api/rest-test-controller.php | 31 +++++++++++++++++-- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php index da5254a327..a2d1134f11 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php @@ -626,9 +626,24 @@ abstract class WP_REST_Controller { */ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { - $schema = $this->get_item_schema(); - $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); - $endpoint_args = array(); + $schema = $this->get_item_schema(); + $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); + $endpoint_args = array(); + $valid_schema_properties = array( + 'type', + 'format', + 'enum', + 'items', + 'properties', + 'additionalProperties', + 'minimum', + 'maximum', + 'exclusiveMinimum', + 'exclusiveMaximum', + 'minLength', + 'maxLength', + 'pattern', + ); foreach ( $schema_properties as $field_id => $params ) { @@ -654,7 +669,7 @@ abstract class WP_REST_Controller { $endpoint_args[ $field_id ]['required'] = true; } - foreach ( array( 'type', 'format', 'enum', 'items', 'properties', 'additionalProperties' ) as $schema_prop ) { + foreach ( $valid_schema_properties as $schema_prop ) { if ( isset( $params[ $schema_prop ] ) ) { $endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ]; } diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php index 094033de92..cb29822d97 100644 --- a/tests/phpunit/tests/rest-api/rest-controller.php +++ b/tests/phpunit/tests/rest-api/rest-controller.php @@ -247,6 +247,33 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { $this->assertEquals( 'a', $args['somedefault']['default'] ); } + /** + * @ticket 50301 + */ + public function test_get_endpoint_args_for_item_schema_arg_properties() { + + $controller = new WP_REST_Test_Controller(); + $args = $controller->get_endpoint_args_for_item_schema(); + + foreach ( array( 'minLength', 'maxLength', 'pattern' ) as $property ) { + $this->assertArrayHasKey( $property, $args['somestring'] ); + } + + foreach ( array( 'minimum', 'maximum', 'exclusiveMinimum', 'exclusiveMaximum' ) as $property ) { + $this->assertArrayHasKey( $property, $args['someinteger'] ); + } + + $this->assertArrayHasKey( 'items', $args['somearray'] ); + + foreach ( array( 'properties', 'additionalProperties' ) as $property ) { + $this->assertArrayHasKey( $property, $args['someobject'] ); + } + + // ignored properties + $this->assertArrayNotHasKey( 'ignored_prop', $args['someobject'] ); + + } + /** * @dataProvider data_get_fields_for_response, */ @@ -267,6 +294,8 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 'someenum', 'someargoptions', 'somedefault', + 'somearray', + 'someobject', ), $fields ); @@ -298,6 +327,8 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase { 'someenum', 'someargoptions', 'somedefault', + 'somearray', + 'someobject', ), ), ); diff --git a/tests/phpunit/tests/rest-api/rest-test-controller.php b/tests/phpunit/tests/rest-api/rest-test-controller.php index 93fac6feae..a201ec1c80 100644 --- a/tests/phpunit/tests/rest-api/rest-test-controller.php +++ b/tests/phpunit/tests/rest-api/rest-test-controller.php @@ -39,11 +39,18 @@ class WP_REST_Test_Controller extends WP_REST_Controller { 'somestring' => array( 'type' => 'string', 'description' => 'A pretty string.', + 'minLength' => 3, + 'maxLength' => 3, + 'pattern' => '[a-zA-Z]+', 'context' => array( 'view' ), ), 'someinteger' => array( - 'type' => 'integer', - 'context' => array( 'view' ), + 'type' => 'integer', + 'minimum' => 100, + 'maximum' => 200, + 'exclusiveMinimum' => true, + 'exclusiveMaximum' => true, + 'context' => array( 'view' ), ), 'someboolean' => array( 'type' => 'boolean', @@ -93,6 +100,26 @@ class WP_REST_Test_Controller extends WP_REST_Controller { 'context' => array( 'view' ), 'default' => 'a', ), + 'somearray' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + 'context' => array( 'view' ), + ), + 'someobject' => array( + 'type' => 'object', + 'additionalProperties' => array( + 'type' => 'string', + ), + 'properties' => array( + 'object_id' => array( + 'type' => 'integer', + ), + ), + 'ignored_prop' => 'ignored_prop', + 'context' => array( 'view' ), + ), ), );