Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#52106 closed defect (bug) (fixed)

REST API json_encode error results in `null` response

Reported by: atimmer's profile atimmer Owned by: timothyblynjacobs's profile TimothyBlynJacobs
Milestone: 5.7 Priority: normal
Severity: normal Version:
Component: REST API Keywords: has-patch
Focuses: rest-api Cc:

Description (last modified by atimmer)

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
/*
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.

Change History (8)

github-actions[bot] commented on PR #823:


4 years ago
#1

Hi @atimmer! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

#2 @atimmer
4 years ago

  • Description modified (diff)

This ticket was mentioned in PR #824 on WordPress/wordpress-develop by technosailor.


4 years ago
#3

  • Keywords has-patch added

Fixes a bug where null responses were sent when a PHP error occurs.

In this case, the array key 0 doesn't exist, so a Notice is thrown. By correcting this inaccurate array key usage, the output is as expected.

I did not include a test with this PR because tests/rest-api/rest-server::test_does_not_echo_body_for_null_responses() already checks null responses. This one fails because of the notice.

Trac ticket: https://core.trac.wordpress.org/ticket/52106

github-actions[bot] commented on PR #824:


4 years ago
#4

Hi @technosailor! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

#5 @TimothyBlynJacobs
4 years ago

  • Owner set to TimothyBlynJacobs
  • Resolution set to fixed
  • Status changed from new to closed

In 49918:

REST API: Properly display JSON encoding errors.

Previously null was returned because the server was accessing the incorrect variable.

Props atimmer, technosailor.
Fixes #52106.

#6 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.7

hellofromtonya commented on PR #824:


4 years ago
#8

Trac ticket closed with changeset https://core.trac.wordpress.org/changeset/49918

Note: See TracTickets for help on using tickets.