Make WordPress Core

Changeset 44254


Ignore:
Timestamp:
12/17/2018 01:14:17 AM (6 years ago)
Author:
SergeyBiryukov
Message:

REST API: Include fields with null schema in get_fields_for_response().

In [43736], we prevented rendering fields when not present in ?_fields=. However, because get_fields_for_response() is dependent on get_item_schema(), any custom fields registered with a null schema would be incorrectly excluded from the response. Because the REST API permits a null schema for register_rest_field(), those fields should be included in the available fields for a response.

Props danielbachhuber.
Merges [43908] to trunk.
Fixes #45220.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk

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

    r44173 r44254  
    520520        $schema = $this->get_item_schema();
    521521        $fields = isset( $schema['properties'] ) ? array_keys( $schema['properties'] ) : array();
     522
     523        $additional_fields = $this->get_additional_fields();
     524        foreach ( $additional_fields as $field_name => $field_options ) {
     525            // For back-compat, include any field with an empty schema
     526            // because it won't be present in $this->get_item_schema().
     527            if ( is_null( $field_options['schema'] ) ) {
     528                $fields[] = $field_name;
     529            }
     530        }
     531
    522532        if ( ! isset( $request['_fields'] ) ) {
    523533            return $fields;
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r44127 r44254  
    37623762    }
    37633763
     3764    /**
     3765     * @ticket 45220
     3766     */
     3767    public function test_get_additional_field_registration_null_schema() {
     3768        register_rest_field(
     3769            'post',
     3770            'my_custom_int',
     3771            array(
     3772                'schema'          => null,
     3773                'get_callback'    => array( $this, 'additional_field_get_callback' ),
     3774                'update_callback' => null,
     3775            )
     3776        );
     3777        $post_id = $this->factory->post->create();
     3778
     3779        // 'my_custom_int' should appear because ?_fields= isn't set.
     3780        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3781        $response = $this->server->dispatch( $request );
     3782        $this->assertArrayHasKey( 'my_custom_int', $response->data );
     3783
     3784        // 'my_custom_int' should appear because it's present in ?_fields=.
     3785        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3786        $request->set_param( '_fields', 'title,my_custom_int' );
     3787        $response = $this->server->dispatch( $request );
     3788        $this->assertArrayHasKey( 'my_custom_int', $response->data );
     3789
     3790        // 'my_custom_int' should not appear because it's not present in ?_fields=.
     3791        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3792        $request->set_param( '_fields', 'title' );
     3793        $response = $this->server->dispatch( $request );
     3794        $this->assertArrayNotHasKey( 'my_custom_int', $response->data );
     3795
     3796        global $wp_rest_additional_fields;
     3797        $wp_rest_additional_fields = array();
     3798    }
     3799
    37643800    public function test_additional_field_update_errors() {
    37653801        $schema = array(
Note: See TracChangeset for help on using the changeset viewer.