Fix: Core abilities invalid schemas (has examples and returns empty array intested of object).
Theis commit fixes two issues with the core abilities schemas we have:
- They have examples on the schema with is not complient with the version draft-04 of schema JSON we are using.
- The top level defaults are defined as an empty array and they are of type object, but a php empty array gets JSON serialized and returned in the rest API as [] instead of {}, causing problems on the client validation.
Developed in #10510.
Props jorgefilipecosta, gziolo.
Fixes #64252.
Built from https://develop.svn.wordpress.org/trunk@61244
git-svn-id: http://core.svn.wordpress.org/trunk@60556 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -220,7 +220,6 @@ function wp_register_core_abilities(): void {
|
||||
'db_server_info' => array(
|
||||
'type' => 'string',
|
||||
'description' => __( 'The database server vendor and version string reported by the driver.' ),
|
||||
'examples' => array( '8.0.34', '10.11.6-MariaDB' ),
|
||||
),
|
||||
'wp_version' => array(
|
||||
'type' => 'string',
|
||||
|
||||
@@ -194,6 +194,27 @@ class WP_REST_Abilities_V1_List_Controller extends WP_REST_Controller {
|
||||
return current_user_can( 'read' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes schema empty object defaults.
|
||||
*
|
||||
* Converts empty array defaults to objects when the schema type is 'object'
|
||||
* to ensure proper JSON serialization as {} instead of [].
|
||||
*
|
||||
* @since 6.9.0
|
||||
*
|
||||
* @param array<string, mixed> $schema The schema array.
|
||||
* @return array<string, mixed> The normalized schema.
|
||||
*/
|
||||
private function normalize_schema_empty_object_defaults( array $schema ): array {
|
||||
if ( isset( $schema['type'] ) && 'object' === $schema['type'] && isset( $schema['default'] ) ) {
|
||||
$default = $schema['default'];
|
||||
if ( is_array( $default ) && empty( $default ) ) {
|
||||
$schema['default'] = (object) $default;
|
||||
}
|
||||
}
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares an ability for response.
|
||||
*
|
||||
@@ -209,8 +230,8 @@ class WP_REST_Abilities_V1_List_Controller extends WP_REST_Controller {
|
||||
'label' => $ability->get_label(),
|
||||
'description' => $ability->get_description(),
|
||||
'category' => $ability->get_category(),
|
||||
'input_schema' => $ability->get_input_schema(),
|
||||
'output_schema' => $ability->get_output_schema(),
|
||||
'input_schema' => $this->normalize_schema_empty_object_defaults( $ability->get_input_schema() ),
|
||||
'output_schema' => $this->normalize_schema_empty_object_defaults( $ability->get_output_schema() ),
|
||||
'meta' => $ability->get_meta(),
|
||||
);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '7.0-alpha-61243';
|
||||
$wp_version = '7.0-alpha-61244';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
||||
Reference in New Issue
Block a user