Abilities API: Normalize required schema shape for REST responses#12013
Abilities API: Normalize required schema shape for REST responses#12013gziolo wants to merge 1 commit into
required schema shape for REST responses#12013Conversation
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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>
6265213 to
03946ca
Compare
Summary
Normalizes the
requiredkeyword 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
requiredarray of property names on the object schema, or a draft-03 per-propertyrequiredboolean (seerest_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 thewp_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():required: truebooleans and emit them as arequired: [ ... ]array of property names on the parent object schema — applied recursively, so nested objects each get their own array.required: falseand any booleanrequiredthat has no draft-04 equivalent (for example on a scalar root schema).requiredarray, 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 arrayitems,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