[wp-trac] [WordPress Trac] #46191: WP_REST_Request::has_valid_params() should utilize get_error_messages() and not get_error_message(). Unexpected behavior occurs when you try to WP_Error->add() in the REST API
WordPress Trac
noreply at wordpress.org
Tue Feb 5 22:40:32 UTC 2019
#46191: WP_REST_Request::has_valid_params() should utilize get_error_messages() and
not get_error_message(). Unexpected behavior occurs when you try to
WP_Error->add() in the REST API
--------------------------+-----------------------------
Reporter: tmfespresso | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: trunk
Severity: normal | Keywords: needs-patch
Focuses: rest-api |
--------------------------+-----------------------------
This is best illustrated via some code that attempts to extend the REST
API, please see the comments above `CoffeeApi->validateBeans()`.
{{{#!php
<?php
class CoffeeApi {
// CONTEXT ONLY: this is fine and provided for context
public function registerRoutes()
{
$version = '1';
$namespace = 'coffee/v' . $version;
$base = 'espresso';
register_rest_route( $namespace, '/' . $base,
[
[
'methods' => \WP_REST_Server::READABLE, // GET
'callback' => [$this, 'statusCheck'],
'permission_callback' => [$this, 'authorize'],
],
[
'methods' => \WP_REST_Server::CREATABLE, // POST
'callback' => [$this, 'createOrUpdate'],
'permission_callback' => [$this, 'authorize'],
'args' => array(
'beanId' => array(
'validate_callback' => [$this,
'validateBeans']
),
),
],
]
);
}
// CONTEXT ONLY: this is fine and provided for context
public function createOrUpdate(\WP_REST_Request $request)
{
$parameters = $request->get_json_params();
// TODO: pretend stuff happens here
return rest_ensure_response( 'SUCCESS: Record created.' );
}
/**
* WARNING: this does not result in the expected behavior
*
* TEST CASE: the parameter tested here is non-numeric and fails the
first condition
*
* EXPLANATION OF PROBLEM:
* Because of https://github.com/WordPress/WordPress/blob/master/wp-
includes/rest-api/class-wp-rest-request.php#L878
* utilizing get_error_message() instead of get_error_messages(), only
'Bean data is invalid.' gets returned.
* E.g.
*
* {"code":"rest_invalid_param","message":"Invalid parameter(s):
merchantAccountId","data":{"status":400,"params":
* {"merchantAccountId":"User data is invalid."}}}
*
* If get_error_messages() is used, the error response looks like this:
* {"code":"rest_invalid_param","message":"Invalid parameter(s):
merchantAccountId","data":{"status":400,"params":
* {"merchantAccountId":["User data is invalid.","Merchant Account ID
must be numeric."]}}}
** /
public function validateBeans($rawBeanId){
$userError = new \WP_Error( 'rest_invalid_validate_beans',
esc_html__( 'Bean data is invalid.', 'coffee-api' ), array( 'status' =>
400 ) );
// this gets ignored in the REST API returned errors
if(!is_numeric( $rawBeanId )){
$userError->add('rest_invalid_validate_beans', esc_html__(
'Bean data must be numeric.', 'coffee-api' ));
return $userError;
}
// if you get here, this also gets ignored in the REST API
returned errors
if(!$this->otherCondition()){
$userError->add('rest_invalid_validate_beans', esc_html__(
'Fail for funsies.', 'coffee-api' ));
return $userError;
}
return true;
}
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/46191>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list