Make WordPress Core

Changeset 46563


Ignore:
Timestamp:
10/21/2019 07:08:34 PM (7 years ago)
Author:
kadamwhite
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r46454 r46563  
    431431
    432432                        if ( null === $rest_args['schema']['default'] ) {
    433                                 $rest_args['schema']['default'] = $this->get_default_for_type( $type );
     433                                $rest_args['schema']['default'] = static::get_empty_value_for_type( $type );
    434434                        }
    435435
     
    502502                }
    503503
     504                if ( '' === $value && in_array( $schema['type'], array( 'boolean', 'integer', 'number' ), true ) ) {
     505                        $value = static::get_empty_value_for_type( $schema['type'] );
     506                }
     507
    504508                if ( is_wp_error( rest_validate_value_from_schema( $value, $schema ) ) ) {
    505509                        return null;
     
    560564
    561565        /**
    562          * Gets the default value for a schema type.
     566         * Gets the empty value for a schema type.
    563567         *
    564568         * @since 5.3.0
     
    567571         * @return mixed
    568572         */
    569         protected function get_default_for_type( $type ) {
     573        protected static function get_empty_value_for_type( $type ) {
    570574                switch ( $type ) {
    571575                        case 'string':
  • trunk/tests/phpunit/tests/rest-api/rest-post-meta-fields.php

    r46454 r46563  
    19801980        /**
    19811981         * @ticket 43392
    1982          */
    1983         public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() {
    1984                 register_post_meta(
    1985                         'post',
    1986                         'boolean',
     1982         * @ticket 48363
     1983         * @dataProvider _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable
     1984         */
     1985        public function test_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable( $type, $stored, $expected ) {
     1986                register_post_meta(
     1987                        'post',
     1988                        'safe',
    19871989                        array(
    19881990                                'single'       => true,
    19891991                                'show_in_rest' => true,
    1990                                 'type'         => 'boolean',
    1991                         )
    1992                 );
    1993 
    1994                 update_post_meta( self::$post_id, 'boolean', 'true' );
     1992                                'type'         => $type,
     1993                        )
     1994                );
     1995
     1996                update_post_meta( self::$post_id, 'safe', $stored );
    19951997
    19961998                $request  = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    19971999                $response = rest_get_server()->dispatch( $request );
    19982000
    1999                 $this->assertTrue( $response->get_data()['meta']['boolean'] );
     2001                $this->assertSame( $expected, $response->get_data()['meta']['safe'] );
     2002        }
     2003
     2004        public function _dp_meta_values_are_not_set_to_null_in_response_if_type_safely_serializable() {
     2005                return array(
     2006                        array( 'boolean', 'true', true ),
     2007                        array( 'boolean', 'false', false ),
     2008                        array( 'boolean', '1', true ),
     2009                        array( 'boolean', '0', false ),
     2010                        array( 'boolean', '', false ),
     2011                        array( 'integer', '', 0 ),
     2012                        array( 'integer', '1', 1 ),
     2013                        array( 'integer', '0', 0 ),
     2014                        array( 'number', '', 0.0 ),
     2015                        array( 'number', '1.1', 1.1 ),
     2016                        array( 'number', '0.0', 0.0 ),
     2017                        array( 'string', '', '' ),
     2018                        array( 'string', '1', '1' ),
     2019                        array( 'string', '0', '0' ),
     2020                        array( 'string', 'str', 'str' ),
     2021                );
    20002022        }
    20012023
     
    26002622
    26012623        /**
     2624         * @ticket 48363
     2625         */
     2626        public function test_boolean_meta_update_to_false_stores_0() {
     2627                $this->grant_write_permission();
     2628
     2629                register_post_meta(
     2630                        'post',
     2631                        'boolean',
     2632                        array(
     2633                                'single'            => true,
     2634                                'type'              => 'boolean',
     2635                                'show_in_rest'      => true,
     2636                                'sanitize_callback' => function( $value ) {
     2637                                        return $value ? '1' : '0';
     2638                                },
     2639                        )
     2640                );
     2641
     2642                update_post_meta( self::$post_id, 'boolean', 1 );
     2643
     2644                $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     2645                $request->set_body_params(
     2646                        array(
     2647                                'meta' => array(
     2648                                        'boolean' => false,
     2649                                ),
     2650                        )
     2651                );
     2652
     2653                $response = rest_get_server()->dispatch( $request );
     2654                $this->assertEquals( 200, $response->get_status() );
     2655                $this->assertEquals( '0', get_post_meta( self::$post_id, 'boolean', true ) );
     2656        }
     2657
     2658        /**
    26022659         * Internal function used to disable an insert query which
    26032660         * will trigger a wpdb error for testing purposes.
Note: See TracChangeset for help on using the changeset viewer.