Skip to content

Abilities API: Prepare empty object results for JSON clients - #14139

Closed
Sukhendu2002 wants to merge 1 commit into
WordPress:trunkfrom
Sukhendu2002:ticket/66267-empty-object-results
Closed

Sukhendu2002 wants to merge 1 commit into
WordPress:trunkfrom
Sukhendu2002:ticket/66267-empty-object-results

Conversation

@Sukhendu2002

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/66267

What

Adds wp_prepare_json_value_for_client() to prepare ability results according to their output schema before sending them to JSON clients.

The helper:

  • Converts an empty PHP array to an object when the schema allows object but not array.
  • Leaves the value as [] when both object and array are allowed.
  • Recursively prepares properties, patternProperties, additionalProperties, and array items.
  • Supports arrays, stdClass, and JsonSerializable values.

Why

An ability can return an empty PHP array for an output schema with type: object. Server-side validation accepts that value, but JSON encoding produces [] instead of {}.

This violates the published output schema and can be rejected by strict clients or AI providers expecting an object. When both object and array are allowed, the empty array remains [] because its intended JSON type is ambiguous.

Testing

   npm run test:php -- --filter 'Tests_JSON_Schema|Tests_REST_API_WpRestAbilitiesV1RunController|Tests_AI_Client_AbilityFunctionResolver|Tests_AI_Client_PromptBuilder'

Use of AI Tools

AI assistance: Yes
Tool(s): Pi agent
Model(s): GPT-5.6 Sol
Used for: Initial code skeleton, Test drafting


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@Sukhendu2002
Sukhendu2002 marked this pull request as ready for review October 9, 2026 17:12
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sukhendu2002, gziolo.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment thread src/wp-includes/json-schema.php Outdated
@Sukhendu2002
Sukhendu2002 requested a review from gziolo October 13, 2026 06:43
Comment thread src/wp-includes/rest-api/endpoints/class-wp-rest-abilities-v1-run-controller.php Outdated

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything else looks good to me. The schema handling and test coverage look solid. My only concern is the interaction with REST response processing noted inline. I’d be happy to address that in stages and move forward with the cases we can safely support now.

Prepare empty objects and nested values using their output schemas, reusing
REST schema matching and validation helpers. Prefer array branches only
when the value satisfies their array constraints, including nested branches
and explicit or inferred types.

Prepare complete AI client results and nested properties of associative
REST results. Keep REST root empty arrays and numeric collections unchanged
for compatibility with the server's _fields and _embed processing.

Add focused schema regression tests and coverage for both client adapters.

Validation: 774 tests and 1,474 assertions, with two existing PHPUnit
warnings; coding standards and PHP compatibility checks pass.

Co-authored-by: Grzegorz Ziolkowski <grzegorz@gziolo.pl>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@gziolo
gziolo force-pushed the ticket/66267-empty-object-results branch from 5b0ae36 to 0d92137 Compare October 20, 2026 08:54

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Sukhendu2002! My feedback is addressed. With 7.2 Beta 1 today, I had to act fast and pushed the changes directly to this branch. I hope that's OK.

The main change: the REST run controller now prepares only associative results. Root empty results and numeric collections stay as they are for now, so _fields and _embed keep working. We can cover those in a follow-up.

Looks good to go! 🚀

@gziolo

gziolo commented Oct 20, 2026

Copy link
Copy Markdown
Member

Committed with https://core.trac.wordpress.org/changeset/64592.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants