From 29124d3c74ff50f3ad3487ade4030e57e5c31497 Mon Sep 17 00:00:00 2001 From: jorgefilipecosta Date: Thu, 13 Nov 2025 22:27:29 +0000 Subject: [PATCH] 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 --- wp-includes/abilities.php | 1 - ...s-wp-rest-abilities-v1-list-controller.php | 25 +++++++++++++++++-- wp-includes/version.php | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/wp-includes/abilities.php b/wp-includes/abilities.php index c71eeb158a..0320df3b9f 100644 --- a/wp-includes/abilities.php +++ b/wp-includes/abilities.php @@ -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', diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php index 4f59d908de..6dfc540038 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php @@ -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 $schema The schema array. + * @return array 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(), ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 81cb0efe57..dc697dad91 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.