[wp-trac] [WordPress Trac] #65271: REST API Integer Type Check Fails on Large Ints

WordPress Trac noreply at wordpress.org
Wed May 20 17:41:41 UTC 2026


#65271: REST API Integer Type Check Fails on Large Ints
--------------------------------------+-----------------------------
 Reporter:  kevinfodness              |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  Future Release
Component:  REST API                  |     Version:  5.5
 Severity:  minor                     |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+-----------------------------

Comment (by westonruter):

 While I originally suggested something like `is_numeric( $x ) && !
 str_contains( (string) $maybe_integer, '.' )`, I just learned that
 `is_numeric()` returns true for numbers in scientific notation. This would
 fail when `'15e-1'` is passed as this is `1.5`.

 I also just learned that scientific notation is supported by
 `rest_is_integer()`: `rest_is_integer( '15e-0' )` returns `true`. I wonder
 if this is expected, as there aren't any unit tests for it in
 `\Tests_REST_API::test_rest_is_integer()`. It would be a change in
 behavior from what @gautam23 suggests in the [https://github.com/WordPress
 /wordpress-develop/pull/11883 regex PR].

 This is what Claude Opus 4.7 suggests taking into account all the above:

 {{{#!php
 <?php
 function rest_is_integer( $maybe_integer ) {
         if ( is_int( $maybe_integer ) ) {
                 return true;
         }

         // A canonical integer string of any magnitude — verified without
 float conversion.
         if ( is_string( $maybe_integer ) && preg_match(
 '/^\s*[+-]?[0-9]+\s*$/', $maybe_integer ) ) {
                 return true;
         }

         // Decimal and scientific-notation strings (and floats) keep their
 historical behavior.
         return is_numeric( $maybe_integer ) && floor( (float)
 $maybe_integer ) === (float) $maybe_integer;
 }
 }}}

 Regardless, I think we need to update the data provider to include more
 examples, including scientific notation strings, both floats (e.g.
 `15e-1`) and ints (e.g. `15e-0`).

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/65271#comment:18>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list