Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#55213 closed enhancement (fixed)

Respect _fields query arg in preloaded requests

Reported by: jsnajdr's profile jsnajdr Owned by: gziolo's profile gziolo
Milestone: 6.0 Priority: normal
Severity: normal Version: 5.9
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

Suppose a preloaded request includes a _fields query param that asks that only selected response fields are returned. You can add such a request with a filter:

function add_filtered_index_request( $preload_paths ) {
  array_push( $preload_paths, '/?_fields=home,title' );
  return $preload_paths;
}
add_filter( 'block_editor_rest_api_preload_paths', 'add_filtered_index_request' );

However, you will discover that the _fields param is not respected and all fields are returned.

This is because the rest_preload_api_request doesn't call rest_filter_response_fields on the response object. For other requests this call happens inside the rest_post_dispatch filter, but this filter is not called when preloading responses.

Change History (7)

This ticket was mentioned in PR #2340 on WordPress/wordpress-develop by jsnajdr.


2 years ago
#1

Fixes https://core.trac.wordpress.org/ticket/55213

Suppose a preloaded request includes a _fields query param that asks that only selected response fields are returned. You can add such a request with a filter:
{{{php
function add_filtered_index_request( $preload_paths ) {

array_push( $preload_paths, '/?_fields=home,title' );
return $preload_paths;

}
add_filter( 'block_editor_rest_api_preload_paths', 'add_filtered_index_request' );
}}}
However, you will discover that the _fields param is not respected and all fields are returned.

This is because the rest_preload_api_request doesn't call rest_filter_response_fields on the response object. For other requests this call happens inside the rest_post_dispatch filter, but this filter is not called when preloading responses.

This patch adds the rest_post_dispatch filter call after every preloaded response. This filter also calls the rest_send_allow_header function to add the Allow header, so we can also remove an explicit call when handling an OPTIONS request.

Calling the rest_post_dispatch filter should be perfectly fine because it's already called in all similar circumstances:

  • when serving a normal REST request in WP_REST_Server::serve_request
  • when performing a batched request in WP_REST_Server::serve_batch_request_v1
  • when embedding links in WP_REST_Server::embed_links

One extra effect of this patch is that preloaded responses for GET requests will also include headers, while previously they would only include body, and headers would be present only for OPTIONS responses. Before:

{
  '/': {
    body: { home, title }
  },
  'OPTIONS': {
    '/': {
      body: { home, title },
      headers: { Allow }
    }
  }
}

After:

{
  '/': {
    body: { home, title },
    headers: { Allow }
  },
  'OPTIONS': {
    '/': {
      body: { home, title },
      headers: { Allow }
    }
  }
}

Including these headers is a good and desirable thing because it allows us to get rid of redundant OPTIONS request where we already have GET response. The GET response would now already contain the desired info about permissions. See also this discussion on a Gutenberg PR: https://github.com/WordPress/gutenberg/pull/38901#issuecomment-1044340843

In principle this patch is similar to what @TimothyBJacobs did some time ago for embeds: https://core.trac.wordpress.org/ticket/51722

This ticket was mentioned in Slack in #core-editor by jsnajdr. View the logs.


2 years ago

#3 @noisysocks
2 years ago

  • Milestone changed from Awaiting Review to 6.0

#4 @costdev
2 years ago

  • Milestone changed from 6.0 to 6.1

With 6.0 Beta 1 released yesterday, I'm moving this ticket to the 6.1 milestone.

#5 @gziolo
2 years ago

  • Keywords has-unit-tests added
  • Milestone changed from 6.1 to 6.0

#6 @gziolo
2 years ago

  • Owner set to gziolo
  • Resolution set to fixed
  • Status changed from new to closed

In 53217:

REST API: Respect _fields query arg in preloaded requests

Ensures that preloaded request can include a _fields query param that asks that only selected response fields are returned.

Props jsnajdr, timothyblynjacobs.
Fixes #55213.
See #55567.

Note: See TracTickets for help on using tickets.