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:
parent
b5190458fe
commit
4e0062e5be
|
@ -1250,6 +1250,10 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( 'object' === $args['type'] ) {
|
if ( 'object' === $args['type'] ) {
|
||||||
|
if ( '' === $value ) {
|
||||||
|
$value = array();
|
||||||
|
}
|
||||||
|
|
||||||
if ( $value instanceof stdClass ) {
|
if ( $value instanceof stdClass ) {
|
||||||
$value = (array) $value;
|
$value = (array) $value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 ) );
|
$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() {
|
public function test_type_unknown() {
|
||||||
$schema = array(
|
$schema = array(
|
||||||
'type' => 'lalala',
|
'type' => 'lalala',
|
||||||
|
|
|
@ -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 ) );
|
$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() {
|
public function test_type_unknown() {
|
||||||
$schema = array(
|
$schema = array(
|
||||||
'type' => 'lalala',
|
'type' => 'lalala',
|
||||||
|
|
Loading…
Reference in New Issue