REST API: Don't assume all item schemas have properties.

All schema types, not just objects, are permitted as the base type of a resource. A future patch could add validation support for those types, but this fix only prevents a PHP warning from being issued.

Props dhavalkasvala, johnwatkins0, birgire.
Fixes #48785.


git-svn-id: https://develop.svn.wordpress.org/trunk@47328 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Timothy Jacobs 2020-02-20 16:56:17 +00:00
parent 6194b3c5d2
commit 8a62f46beb
4 changed files with 98 additions and 2 deletions

View File

@ -344,8 +344,10 @@ abstract class WP_REST_Controller {
$schema = $this->get_item_schema();
foreach ( $schema['properties'] as &$property ) {
unset( $property['arg_options'] );
if ( ! empty( $schema['properties'] ) ) {
foreach ( $schema['properties'] as &$property ) {
unset( $property['arg_options'] );
}
}
return $schema;

View File

@ -154,6 +154,7 @@ require __DIR__ . '/exceptions.php';
require __DIR__ . '/utils.php';
require __DIR__ . '/spy-rest-server.php';
require __DIR__ . '/class-wp-rest-test-search-handler.php';
require __DIR__ . '/class-wp-rest-test-configurable-controller.php';
require __DIR__ . '/class-wp-fake-block-type.php';
/**

View File

@ -0,0 +1,60 @@
<?php
/**
* Unit tests covering WP_REST_Controller functionality using a flexible schema.
*
* @package WordPress
* @subpackage REST API
* @since 5.4.0
*/
/**
* WP_REST_Test_Configurable_Controller class.
*
* @group restapi
*
* @since 5.4.0
*/
class WP_REST_Test_Configurable_Controller extends WP_REST_Controller {
/**
* Test schema.
*
* @since 5.4.0
*
* @var array $test_schema
*/
protected $test_schema;
/**
* Class constructor.
*
* @since 5.4.0
*
* @param array $test_schema Schema for use in testing.
*/
public function __construct( $test_schema ) {
$this->test_schema = $test_schema;
}
/**
* Provides the test schema.
*
* @since 5.4.0
*
* @return array Test schema.
*/
public function get_test_schema() {
return $this->test_schema;
}
/**
* Get the item's schema, conforming to JSON Schema.
*
* @since 5.4.0
*
* @return array
*/
public function get_item_schema() {
return $this->add_additional_fields_schema( $this->get_test_schema() );
}
}

View File

@ -343,6 +343,39 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
}
/**
* @ticket 48785
*/
public function test_get_public_item_schema_with_properties() {
$schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
// Double-check that the public item schema set in WP_REST_Test_Controller still has properties.
$this->assertArrayHasKey( 'properties', $schema );
// But arg_options should be removed.
$this->assertArrayNotHasKey( 'arg_options', $schema['properties']['someargoptions'] );
}
/**
* @ticket 48785
*/
public function test_get_public_item_schema_no_properties() {
$controller = new WP_REST_Test_Configurable_Controller(
array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'foo',
'type' => 'string',
'description' => 'This is my magical endpoint that just returns a string.',
)
);
// Initial check that the test class is working as expected.
$this->assertArrayNotHasKey( 'properties', $controller->get_public_item_schema() );
// Test that the schema lacking 'properties' is returned as expected.
$this->assertEqualSetsWithIndex( $controller->get_public_item_schema(), $controller->get_test_schema() );
}
public function test_add_additional_fields_to_object_respects_fields_param() {
$controller = new WP_REST_Test_Controller();
$request = new WP_REST_Request( 'GET', '/wp/v2/testroute' );