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.
Built from https://develop.svn.wordpress.org/branches/5.7@50656


git-svn-id: http://core.svn.wordpress.org/branches/5.7@50268 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-04-04 19:13:03 +00:00
parent 69bfd1ff63
commit 551b6260da
2 changed files with 7 additions and 1 deletions

View File

@ -1926,6 +1926,12 @@ function rest_are_values_equal( $value1, $value2 ) {
return true; return true;
} }
if ( is_int( $value1 ) && is_float( $value2 )
|| is_float( $value1 ) && is_int( $value2 )
) {
return (float) $value1 === (float) $value2;
}
return $value1 === $value2; return $value1 === $value2;
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.7.1-alpha-50655'; $wp_version = '5.7.1-alpha-50656';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.