[wp-trac] [WordPress Trac] #50012: REST API: Fields filter does not work with "object" collection responses

WordPress Trac noreply at wordpress.org
Sat Apr 25 22:10:43 UTC 2020


#50012: REST API: Fields filter does not work with "object" collection responses
-------------------------------+----------------------------
 Reporter:  TimothyBlynJacobs  |      Owner:  (none)
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Future Release
Component:  REST API           |    Version:  4.8
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+----------------------------
 As reported in
 [https://wordpress.slack.com/archives/C02RQC26G/p1587758552319200 #core-
 restapi], trying to use the `_fields` parameter with the
 `wp/v2/taxonomies` route does not return the expected result. Instead, an
 empty set is returned.

 `http://trunk.test/wp-json/wp/v2/taxonomies?_fields=name`

 Looking into it, this happens with both the `taxonomies` and `types` route
 because they return an "object" collection response. That is, instead of
 returning an array, they return an object keyed by the taxonomy or post
 type slug.

 {{{
 {
   "category": {
     "name": "Categories",
     "slug": "category"
   }
 }
 }}}

 instead of...

 {{{
 [
   {
     "name": "Categories",
     "slug": "category"
   }
 ]
 }}}

 This means that the `wp_is_numeric_array` check in
 `rest_filter_response_fields` to try and determine whether this is a
 collection response fails and it treats the response as a single object,
 and then ends up removing all the properties.

 It appears that the `_fields` check inside of the controller itself work,
 it is just the "fallback" filtering that doesn't work and ends up
 corrupting the response.

 A possible fix would be to somehow mark that a response has already been
 filtered, and `rest_filter_response_fields` does not need to be run. This
 would probably also be a performance benefit.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50012>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list