[wp-trac] [WordPress Trac] #52106: REST API json_encode error results in `null` response

WordPress Trac noreply at wordpress.org
Thu Dec 17 14:02:38 UTC 2020


#52106: REST API json_encode error results in `null` response
--------------------------+------------------------------
 Reporter:  atimmer       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  REST API      |     Version:
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:  rest-api
--------------------------+------------------------------
Description changed by atimmer:

Old description:

> When an endpoint returns data that results in an error when json
> serialising, the REST API code outputs `null`. From the code the
> intention is to output the JSON encoding error instead.
>
> I tested this on 5.4 and 5.6.
>
> A dummy plugin to reproduce this:
>
> {{{#!php
> <?php
> /*
> Plugin Name:    REST API Bug
> Version:        1.0.0
> Description:    Shows a bug in the REST API
> */
>
> add_action( 'rest_api_init', function() {
>         register_rest_route(
>                 'v1',
>                 '/show-bug',
>                 array(
>                         array(
>                                 'methods'             =>
> \WP_REST_Server::READABLE,
>                                 'callback'            => function() {
>                                         return new
> \WP_REST_Response(INF);
>                                 },
>                                 'permission_callback' => '__return_true',
>                                 'args'                => array(),
>                         ),
>                 )
>         );
> });
> }}}
>
> Go to `/wp-json/v1/show-bug` and see that `null` is the output. Fixing
> this saves developers time by preventing them from having to debug this.

New description:

 When an endpoint returns data that results in an error when json
 serialising, the REST API code outputs `null`. From the code the intention
 is to output the JSON encoding error instead.

 I tested this on 5.4 and 5.6.

 A dummy plugin to reproduce this:

 {{{#!php
 <?php
 /*
 Plugin Name:    REST API Bug
 Version:        1.0.0
 Description:    Shows a bug in the REST API
 */

 add_action( 'rest_api_init', function() {
         register_rest_route(
                 'v1',
                 '/show-bug',
                 array(
                         array(
                                 'methods'             =>
 \WP_REST_Server::READABLE,
                                 'callback'            => function() {
                                         return new \WP_REST_Response(INF);
                                 },
                                 'permission_callback' => '__return_true',
                                 'args'                => array(),
                         ),
                 )
         );
 });
 }}}

 Go to `/wp-json/v1/show-bug` and see that `null` is the output. Fixing
 this saves developers time by preventing them from having to debug this.

 I believe the expected response is
 "{"code":"rest_encode_error","message":"Inf and NaN cannot be JSON
 encoded","data":{"status":500}}". With the PR/patch that is the response I
 get.

--

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


More information about the wp-trac mailing list