REST API: Cast empty meta values to correct scalar types in REST response.

Introducing complex meta value handling in [45807] unintentionally removed value casting for empty scalar meta values.

Props TimothyBlynJacobs, chrisvanpatten, rmccue, kadamwhite.
Fixes #48363.



git-svn-id: https://develop.svn.wordpress.org/trunk@46563 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White 2019-10-21 19:08:34 +00:00
parent 0ad9c8231e
commit 56aa018065
2 changed files with 69 additions and 8 deletions

View File

@ -430,7 +430,7 @@ abstract class WP_REST_Meta_Fields {
$type = ! empty( $rest_args['schema']['type'] ) ? $rest_args['schema']['type'] : $type;
if ( null === $rest_args['schema']['default'] ) {
$rest_args['schema']['default'] = $this->get_default_for_type( $type );
$rest_args['schema']['default'] = static::get_empty_value_for_type( $type );
}
$rest_args['schema'] = $this->default_additional_properties_to_false( $rest_args['schema'] );
@ -501,6 +501,10 @@ abstract class WP_REST_Meta_Fields {
$schema = $args['schema']['items'];
}
if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) {
$value = static::get_empty_value_for_type( $schema['type'] );
}
if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
return null;
}
@ -559,14 +563,14 @@ abstract class WP_REST_Meta_Fields {
}
/**
* Gets the default value for a schema type.
* Gets the empty value for a schema type.
*
* @since 5.3.0
*
* @param string $type The schema type.
* @return mixed
*/
protected function get_default_for_type( $type ) {
protected static function get_empty_value_for_type( $type ) {
switch ( $type ) {
case 'string':
return '';

View File

@ -1979,24 +1979,46 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
/**
* @ticket 43392
* @ticket 48363
* @dataProvider _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable
*/
public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() {
public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable( $type, $stored, $expected ) {
register_post_meta(
'post',
'boolean',
'safe',
array(
'single' => true,
'show_in_rest' => true,
'type' => 'boolean',
'type' => $type,
)
);
update_post_meta( self::$post_id, 'boolean', 'true' );
update_post_meta( self::$post_id, 'safe', $stored );
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$response = rest_get_server()->dispatch( $request );
$this->assertTrue( $response->get_data()['meta']['boolean'] );
$this->assertSame( $expected, $response->get_data()['meta']['safe'] );
}
public function _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() {
return array(
array( 'boolean', 'true', true ),
array( 'boolean', 'false', false ),
array( 'boolean', '1', true ),
array( 'boolean', '0', false ),
array( 'boolean', '', false ),
array( 'integer', '', 0 ),
array( 'integer', '1', 1 ),
array( 'integer', '0', 0 ),
array( 'number', '', 0.0 ),
array( 'number', '1.1', 1.1 ),
array( 'number', '0.0', 0.0 ),
array( 'string', '', '' ),
array( 'string', '1', '1' ),
array( 'string', '0', '0' ),
array( 'string', 'str', 'str' ),
);
}
/**
@ -2598,6 +2620,41 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
$this->assertSame( array( false, true ), get_post_meta( self::$post_id, 'items', true ) );
}
/**
* @ticket 48363
*/
public function test_boolean_meta_update_to_false_stores_0() {
$this->grant_write_permission();
register_post_meta(
'post',
'boolean',
array(
'single' => true,
'type' => 'boolean',
'show_in_rest' => true,
'sanitize_callback' => function( $value ) {
return $value ? '1' : '0';
},
)
);
update_post_meta( self::$post_id, 'boolean', 1 );
$request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
$request->set_body_params(
array(
'meta' => array(
'boolean' => false,
),
)
);
$response = rest_get_server()->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( '0', get_post_meta( self::$post_id, 'boolean', true ) );
}
/**
* Internal function used to disable an insert query which
* will trigger a wpdb error for testing purposes.