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

WordPress Trac noreply at wordpress.org
Thu Dec 17 13:52:56 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        |   Keywords:
  Focuses:  rest-api      |
--------------------------+-----------------------------
 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.

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


More information about the wp-trac mailing list