From c9618c09ad5c169f744cbab16c1d2d8d2268af8d Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 31 Oct 2016 05:44:56 +0000 Subject: [PATCH] REST API: Allow parameters defined as `array` to be sent as CSVs. This allows parameters that are often handled as CSVs to be properly parsed. Fixes #38586. git-svn-id: https://develop.svn.wordpress.org/trunk@39048 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 2 +- .../tests/rest-api/rest-schema-validation.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index dc359aec50..988dd9c778 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -996,7 +996,7 @@ function rest_get_avatar_sizes() { function rest_validate_value_from_schema( $value, $args, $param = '' ) { if ( 'array' === $args['type'] ) { if ( ! is_array( $value ) ) { - return new WP_Error( 'rest_invalid_param', sprintf( /* translators: 1: parameter, 2: type name */ __( '%1$s is not of type %2$s.' ), $param, 'array' ) ); + $value = preg_split( '/[\s,]+/', $value ); } foreach ( $value as $index => $v ) { $is_valid = rest_validate_value_from_schema( $v, $args['items'], $param . '[' . $index . ']' ); diff --git a/tests/phpunit/tests/rest-api/rest-schema-validation.php b/tests/phpunit/tests/rest-api/rest-schema-validation.php index 9e413daff6..1d8500882d 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-validation.php +++ b/tests/phpunit/tests/rest-api/rest-schema-validation.php @@ -103,4 +103,16 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase { $this->assertTrue( rest_validate_value_from_schema( array( 1 ), $schema ) ); $this->assertWPError( rest_validate_value_from_schema( array( true ), $schema ) ); } + + public function test_type_array_as_csv() { + $schema = array( + 'type' => 'array', + 'items' => array( + 'type' => 'number', + ), + ); + $this->assertTrue( rest_validate_value_from_schema( '1', $schema ) ); + $this->assertTrue( rest_validate_value_from_schema( '1,2,3', $schema ) ); + $this->assertWPError( rest_validate_value_from_schema( 'lol', $schema ) ); + } }