REST API: Correctly infer empty objects passed via query parameters.

Permit passing an empty object as the string "?obj=". The type of the passed empty argument is inferred from the registered schema.

Props TimothyBlynJacobs, steffanhalv, schlessera, dd32.
Fixes #42961.


git-svn-id: https://develop.svn.wordpress.org/trunk@47362 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2020-02-25 15:32:27 +00:00
parent b5190458fe
commit 4e0062e5be
3 changed files with 18 additions and 0 deletions

View File

@ -1250,6 +1250,10 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
}
if ( 'object' === $args['type'] ) {
if ( '' === $value ) {
$value = array();
}
if ( $value instanceof stdClass ) {
$value = (array) $value;
}

View File

@ -275,6 +275,13 @@ class WP_Test_REST_Schema_Sanitization extends WP_UnitTestCase {
$this->assertEquals( array( 'a' => 1 ), rest_sanitize_value_from_schema( (object) array( 'a' => '1' ), $schema ) );
}
/**
* @ticket 42961
*/
public function test_type_object_accepts_empty_string() {
$this->assertEquals( array(), rest_sanitize_value_from_schema( '', array( 'type' => 'object' ) ) );
}
public function test_type_unknown() {
$schema = array(
'type' => 'lalala',

View File

@ -288,6 +288,13 @@ class WP_Test_REST_Schema_Validation extends WP_UnitTestCase {
$this->assertTrue( rest_validate_value_from_schema( (object) array( 'a' => 1 ), $schema ) );
}
/**
* @ticket 42961
*/
public function test_type_object_allows_empty_string() {
$this->assertTrue( rest_validate_value_from_schema( '', array( 'type' => 'object' ) ) );
}
public function test_type_unknown() {
$schema = array(
'type' => 'lalala',