Make WordPress Core

Changeset 46456


Ignore:
Timestamp:
10/10/2019 05:16:44 PM (7 years ago)
Author:
kadamwhite
Message:

REST API: Fix error in _fields filtering logic where only one of several requested sibling properties would be included.

Props kadamwhite, TimothyBlynJacobs.
Fixes #48266.

Location:
trunk
Files:
2 edited

Legend:

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

    r46272 r46456  
    744744                $ref   = &$fields_as_keyed;
    745745                while ( count( $parts ) > 1 ) {
    746                         $next         = array_shift( $parts );
    747                         $ref[ $next ] = array();
     746                        $next = array_shift( $parts );
     747                        if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) {
     748                                // Skip any sub-properties if their parent prop is already marked for inclusion.
     749                                break 2;
     750                        }
     751                        $ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array();
    748752                        $ref          = &$ref[ $next ];
    749753                }
  • trunk/tests/phpunit/tests/rest-api.php

    r46219 r46456  
    555555                                'd' => array(
    556556                                        '5' => 5,
     557                                ),
     558                        ),
     559                        $response->get_data()
     560                );
     561        }
     562
     563        /**
     564         * Ensure that specifying a single top-level key in _fields includes that field and all children.
     565         *
     566         * @ticket 48266
     567         */
     568        public function test_rest_filter_response_fields_top_level_key() {
     569                $response = new WP_REST_Response();
     570
     571                $response->set_data(
     572                        array(
     573                                'meta' => array(
     574                                        'key1' => 1,
     575                                        'key2' => 2,
     576                                ),
     577                        )
     578                );
     579                $request = array(
     580                        '_fields' => 'meta',
     581                );
     582
     583                $response = rest_filter_response_fields( $response, null, $request );
     584                $this->assertEquals(
     585                        array(
     586                                'meta' => array(
     587                                        'key1' => 1,
     588                                        'key2' => 2,
     589                                ),
     590                        ),
     591                        $response->get_data()
     592                );
     593        }
     594
     595        /**
     596         * Ensure that a top-level key in _fields supersedes any specified children of that field.
     597         *
     598         * @ticket 48266
     599         */
     600        public function test_rest_filter_response_fields_child_after_parent() {
     601                $response = new WP_REST_Response();
     602
     603                $response->set_data(
     604                        array(
     605                                'meta' => array(
     606                                        'key1' => 1,
     607                                        'key2' => 2,
     608                                ),
     609                        )
     610                );
     611                $request = array(
     612                        '_fields' => 'meta,meta.key1',
     613                );
     614
     615                $response = rest_filter_response_fields( $response, null, $request );
     616                $this->assertEquals(
     617                        array(
     618                                'meta' => array(
     619                                        'key1' => 1,
     620                                        'key2' => 2,
     621                                ),
     622                        ),
     623                        $response->get_data()
     624                );
     625        }
     626
     627        /**
     628         * Ensure that specifying two sibling properties in _fields causes both to be included.
     629         *
     630         * @ticket 48266
     631         */
     632        public function test_rest_filter_response_fields_include_all_specified_siblings() {
     633                $response = new WP_REST_Response();
     634
     635                $response->set_data(
     636                        array(
     637                                'meta' => array(
     638                                        'key1' => 1,
     639                                        'key2' => 2,
     640                                ),
     641                        )
     642                );
     643                $request = array(
     644                        '_fields' => 'meta.key1,meta.key2',
     645                );
     646
     647                $response = rest_filter_response_fields( $response, null, $request );
     648                $this->assertEquals(
     649                        array(
     650                                'meta' => array(
     651                                        'key1' => 1,
     652                                        'key2' => 2,
    557653                                ),
    558654                        ),
Note: See TracChangeset for help on using the changeset viewer.