Skip to content

REST API: Allow routes to disable _fields, _embed and response links - #14671

Open
jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/rest-route-response-processing-options
Open

jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/rest-route-response-processing-options

Conversation

@jorgefilipecosta

Copy link
Copy Markdown
Member

Follow up to #14547 (comment).

This PR adds three route and handler options that let a route opt out of the global response processing:

  • allow_fields: when false, the _fields parameter does not filter the response data.
  • allow_embed: when false, the _embed parameter does not embed linked resources. The links are still added as _links.
  • allow_links: when false, the response links are not added to the response data as _links.

All three default to true. A handler option takes precedence over the route option of the same name, as with allow_batch. The new WP_REST_Server::is_response_option_enabled() method reads them from the matched route and handler. The options apply to served, enveloped and preloaded requests.

register_rest_route(
	'my-ns/v1',
	'/run',
	array(
		'methods'             => 'GET',
		'callback'            => 'my_callback',
		'permission_callback' => '__return_true',
		'allow_fields'        => false,
		'allow_embed'         => false,
		'allow_links'         => false,
	)
);

This lets a route return its data unchanged, e.g. the abilities run endpoint, whose result follows the ability's output schema and can be an object, a list or a scalar value. #14547 uses these options for that endpoint.

Drafted with AI assistance and reviewed by me.

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

Testing

Verify unit tests are passing:

npm run test:php -- --filter 'Tests_REST_Server' --group 66267

…nks.

Add the `allow_fields`, `allow_embed` and `allow_links` route and
handler options. When an option is false, the `_fields` parameter does
not filter the response data, the `_embed` parameter does not embed
linked resources, or the response links are not added to the response
data as `_links`. A handler option takes precedence over the route
option, as with `allow_batch`.

This lets a route serve its response data as returned, for example data
that follows a schema of its own or that is not an array.

See #66267.
@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 jorgefilipecosta.

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.

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