Commit d53f69c59e7 for woocommerce
commit d53f69c59e79214e651232ef9e903ed79c3e1126
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date: Thu Nov 19 11:01:22 2026 -0300
Send empty objects as {} under anyOf, oneOf and allOf branches in extension field output (#71087)
* Send empty objects as {} under an anyOf or oneOf in extension field output
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Use the nested schemas of anyOf, oneOf and allOf branches for empty objects
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Test the outer child schemas and the empty object that fits an array branch
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Document the branch limits of the empty object output
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Pin that an empty list under an object-first anyOf goes out as {}
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/fix-extensible-abilities-any-of-empty-object b/plugins/woocommerce/changelog/fix-extensible-abilities-any-of-empty-object
new file mode 100644
index 00000000000..951ada0e8a7
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-extensible-abilities-any-of-empty-object
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Send empty objects as {} under anyOf, oneOf and allOf branches in experimental extensible ability fields; not stable yet.
diff --git a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
index 72e895ab2a5..d6232c65819 100644
--- a/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
+++ b/plugins/woocommerce/src/Abilities/ExtensibleAbilities.php
@@ -1019,12 +1019,40 @@ class ExtensibleAbilities {
* Send an empty array for an `object` as an empty object, at any depth.
* rest_sanitize_value_from_schema() turns every object into an array, so a
* callback cannot send `{}` by itself. A part with no schema stays as it is.
+ * The nested schemas of every `allOf` branch, and of the `anyOf` and `oneOf`
+ * branch that the sanitized value matches, apply too, after the schema's own.
+ * A value that matches no branch stays as it is. An empty array fits both an
+ * `object` and an `array` branch, so it takes the first `anyOf` branch, and
+ * under a `oneOf` it stays as it is. Only one level of `allOf`, `anyOf` and
+ * `oneOf` branches is read, so a combinator inside a branch is not followed.
+ * When more than one branch declares the same property, the first one is used.
*
* @param mixed $value Sanitized value.
* @param array<string, mixed> $schema Schema of the value.
* @return mixed The value for the JSON output.
*/
private static function prepare_json_value( $value, array $schema ) {
+ $branches = (array) ( $schema['allOf'] ?? array() );
+ $finders = array(
+ 'anyOf' => 'rest_find_any_matching_schema',
+ 'oneOf' => 'rest_find_one_matching_schema',
+ );
+ foreach ( $finders as $keyword => $find_branch ) {
+ if ( isset( $schema[ $keyword ] ) ) {
+ $branch = $find_branch( $value, $schema, '' );
+ if ( is_wp_error( $branch ) ) {
+ return $value;
+ }
+ $branches[] = $branch;
+ }
+ }
+ foreach ( array_filter( $branches, 'is_array' ) as $branch ) {
+ $schema['type'] = $schema['type'] ?? $branch['type'] ?? null;
+ $schema['properties'] = (array) ( $schema['properties'] ?? array() ) + (array) ( $branch['properties'] ?? array() );
+ $schema['patternProperties'] = (array) ( $schema['patternProperties'] ?? array() ) + (array) ( $branch['patternProperties'] ?? array() );
+ $schema['items'] = $schema['items'] ?? $branch['items'] ?? null;
+ $schema['additionalProperties'] = $schema['additionalProperties'] ?? $branch['additionalProperties'] ?? null;
+ }
$type = rest_get_best_type_for_value( $value, (array) ( $schema['type'] ?? array() ) );
if ( 'array' === $type ) {
$children = array_fill_keys( array_keys( $value ), $schema['items'] ?? null );
diff --git a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
index 0c1f5e9baed..fcc28d743ec 100644
--- a/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/AbilitiesApi/ExtensibleAbilitiesTest.php
@@ -2050,6 +2050,159 @@ class ExtensibleAbilitiesTest extends \WC_REST_Unit_Test_Case {
$this->assertSame( '{"flat":[],"of_objects":[],"in_object":{"list":[]}}', wp_json_encode( $output ) );
}
+ /**
+ * @testdox Should send an empty array for an object as an empty object under an anyOf or a oneOf with a null branch.
+ */
+ public function test_an_empty_object_under_an_any_of_or_one_of_is_an_empty_object(): void {
+ $object_or_null = fn( string $keyword ) => array( $keyword => array( array( 'type' => 'object' ), array( 'type' => 'null' ) ) );
+ $output = $this->read_field_values(
+ array(
+ 'any_of_items' => array(
+ array(
+ 'type' => 'array',
+ 'items' => $object_or_null( 'anyOf' ),
+ ),
+ array( array(), null ),
+ ),
+ 'one_of_items' => array(
+ array(
+ 'type' => 'array',
+ 'items' => $object_or_null( 'oneOf' ),
+ ),
+ array( array(), null ),
+ ),
+ 'any_of_property' => array(
+ array(
+ 'type' => 'object',
+ 'properties' => array(
+ 'empty' => $object_or_null( 'anyOf' ),
+ 'null' => $object_or_null( 'anyOf' ),
+ ),
+ ),
+ array(
+ 'empty' => array(),
+ 'null' => null,
+ ),
+ ),
+ 'any_of_deep' => array(
+ array(
+ 'type' => 'array',
+ 'items' => array(
+ 'anyOf' => array(
+ array(
+ 'type' => 'object',
+ 'properties' => array( 'inner' => array( 'type' => 'object' ) ),
+ ),
+ array( 'type' => 'null' ),
+ ),
+ ),
+ ),
+ array( array( 'inner' => array() ) ),
+ ),
+ )
+ );
+
+ $this->assertSame(
+ '{"any_of_items":[{},null],"one_of_items":[{},null],"any_of_property":{"empty":{},"null":null},"any_of_deep":[{"inner":{}}]}',
+ wp_json_encode( $output )
+ );
+ }
+
+ /**
+ * @testdox Should send an empty array for an object as an empty object when the nested schema is in an anyOf, oneOf or allOf branch, or under patternProperties or additionalProperties.
+ */
+ public function test_an_empty_object_in_a_branch_or_a_pattern_is_an_empty_object(): void {
+ $object_or_null = fn( string $keyword ) => array( $keyword => array( array( 'type' => 'object' ), array( 'type' => 'null' ) ) );
+ $property = fn( string $name ) => array( 'properties' => array( $name => array( 'type' => 'object' ) ) );
+ $output = $this->read_field_values(
+ array(
+ 'any_of_branch' => array(
+ array(
+ 'type' => 'object',
+ 'anyOf' => array( $property( 'a' ), $property( 'b' ) ),
+ ),
+ array( 'a' => array() ),
+ ),
+ 'one_of_branch' => array(
+ array(
+ 'type' => 'object',
+ 'oneOf' => array( $property( 'a' ) + array( 'required' => array( 'a' ) ), $property( 'b' ) + array( 'required' => array( 'b' ) ) ),
+ ),
+ array( 'a' => array() ),
+ ),
+ 'one_of_items' => array(
+ array(
+ 'type' => 'array',
+ 'oneOf' => array( array( 'items' => array( 'type' => 'object' ) ), array( 'items' => array( 'type' => 'string' ) ) ),
+ ),
+ array( array() ),
+ ),
+ 'all_of_branch' => array(
+ array(
+ 'type' => 'object',
+ 'allOf' => array( $property( 'a' ) ),
+ ),
+ array( 'a' => array() ),
+ ),
+ 'pattern' => array(
+ array(
+ 'type' => 'object',
+ 'patternProperties' => array( '^x' => $object_or_null( 'anyOf' ) ),
+ ),
+ array( 'x1' => array() ),
+ ),
+ 'additional' => array(
+ array(
+ 'type' => 'object',
+ 'additionalProperties' => $object_or_null( 'oneOf' ),
+ ),
+ array( 'k' => array() ),
+ ),
+ 'top_properties' => array(
+ array(
+ 'type' => 'object',
+ 'anyOf' => array( array( 'required' => array( 'a' ) ) ),
+ ) + $property( 'a' ),
+ array( 'a' => array() ),
+ ),
+ 'untyped_outer' => array(
+ array(
+ 'type' => 'array',
+ 'items' => $object_or_null( 'anyOf' ) + $property( 'inner' ),
+ ),
+ array( array( 'inner' => array() ) ),
+ ),
+ )
+ );
+
+ $this->assertSame(
+ '{"any_of_branch":{"a":{}},"one_of_branch":{"a":{}},"one_of_items":[{}],"all_of_branch":{"a":{}},"pattern":{"x1":{}},"additional":{"k":{}},"top_properties":{"a":{}},"untyped_outer":[{"inner":{}}]}',
+ wp_json_encode( $output )
+ );
+ }
+
+ /**
+ * @testdox Should match the sanitized value, so an empty array, whether meant as an object or a list, takes the first anyOf branch and stays as it is under a oneOf.
+ */
+ public function test_an_empty_object_that_also_fits_an_array_branch_follows_the_sanitized_value(): void {
+ $items = fn( string $keyword, array $branches ) => array(
+ 'type' => 'array',
+ 'items' => array( $keyword => $branches ),
+ );
+ $object = array( 'type' => 'object' );
+ $list = array( 'type' => 'array' );
+ $output = $this->read_field_values(
+ array(
+ 'one_of' => array( $items( 'oneOf', array( $object, $list ) ), array( new \stdClass() ) ),
+ 'any_of_object_first' => array( $items( 'anyOf', array( $object, $list ) ), array( new \stdClass() ) ),
+ 'any_of_object_first_list' => array( $items( 'anyOf', array( $object, $list ) ), array( array() ) ),
+ 'any_of_array_first' => array( $items( 'anyOf', array( $list, $object ) ), array( new \stdClass() ) ),
+ )
+ );
+
+ $this->assertSame( '{"one_of":[[]],"any_of_object_first":[{}],"any_of_object_first_list":[{}],"any_of_array_first":[[]]}', wp_json_encode( $output ) );
+ }
+
/**
* @testdox Should return product amounts as strings when the getters return an int or a float, and save the write.
*/