Commit ebc66d6f78 for wordpress.org
commit ebc66d6f78349fcdc088f85d033643d130b45cb0
Author: jorgefilipecosta <jorgefilipecosta@git.wordpress.org>
Date: Fri Oct 30 18:11:56 2026 +0000
Abilities API: Encode empty object results as JSON objects.
The run endpoint prepared only associative array results for JSON clients, so an empty array returned for an object output schema, such as the `core/settings-get` result when no setting matches, was encoded as `[]` and failed output validation in JSON clients. Prepare every result, including root results, lists, object and `JsonSerializable` results, so empty objects are encoded as `{}` while arrays stay arrays.
Register the run endpoint with the `data_processing` route option set to `false`, so the `_fields` and `_embed` parameters and response links leave the result unchanged and it follows the ability's output schema whether it is an object, an array, or a scalar value. The REST API client fixture lists the disabled processing for the run endpoint.
Follow-up to [64311], [64592], [64689].
Props jorgefilipecosta, gziolo.
See #66267.
Built from https://develop.svn.wordpress.org/trunk@65072
git-svn-id: http://core.svn.wordpress.org/trunk@64222 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php
index ac3f8eb5c6..f92eafe3f6 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php
@@ -65,6 +65,13 @@ class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller {
'callback' => array( $this, 'execute_ability' ),
'permission_callback' => array( $this, 'check_ability_permissions' ),
'args' => $this->get_run_args(),
+
+ /*
+ * Serve the ability result as returned, so it follows the ability's output
+ * schema whether it is an object, an array, or a scalar value: `_fields`,
+ * `_embed` and response links leave it unchanged.
+ */
+ 'data_processing' => false,
),
'schema' => array( $this, 'get_run_schema' ),
)
@@ -96,18 +103,10 @@ class WP_REST_Abilities_V1_Run_Controller extends WP_REST_Controller {
}
/*
- * Preserve empty objects in nested properties when encoding an associative
- * result as JSON. Preparation can convert empty PHP arrays to stdClass.
- *
- * The REST server's `_fields` and `_embed` processing expects arrays at the
- * response root and for numeric collection items. Converting those values
- * to objects would cause type errors, so skip root empty arrays and numeric
- * collections until that processing supports objects. An empty object result
- * therefore still encodes as [], and a list containing one encodes as [[]].
+ * Preserve empty objects when encoding the result as JSON. Preparation can
+ * convert empty PHP arrays to stdClass, also within object results.
*/
- if ( is_array( $result ) && ! wp_is_numeric_array( $result ) ) {
- $result = wp_prepare_json_value_for_client( $result, $ability->get_output_schema() );
- }
+ $result = wp_prepare_json_value_for_client( $result, $ability->get_output_schema() );
return rest_ensure_response( $result );
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index c871f5a963..d69fef55cb 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-beta3-65071';
+$wp_version = '7.2-beta3-65072';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.