Skip to content

Abilities API: Normalize required schema shape for REST responses#12013

Open
gziolo wants to merge 1 commit into
WordPress:trunkfrom
gziolo:64955-abilities-rest-required-normalization
Open

Abilities API: Normalize required schema shape for REST responses#12013
gziolo wants to merge 1 commit into
WordPress:trunkfrom
gziolo:64955-abilities-rest-required-normalization

Conversation

@gziolo
Copy link
Copy Markdown
Member

@gziolo gziolo commented May 29, 2026

Summary

Normalizes the required keyword in an ability's input and output schemas to its JSON Schema draft-04 form when they are exposed through the REST API (wp-abilities/v1).

Background

WordPress validates objects against either a draft-04 required array of property names on the object schema, or a draft-03 per-property required boolean (see rest_validate_value_from_schema()). Both work internally, but only the array form is valid JSON Schema draft-04. Ability schemas registered with the per-property boolean form (as shown in the wp_register_ability() examples) were therefore exposed to clients in a shape that does not conform to the draft-04 contract the endpoint advertises via $schema.

Changes

In WP_REST_Abilities_V1_List_Controller::prepare_schema_for_response():

  • Collect per-property required: true booleans and emit them as a required: [ ... ] array of property names on the parent object schema — applied recursively, so nested objects each get their own array.
  • Strip required: false and any boolean required that has no draft-04 equivalent (for example on a scalar root schema).
  • Preserve and merge with an existing draft-04 required array, de-duplicating names.

The transformation only rewrites the REST response copy; server-side validation continues to use the stored schema via validate_input(), so behavior is unchanged. Adds test coverage for the conversion: top-level, nested objects, objects nested inside array items, required: false, mixed draft-04 array + draft-03 booleans, and the scalar-root case.

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

🤖 Generated with Claude Code

@github-actions
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 gziolo.

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

@github-actions
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.

When exposing an ability's input and output schemas through the REST API, convert any draft-03 per-property `required` boolean into a draft-04 `required` array of property names on the parent object schema, and drop boolean `required` flags that have no draft-04 equivalent (such as on a scalar root schema).

WordPress accepts both forms internally, but only the array form is valid JSON Schema draft-04, so clients consuming `wp-abilities/v1` now receive a conformant schema. The transformation only affects the REST response copy; server-side validation continues to use the stored schema via validate_input().

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@gziolo gziolo force-pushed the 64955-abilities-rest-required-normalization branch from 6265213 to 03946ca Compare May 29, 2026 17:04
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.

1 participant