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
This commit is contained in:
TimothyBlynJacobs 2020-10-20 20:19:09 +00:00
parent fbd8078d4e
commit 7fe78e2f18
3 changed files with 45 additions and 47 deletions

View File

@ -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'];
}

View File

@ -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;
}
}

View File

@ -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.