From 7fe78e2f18b47fb4540a4b7f790157affad27750 Mon Sep 17 00:00:00 2001 From: TimothyBlynJacobs Date: Tue, 20 Oct 2020 20:19:09 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@49257 git-svn-id: http://core.svn.wordpress.org/trunk@49019 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 67 +++++++++++-------- wp-includes/rest-api/class-wp-rest-server.php | 23 ++----- wp-includes/version.php | 2 +- 3 files changed, 45 insertions(+), 47 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index 099fa9a64e..1c507e0524 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -1874,6 +1874,43 @@ function rest_find_one_matching_schema( $value, $args, $param, $stop_after_first return $matching_schemas[0]['schema_object']; } +/** + * Get all valid JSON schema properties. + * + * @since 5.6.0 + * + * @return string[] All valid JSON schema properties. + */ +function rest_get_allowed_schema_keywords() { + return array( + 'title', + 'description', + 'default', + 'type', + 'format', + 'enum', + 'items', + 'properties', + 'additionalProperties', + 'patternProperties', + 'minProperties', + 'maxProperties', + 'minimum', + 'maximum', + 'exclusiveMinimum', + 'exclusiveMaximum', + 'multipleOf', + 'minLength', + 'maxLength', + 'pattern', + 'minItems', + 'maxItems', + 'uniqueItems', + 'anyOf', + 'oneOf', + ); +} + /** * Validate a value based on a schema. * @@ -2765,30 +2802,8 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C $schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array(); $endpoint_args = array(); - $valid_schema_properties = array( - 'type', - 'format', - 'enum', - 'items', - 'properties', - 'additionalProperties', - 'patternProperties', - 'minProperties', - 'maxProperties', - 'minimum', - 'maximum', - 'exclusiveMinimum', - 'exclusiveMaximum', - 'multipleOf', - 'minLength', - 'maxLength', - 'pattern', - 'minItems', - 'maxItems', - 'uniqueItems', - 'anyOf', - 'oneOf', - ); + $valid_schema_properties = rest_get_allowed_schema_keywords(); + $valid_schema_properties = array_diff( $valid_schema_properties, array( 'default', 'required' ) ); foreach ( $schema_properties as $field_id => $params ) { @@ -2802,10 +2817,6 @@ function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::C 'sanitize_callback' => 'rest_sanitize_request_arg', ); - if ( isset( $params['description'] ) ) { - $endpoint_args[ $field_id ]['description'] = $params['description']; - } - if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) { $endpoint_args[ $field_id ]['default'] = $params['default']; } diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index 0844819568..3eeefda167 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -1380,6 +1380,8 @@ class WP_REST_Server { } } + $allowed_schema_keywords = array_flip( rest_get_allowed_schema_keywords() ); + $route = preg_replace( '#\(\?P<(\w+?)>.*?\)#', '{$1}', $route ); foreach ( $callbacks as $callback ) { @@ -1397,24 +1399,9 @@ class WP_REST_Server { $endpoint_data['args'] = array(); foreach ( $callback['args'] as $key => $opts ) { - $arg_data = array( - 'required' => ! empty( $opts['required'] ), - ); - if ( isset( $opts['default'] ) ) { - $arg_data['default'] = $opts['default']; - } - if ( isset( $opts['enum'] ) ) { - $arg_data['enum'] = $opts['enum']; - } - if ( isset( $opts['description'] ) ) { - $arg_data['description'] = $opts['description']; - } - if ( isset( $opts['type'] ) ) { - $arg_data['type'] = $opts['type']; - } - if ( isset( $opts['items'] ) ) { - $arg_data['items'] = $opts['items']; - } + $arg_data = array_intersect_key( $opts, $allowed_schema_keywords ); + $arg_data['required'] = ! empty( $opts['required'] ); + $endpoint_data['args'][ $key ] = $arg_data; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 0d26f052ad..f93a25e5fb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49256'; +$wp_version = '5.6-alpha-49257'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.