Make WordPress Core

Changeset 50656


Ignore:
Timestamp:
04/04/2021 07:11:07 PM (4 years ago)
Author:
SergeyBiryukov
Message:

REST API: Correct enum validation for numeric values.

When validating enum values as integer or number, consider a number with a zero fractional part to be equivalent to an integer of the same value.

In rest_are_values_equal(), when comparing two values of type int or float (in any combination), first cast both of them to float and then compare.

This matches some test cases from the official JSON Schema test suite.

Follow-up to [50010].

Props yakimun, stefanjoebstl, TimothyBlynJacobs, rachelbaker.
Merges [50653] to the 5.7 branch.
Fixes #52932.

Location:
branches/5.7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.7

  • branches/5.7/src/wp-includes/rest-api.php

    r50655 r50656  
    19251925
    19261926        return true;
     1927    }
     1928
     1929    if ( is_int( $value1 ) && is_float( $value2 )
     1930        || is_float( $value1 ) && is_int( $value2 )
     1931    ) {
     1932        return (float) $value1 === (float) $value2;
    19271933    }
    19281934
  • branches/5.7/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r50010 r50656  
    251251    /**
    252252     * @ticket 51911
     253     * @ticket 52932
    253254     *
    254255     * @dataProvider data_different_types_of_value_and_enum_elements
     
    306307            ),
    307308            array(
     309                1,
     310                array(
     311                    'type' => 'integer',
     312                    'enum' => array( 0.0, 1.0 ),
     313                ),
     314                true,
     315            ),
     316            array(
    308317                1.0,
    309318                array(
     
    376385                    'type' => 'number',
    377386                    'enum' => array( 0.0, 1.0 ),
     387                ),
     388                true,
     389            ),
     390            array(
     391                1,
     392                array(
     393                    'type' => 'number',
     394                    'enum' => array( 0, 1 ),
    378395                ),
    379396                true,
Note: See TracChangeset for help on using the changeset viewer.