Skip to content

Commit 2b0423d

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@61244 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 706307a commit 2b0423d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/wp-includes/abilities.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ function wp_register_core_abilities(): void {
220220
'db_server_info' => array(
221221
'type' => 'string',
222222
'description' => __( 'The database server vendor and version string reported by the driver.' ),
223-
'examples' => array( '8.0.34', '10.11.6-MariaDB' ),
224223
),
225224
'wp_version' => array(
226225
'type' => 'string',

src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-list-controller.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,27 @@ public function get_item_permissions_check( $request ) {
194194
return current_user_can( 'read' );
195195
}
196196

197+
/**
198+
* Normalizes schema empty object defaults.
199+
*
200+
* Converts empty array defaults to objects when the schema type is 'object'
201+
* to ensure proper JSON serialization as {} instead of [].
202+
*
203+
* @since 6.9.0
204+
*
205+
* @param array<string, mixed> $schema The schema array.
206+
* @return array<string, mixed> The normalized schema.
207+
*/
208+
private function normalize_schema_empty_object_defaults( array $schema ): array {
209+
if ( isset( $schema['type'] ) && 'object' === $schema['type'] && isset( $schema['default'] ) ) {
210+
$default = $schema['default'];
211+
if ( is_array( $default ) && empty( $default ) ) {
212+
$schema['default'] = (object) $default;
213+
}
214+
}
215+
return $schema;
216+
}
217+
197218
/**
198219
* Prepares an ability for response.
199220
*
@@ -209,8 +230,8 @@ public function prepare_item_for_response( $ability, $request ) {
209230
'label' => $ability->get_label(),
210231
'description' => $ability->get_description(),
211232
'category' => $ability->get_category(),
212-
'input_schema' => $ability->get_input_schema(),
213-
'output_schema' => $ability->get_output_schema(),
233+
'input_schema' => $this->normalize_schema_empty_object_defaults( $ability->get_input_schema() ),
234+
'output_schema' => $this->normalize_schema_empty_object_defaults( $ability->get_output_schema() ),
214235
'meta' => $ability->get_meta(),
215236
);
216237

0 commit comments

Comments
 (0)