WordPress.org

Make WordPress Core

Changeset 47328


Ignore:
Timestamp:
02/20/2020 04:56:17 PM (8 months ago)
Author:
TimothyBlynJacobs
Message:

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.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r47122 r47328  
    345345        $schema = $this->get_item_schema();
    346346
    347         foreach ( $schema['properties'] as &$property ) {
    348             unset( $property['arg_options'] );
     347        if ( ! empty( $schema['properties'] ) ) {
     348            foreach ( $schema['properties'] as &$property ) {
     349                unset( $property['arg_options'] );
     350            }
    349351        }
    350352
  • trunk/tests/phpunit/includes/bootstrap.php

    r47198 r47328  
    155155require __DIR__ . '/spy-rest-server.php';
    156156require __DIR__ . '/class-wp-rest-test-search-handler.php';
     157require __DIR__ . '/class-wp-rest-test-configurable-controller.php';
    157158require __DIR__ . '/class-wp-fake-block-type.php';
    158159
  • trunk/tests/phpunit/tests/rest-api/rest-controller.php

    r46586 r47328  
    344344    }
    345345
     346    /**
     347     * @ticket 48785
     348     */
     349    public function test_get_public_item_schema_with_properties() {
     350        $schema = ( new WP_REST_Test_Controller() )->get_public_item_schema();
     351
     352        // Double-check that the public item schema set in WP_REST_Test_Controller still has properties.
     353        $this->assertArrayHasKey( 'properties', $schema );
     354
     355        // But arg_options should be removed.
     356        $this->assertArrayNotHasKey( 'arg_options', $schema['properties']['someargoptions'] );
     357    }
     358
     359    /**
     360     * @ticket 48785
     361     */
     362    public function test_get_public_item_schema_no_properties() {
     363        $controller = new WP_REST_Test_Configurable_Controller(
     364            array(
     365                '$schema'     => 'http://json-schema.org/draft-04/schema#',
     366                'title'       => 'foo',
     367                'type'        => 'string',
     368                'description' => 'This is my magical endpoint that just returns a string.',
     369            )
     370        );
     371
     372        // Initial check that the test class is working as expected.
     373        $this->assertArrayNotHasKey( 'properties', $controller->get_public_item_schema() );
     374
     375        // Test that the schema lacking 'properties' is returned as expected.
     376        $this->assertEqualSetsWithIndex( $controller->get_public_item_schema(), $controller->get_test_schema() );
     377    }
     378
    346379    public function test_add_additional_fields_to_object_respects_fields_param() {
    347380        $controller = new WP_REST_Test_Controller();
Note: See TracChangeset for help on using the changeset viewer.