#53621 closed defect (bug) (fixed)
The `wp/v2/block-directory/search` schema is incorrect
Reported by: | johnbillion | Owned by: | johnbillion |
---|---|---|---|
Milestone: | 6.0 | Priority: | normal |
Severity: | normal | Version: | 5.5 |
Component: | REST API | Keywords: | has-patch |
Focuses: | rest-api | Cc: |
Description
Some problems with the declared schema for the wp/v2/block-directory/search
endpoint:
rating
is a float, not an integeractive_installs
is an integer, not a stringauthor_block_rating
is a float, not an integer
This can be seen by performing a search for a common term such as "block" and looking through the various results.
This can also be seen in the code: https://github.com/WordPress/wordpress-develop/blob/73e24c997c1182ac48856c1546cf4719b51ae27d/src/wp-includes/rest-api/endpoints/class-wp-rest-block-directory-controller.php#L127-L130
Change History (11)
This ticket was mentioned in PR #1487 on WordPress/wordpress-develop by johnbillion.
3 years ago
#1
- Keywords has-patch added; needs-patch removed
#2
@
3 years ago
I've got test coverage for this in my wp-json-schemas
package that runs a live search and then validates the response against its JSON schema: https://github.com/johnbillion/wp-json-schemas/commit/1eeb02c23a2d80bb57c67cb4efd95997b29ca99a . I had to correct these three types to get the validation to pass.
It would be good to have coverage here either via a live search or via a mock response from the w.org API.
This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.
3 years ago
3 years ago
#4
Here's a unit test that could be added to tests/phpunit/tests/rest-api/rest-block-directory-controller.php
. I started off with some actual API data, but turns out there is a get_mock_plugin()
that does essentially the same thing so I used that instead.
{{{php
/
- @ticket 53621 */
public function test_get_items_response_conforms_to_schema() {
wp_set_current_user( self::$admin_id );
$plugin = $this->get_mock_plugin();
$cb = function () use ( $plugin ) {
return (object) array(
'info' =>
array(
'page' => 1,
'pages' => 1,
'results' => 1,
),
'plugins' => array(
$plugin,
),
);
};
add_filter( 'plugins_api', $cb );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-directory/search' );
$request->set_query_params( array( 'term' => 'cache' ) );
$expected = array(
array(
'name' => 'sortabrilliant/guidepost',
'title' => 'Guidepost',
'description' => 'A guidepost gives you directions. It lets you know where you’re going. It gives you a preview of what’s to come.',
'id' => 'guidepost',
'rating' => 4.3,
'rating_count' => 90,
'active_installs' => 100,
'author_block_rating' => 0,
'author_block_count' => 1,
'author' => 'sorta brilliant',
'icon' => 'https://ps.w.org/guidepost/assets/icon-128x128.jpg?rev=2235512',
'last_updated' => gmdate( 'Y-m-d\TH:i:s', strtotime( $pluginlast_updated? ) ),
'humanized_updated' => sprintf( '%s ago', human_time_diff( strtotime( $pluginlast_updated? ) ) ),
'_links' => array(
'wp:install-plugin' => array(
array(
'href' => 'http://example.org/index.php?rest_route=%2Fwp%2Fv2%2Fplugins&slug=guidepost',
),
),
'curies' => array(
array(
'name' => 'wp',
'href' => 'https://api.w.org/{rel}',
'templated' => true,
),
),
),
),
);
$result = rest_do_request( $request );
remove_filter( 'plugins_api', $cb );
$this->assertNotWPError( $result->as_error() );
$this->assertSame( 200, $result->status );
$this->assertSame( $expected, $result->get_data() );
}
}}}
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
TimothyBJacobs commented on PR #1487:
3 years ago
#6
This test is somewhat of a duplicate of our existing test_prepare_item
. What we probably actually want here is to test that the REST API response validates against the JSON Schema for the endpoint.
We can do this by applying rest_validate_value_from_schema
on the response data. We'll need to grab the schema for the endpoint which could be done by making an OPTIONS
request like we do in test_context_param
(preferred) or instantiating a WP_REST_Block_Directory_Controller
instance and just calling get_public_item_schema
.
johnbillion commented on PR #1487:
2 years ago
#8
I adjusted then test as per Timothy's recommendation. It now validates the result against the schema. It successfully fails with the old schema and passes with the corrected one.
spacedmonkey commented on PR #1487:
2 years ago
#9
Looks good to me.
#10
@
2 years ago
- Owner set to johnbillion
- Resolution set to fixed
- Status changed from new to closed
In 53315:
Trac ticket: https://core.trac.wordpress.org/ticket/53621