#52106 closed defect (bug) (fixed)
REST API json_encode error results in `null` response
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 5.7 | Priority: | normal |
Severity: | normal | Version: | |
Component: | REST API | Keywords: | has-patch |
Focuses: | rest-api | Cc: |
Description (last modified by )
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
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:
- PHP Coding Standards
- CSS Coding Standards
- HTML Coding Standards
- JavaScript Coding Standards
- Accessibility Coding Standards
- Inline Documentation Standards
Thank you,
The WordPress Project
#5
@
4 years ago
- Owner set to TimothyBlynJacobs
- Resolution set to fixed
- Status changed from new to closed
In 49918:
hellofromtonya commented on PR #823:
4 years ago
#7
Merged with changeset https://core.trac.wordpress.org/changeset/49918
hellofromtonya commented on PR #824:
4 years ago
#8
Trac ticket closed with changeset https://core.trac.wordpress.org/changeset/49918
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