Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#53621 closed defect (bug) (fixed)

The `wp/v2/block-directory/search` schema is incorrect

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: 6.0 Priority: normal
Severity: normal Version: 5.5
Component: REST API Keywords: has-patch
Focuses: rest-api Cc:


Some problems with the declared schema for the wp/v2/block-directory/search endpoint:

  • rating is a float, not an integer
  • active_installs is an integer, not a string
  • author_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:

Change History (11)

This ticket was mentioned in PR #1487 on WordPress/wordpress-develop by johnbillion.

3 years ago

  • Keywords has-patch added; needs-patch removed

#2 @johnbillion
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: . 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 API.

This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.

3 years ago

adamziel commented on PR #1487:

3 years ago

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.



  • @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' =>


'page' => 1,
'pages' => 1,
'results' => 1,


'plugins' => array(




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(


'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' => '',
'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(


'href' => '',


'curies' => array(


'name' => 'wp',
'href' => '{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

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.

#7 @TimothyBlynJacobs
3 years ago

  • Milestone changed from 5.9 to 6.0

Punting to 6.0 based on comment:6.

johnbillion commented on PR #1487:

2 years ago

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

Looks good to me.

#10 @johnbillion
2 years ago

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

In 53315:

REST API: Correct the schema for the wp/v2/block-directory/search endpoint.

Props zieladam, TimothyBlynJacobs, spacedmonkey, johnbillion

Fixes #53621

Note: See TracTickets for help on using tickets.