Opened 7 years ago
Closed 7 years ago
#42989 closed defect (bug) (worksforme)
REST API: Using array type when defining fields in register_rest_route() causes undefined index notices
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | |
Focuses: | rest-api | Cc: |
Description
I have error reporting turned on in dev. Then I register a route like this:
register_rest_route( 'test/v1', 'array_test', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => function ( WP_REST_Request $request ) {
// Just reflect back the value of the test parameter
return rest_ensure_response( $request->get_param( 'test' ) );
},
'args' => array(
'test' => array(
'type' => 'array'
)
)
)
));
Then I call it like this:
<your-dev-domain>/wp-json/test/v1/array_test/?test[]=value1&test[]=value2
I expect to get back a JSON response where the content looks like this: ["value1","value2"]. Instead I get Notice: Undefined index: items in /var/www/html/wp-includes/rest-api.php on line 1092 twice and then Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/rest-api.php:1092) in /var/www/html/wp-includes/rest-api/class-wp-rest-server.php on line 1248.
Change History (5)
#2
@
7 years ago
@sarukuku what happens if you set the items
key for the test
arg? It looks like you haven't set what type of values expect from the array.
<?php register_rest_route( 'test/v1', 'array_test', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => function ( WP_REST_Request $request ) { // Just reflect back the value of the test parameter return rest_ensure_response( $request->get_param( 'test' ) ); }, 'args' => array( 'test' => array( 'type' => 'array', 'items' => 'string' ) ) ) ));
Your debugging output is telling you exactly where to look in the code to see what is wrong. https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api.php#L1092
Is your bug report that items
should not be required for array
type params?
#3
@
7 years ago
I tried setting the items
key for the test
arg to string
like you suggested above but I get something like this after that:
Your debugging output is telling you exactly where to look in the code to see what is wrong.
https://core.trac.wordpress.org/browser/trunk/src/wp-includes/rest-api.php#L1092
I tried reading the rest api code from trac but couldn't figure out what the items
keyword was used for. I also tried to google for documentation or blog posts where someone would have used the array
type but came short.
Is your bug report that
items
should not be required forarray
type params?
Because I couldn't find any documentation or examples related to the different options available for the type
key for args and then encountered an error I couldn't fix I felt that opening an issue here was the right thing to do. At this point I'm asking the community if I'm using this wrong or if there's a possible bug or a chance for improvement here.
What comes to the validation I think having a compulsory type set for the array values sounds like a good idea as long as it supports all current types, works recursively (like it does now) and its usage is documented somewhere.
#4
@
7 years ago
It seems that the items
needs to be an array that has the key type
in it. The example below works as I'd expect and It's also much more readable.
<?php register_rest_route( 'test/v1', 'array_test', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => function ( WP_REST_Request $request ) { // Just reflect back the value of the test parameter return rest_ensure_response( $request->get_param( 'test' ) ); }, 'args' => array( 'test' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ) ) ) ) ));
But as we see here it's quite hard to figure these out without any documentation. This issue as it is can (probably?) be closed now but how should we go with improving the documentation on this? Should I open a new issue to somewhere else?
#5
@
7 years ago
- Milestone Awaiting Review deleted
- Resolution set to worksforme
- Status changed from new to closed
- Version changed from 4.9.1 to 4.7
@sarukuku You can update the documentation for the REST API handbook from this Github repo: https://github.com/WP-API/docs .
I am going to close this Trac ticket for now. It can be reopened in the future if needed.
These might be related but I'm not sure:
https://core.trac.wordpress.org/ticket/42875
https://core.trac.wordpress.org/ticket/42961