Make WordPress Core

Changeset 48555


Ignore:
Timestamp:
07/21/2020 09:20:22 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Optimize rest_filter_response_by_context performance.

In [47758] a new function rest_filter_response_by_context was introduced to expand the JSON schema features supported by the context filtering mechanism.

This commit improves the performance of that function by eliminating repetitive comparisons and loops. Additionally, it improves multi-type support for object + array types.

Fixes #50700.
Props dlh.

Location:
trunk
Files:
2 edited

Legend:

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

    r48526 r48555  
    20532053    }
    20542054
     2055    $is_array_type  = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );
     2056    $is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) );
     2057
     2058    if ( $is_array_type && $is_object_type ) {
     2059        if ( rest_is_array( $data ) ) {
     2060            $is_object_type = false;
     2061        } else {
     2062            $is_array_type = false;
     2063        }
     2064    }
     2065
     2066    $has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] );
     2067
    20552068    foreach ( $data as $key => $value ) {
    20562069        $check = array();
    20572070
    2058         if ( 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) ) ) {
     2071        if ( $is_array_type ) {
    20592072            $check = isset( $schema['items'] ) ? $schema['items'] : array();
    2060         } elseif ( 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) ) ) {
     2073        } elseif ( $is_object_type ) {
    20612074            if ( isset( $schema['properties'][ $key ] ) ) {
    20622075                $check = $schema['properties'][ $key ];
    2063             } elseif ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
     2076            } elseif ( $has_additional_properties ) {
    20642077                $check = $schema['additionalProperties'];
    20652078            }
     
    20712084
    20722085        if ( ! in_array( $context, $check['context'], true ) ) {
     2086            if ( $is_array_type ) {
     2087                // All array items share schema, so there's no need to check each one.
     2088                $data = array();
     2089                break;
     2090            }
     2091
    20732092            if ( is_object( $data ) ) {
    20742093                unset( $data->$key );
  • trunk/tests/phpunit/tests/rest-api.php

    r48526 r48555  
    10861086    public function _dp_rest_filter_response_by_context() {
    10871087        return array(
    1088             'default'                => array(
     1088            'default'                             => array(
    10891089                array(
    10901090                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    11071107                array( 'first' => 'a' ),
    11081108            ),
    1109             'keeps missing context'  => array(
     1109            'keeps missing context'               => array(
    11101110                array(
    11111111                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    11301130                ),
    11311131            ),
    1132             'removes empty context'  => array(
     1132            'removes empty context'               => array(
    11331133                array(
    11341134                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    11511151                array( 'first' => 'a' ),
    11521152            ),
    1153             'nested properties'      => array(
     1153            'nested properties'                   => array(
    11541154                array(
    11551155                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    11801180                array( 'parent' => array( 'child' => 'hi' ) ),
    11811181            ),
    1182             'grand child properties' => array(
     1182            'grand child properties'              => array(
    11831183                array(
    11841184                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    12161216                array( 'parent' => array( 'child' => array( 'grand' => 'hi' ) ) ),
    12171217            ),
    1218             'array'                  => array(
     1218            'array'                               => array(
    12191219                array(
    12201220                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    12511251                array( 'arr' => array( array( 'visible' => 'hi' ) ) ),
    12521252            ),
    1253             'additional properties'  => array(
     1253            'additional properties'               => array(
    12541254                array(
    12551255                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    12851285                array( 'additional' => array( 'a' => '1' ) ),
    12861286            ),
    1287             'multiple types object'  => array(
     1287            'multiple types object'               => array(
    12881288                array(
    12891289                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    13141314                array( 'multi' => array( 'a' => '1' ) ),
    13151315            ),
    1316             'multiple types array'   => array(
     1316            'multiple types array'                => array(
    13171317                array(
    13181318                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    13491349                array( 'multi' => array( array( 'visible' => '1' ) ) ),
    13501350            ),
    1351             'grand child properties does not traverses missing context' => array(
     1351            'does not traverse missing context'  => array(
    13521352                array(
    13531353                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     
    13921392                ),
    13931393            ),
     1394            'object with no matching properties'  => array(
     1395                array(
     1396                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1397                    'type'       => 'object',
     1398                    'properties' => array(
     1399                        'a' => array(
     1400                            'type'    => 'string',
     1401                            'context' => array( 'edit' ),
     1402                        ),
     1403                        'b' => array(
     1404                            'type'    => 'string',
     1405                            'context' => array( 'edit' ),
     1406                        ),
     1407                    ),
     1408                ),
     1409                array(
     1410                    'a' => 'hi',
     1411                    'b' => 'hello',
     1412                ),
     1413                array(),
     1414            ),
     1415            'array whose type does not match'     => array(
     1416                array(
     1417                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1418                    'type'       => 'object',
     1419                    'properties' => array(
     1420                        'arr' => array(
     1421                            'type'    => 'array',
     1422                            'context' => array( 'view' ),
     1423                            'items'   => array(
     1424                                'type'    => 'string',
     1425                                'context' => array( 'edit' ),
     1426                            ),
     1427                        ),
     1428                    ),
     1429                ),
     1430                array(
     1431                    'arr' => array( 'foo', 'bar', 'baz' ),
     1432                ),
     1433                array( 'arr' => array() ),
     1434            ),
     1435            'array and object type passed object' => array(
     1436                array(
     1437                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1438                    'type'       => array( 'array', 'object' ),
     1439                    'properties' => array(
     1440                        'a' => array(
     1441                            'type'    => 'string',
     1442                            'context' => array( 'view' ),
     1443                        ),
     1444                        'b' => array(
     1445                            'type'    => 'string',
     1446                            'context' => array( 'view' ),
     1447                        ),
     1448                    ),
     1449                    'items'      => array(
     1450                        'type'       => 'object',
     1451                        'context'    => array( 'edit' ),
     1452                        'properties' => array(
     1453                            'a' => array(
     1454                                'type'    => 'string',
     1455                                'context' => array( 'view' ),
     1456                            ),
     1457                            'b' => array(
     1458                                'type'    => 'string',
     1459                                'context' => array( 'view' ),
     1460                            ),
     1461                        ),
     1462                    ),
     1463                ),
     1464                array(
     1465                    'a' => 'foo',
     1466                    'b' => 'bar',
     1467                ),
     1468                array(
     1469                    'a' => 'foo',
     1470                    'b' => 'bar',
     1471                ),
     1472            ),
     1473            'array and object type passed array'  => array(
     1474                array(
     1475                    '$schema'    => 'http://json-schema.org/draft-04/schema#',
     1476                    'type'       => array( 'array', 'object' ),
     1477                    'properties' => array(
     1478                        'a' => array(
     1479                            'type'    => 'string',
     1480                            'context' => array( 'view' ),
     1481                        ),
     1482                        'b' => array(
     1483                            'type'    => 'string',
     1484                            'context' => array( 'view' ),
     1485                        ),
     1486                    ),
     1487                    'items'      => array(
     1488                        'type'       => 'object',
     1489                        'context'    => array( 'edit' ),
     1490                        'properties' => array(
     1491                            'a' => array(
     1492                                'type'    => 'string',
     1493                                'context' => array( 'view' ),
     1494                            ),
     1495                            'b' => array(
     1496                                'type'    => 'string',
     1497                                'context' => array( 'view' ),
     1498                            ),
     1499                        ),
     1500                    ),
     1501                ),
     1502                array(
     1503                    array(
     1504                        'a' => 'foo',
     1505                        'b' => 'bar',
     1506                    ),
     1507                    array(
     1508                        'a' => 'foo',
     1509                        'b' => 'bar',
     1510                    ),
     1511                ),
     1512                array(),
     1513            ),
    13941514        );
    13951515    }
Note: See TracChangeset for help on using the changeset viewer.