Make WordPress Core


Ignore:
Timestamp:
08/27/2020 02:43:40 PM (4 years ago)
Author:
SergeyBiryukov
Message:

REST API: Fix multi-type schemas with integer fields.

In [48306] support for multi-typed schemas was improved to first detect the data type of the value before applying further validation. The integer data type was detected using the new rest_is_integer function. This function used logic, however, that assumed that the value had already passed an is_numeric check. This meant that if integer and string were both acceptable types, the value would always be considered an integer causing the later accurate type validation to fail.

This commit fixes the rest_is_integer logic to include an is_numeric check.

Props rtagliento, TimothyBlynJacobs.
Merges [48881] to the 5.5 branch.
Fixes #51146.

Location:
branches/5.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/tests/phpunit/tests/rest-api.php

    r48555 r48883  
    19201920
    19211921    /**
     1922     * @ticket 51146
     1923     *
     1924     * @dataProvider _dp_rest_is_integer
     1925     *
     1926     * @param bool  $expected Expected result of the check.
     1927     * @param mixed $value    The value to check.
     1928     */
     1929    public function test_rest_is_integer( $expected, $value ) {
     1930        $is_integer = rest_is_integer( $value );
     1931
     1932        if ( $expected ) {
     1933            $this->assertTrue( $is_integer );
     1934        } else {
     1935            $this->assertFalse( $is_integer );
     1936        }
     1937    }
     1938
     1939    public function _dp_rest_is_integer() {
     1940        return array(
     1941            array(
     1942                true,
     1943                1,
     1944            ),
     1945            array(
     1946                true,
     1947                '1',
     1948            ),
     1949            array(
     1950                true,
     1951                0,
     1952            ),
     1953            array(
     1954                true,
     1955                -1,
     1956            ),
     1957            array(
     1958                true,
     1959                '05',
     1960            ),
     1961            array(
     1962                false,
     1963                'garbage',
     1964            ),
     1965            array(
     1966                false,
     1967                5.5,
     1968            ),
     1969            array(
     1970                false,
     1971                '5.5',
     1972            ),
     1973            array(
     1974                false,
     1975                array(),
     1976            ),
     1977            array(
     1978                false,
     1979                true,
     1980            ),
     1981        );
     1982    }
     1983
     1984    /**
    19221985     * @ticket 50300
    19231986     *
     
    20192082                array( 'array', 'string' ),
    20202083            ),
     2084            array(
     2085                'string',
     2086                'hello',
     2087                array( 'integer', 'string' ),
     2088            ),
    20212089        );
    20222090    }
Note: See TracChangeset for help on using the changeset viewer.