Make WordPress Core


Ignore:
Timestamp:
10/20/2020 08:17:20 PM (3 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Make sure all supported JSON Schema keywords are output in the index.

Previously, only a small subset of keywords were exposed which limited the utility of OPTIONS requests.

Props raubvogel, TimothyBlynJacobs.
Fixes #51020.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r49252 r49257  
    19321932    }
    19331933
     1934    /**
     1935     * @ticket 51020
     1936     */
     1937    public function test_get_data_for_route_includes_permitted_schema_keywords() {
     1938        $keywords = array(
     1939            'title'                => 'Hi',
     1940            'description'          => 'World',
     1941            'type'                 => 'string',
     1942            'default'              => 0,
     1943            'format'               => 'uri',
     1944            'enum'                 => array( 'https://example.org' ),
     1945            'items'                => array( 'type' => 'string' ),
     1946            'properties'           => array( 'a' => array( 'type' => 'string' ) ),
     1947            'additionalProperties' => false,
     1948            'patternProperties'    => array( '\d' => array( 'type' => 'string' ) ),
     1949            'minProperties'        => 1,
     1950            'maxProperties'        => 5,
     1951            'minimum'              => 1,
     1952            'maximum'              => 5,
     1953            'exclusiveMinimum'     => true,
     1954            'exclusiveMaximum'     => false,
     1955            'multipleOf'           => 2,
     1956            'minLength'            => 1,
     1957            'maxLength'            => 5,
     1958            'pattern'              => '\d',
     1959            'minItems'             => 1,
     1960            'maxItems'             => 5,
     1961            'uniqueItems'          => true,
     1962            'anyOf'                => array(
     1963                array( 'type' => 'string' ),
     1964                array( 'type' => 'integer' ),
     1965            ),
     1966            'oneOf'                => array(
     1967                array( 'type' => 'string' ),
     1968                array( 'type' => 'integer' ),
     1969            ),
     1970        );
     1971
     1972        $param            = $keywords;
     1973        $param['invalid'] = true;
     1974
     1975        $expected             = $keywords;
     1976        $expected['required'] = false;
     1977
     1978        register_rest_route(
     1979            'test-ns/v1',
     1980            '/test',
     1981            array(
     1982                'methods'             => 'POST',
     1983                'callback'            => static function () {
     1984                    return new WP_REST_Response( 'test' );
     1985                },
     1986                'permission_callback' => '__return_true',
     1987                'args'                => array(
     1988                    'param' => $param,
     1989                ),
     1990            )
     1991        );
     1992
     1993        $response = rest_do_request( new WP_REST_Request( 'OPTIONS', '/test-ns/v1/test' ) );
     1994        $args     = $response->get_data()['endpoints'][0]['args'];
     1995
     1996        $this->assertSameSetsWithIndex( $expected, $args['param'] );
     1997    }
     1998
    19341999    public function _validate_as_integer_123( $value, $request, $key ) {
    19352000        if ( ! is_int( $value ) ) {
Note: See TracChangeset for help on using the changeset viewer.