Make WordPress Core


Ignore:
Timestamp:
11/19/2018 02:09:07 PM (6 years ago)
Author:
danielbachhuber
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.

Fixes #45220.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r43770 r43908  
    33663366    }
    33673367
     3368    /**
     3369     * @ticket 45220
     3370     */
     3371    public function test_get_additional_field_registration_null_schema() {
     3372        register_rest_field( 'post', 'my_custom_int', array(
     3373            'schema'          => null,
     3374            'get_callback'    => array( $this, 'additional_field_get_callback' ),
     3375            'update_callback' => null,
     3376        ) );
     3377        $post_id = $this->factory->post->create();
     3378
     3379        // 'my_custom_int' should appear because ?_fields= isn't set.
     3380        $request  = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3381        $response = $this->server->dispatch( $request );
     3382        $this->assertArrayHasKey( 'my_custom_int', $response->data );
     3383
     3384        // 'my_custom_int' should appear because it's present in ?_fields=.
     3385        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3386        $request->set_param( '_fields', 'title,my_custom_int' );
     3387        $response = $this->server->dispatch( $request );
     3388        $this->assertArrayHasKey( 'my_custom_int', $response->data );
     3389
     3390        // 'my_custom_int' should not appear because it's not present in ?_fields=.
     3391        $request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . $post_id );
     3392        $request->set_param( '_fields', 'title' );
     3393        $response = $this->server->dispatch( $request );
     3394        $this->assertArrayNotHasKey( 'my_custom_int', $response->data );
     3395
     3396        global $wp_rest_additional_fields;
     3397        $wp_rest_additional_fields = array();
     3398    }
     3399
    33683400    public function test_additional_field_update_errors() {
    33693401        $schema = array(
Note: See TracChangeset for help on using the changeset viewer.