WordPress.org

Make WordPress Core

Opened 2 weeks ago

Last modified 10 hours ago

#49648 reopened defect (bug)

_fields parameter doesn't get nested proprieties from custom attribute

Reported by: Dudo Owned by: kadamwhite
Milestone: 5.4.1 Priority: normal
Severity: minor Version: 5.3
Component: REST API Keywords: fixed-major
Focuses: rest-api Cc:

Description

I've added a meta_key with register_meta, with this code

register_meta('post', 'yasr_overall_rating',
        array(
            'show_in_rest' => true,
            'single'       => true,
            'type'         => 'number',
            'auth_callback' => function() {
                return current_user_can('edit_posts');
            }
        )
    );

I can reach the nested proprieties successful with this:

?_fields=meta.yasr_overall_rating

But, I can't access the nested proprieties of a custom attribute, added with this code:

    $visitor_votes_obj = new YasrDatabaseRatings();

    $yasr_vv_schema = array(
        'description'          => 'Yasr Visitor Votes Data',
        'type'                 => 'object',
        'context'              => array('view', 'edit'),
        'properties'           => array(
            'sum_votes' => array(
                'type'  => 'integer',
            ),
            'number_of_votes' => array(
                'type'        => 'integer',
            ),
        ),
    );

register_rest_field(
        'post'
        'yasr-visitor-votes',
        array(
            'get_callback'    => (array($visitor_votes_obj, 'getVisitorVotes')),
            'update_callback' => null,
            'schema'          => $yasr_vv_schema
        )
    );

I can reach successful the attribute yasr-visitor-votes with this:

?_fields=yasr-visitor-votes

But I can't access the nested properties

?_fields=yasr-visitor-votes.sum_votes
?_fields=yasr-visitor-votes.number_of_votes

Attachments (2)

49648.1.diff (5.9 KB) - added by kadamwhite 3 days ago.
49648.2.diff (4.9 KB) - added by kadamwhite 44 hours ago.
Move cleanup to tearDown and resolve PHPCS/Syntax errors

Download all attachments as: .zip

Change History (8)

@kadamwhite
3 days ago

#1 @kadamwhite
3 days ago

  • Keywords has-patch has-unit-tests added
  • Milestone changed from Awaiting Review to 5.5
  • Owner set to kadamwhite
  • Status changed from new to accepted

Nice catch @Dudo, thank you for the ticket.

This happens because we were checking for field names with in_array in the REST Controller when iterating through additional fields, instead of using the more powerful new rest_is_field_included method. Switching to using that method causes the top-level field to be included if any child is present. Once that field is included, then the filtering logic (which runs on a hook within the server) can properly do its job to filter down to leaf nodes.

@TimothyBlynJacobs can you check my work here?

We could pull out the global field resets into a separate commit, those aren't strictly necessary for this patch, it just felt like missing cleanup in the test suite.

#2 @TimothyBlynJacobs
3 days ago

  • Summary changed from WordPress 5.4 RC2 : _fields parameter doesn't get nested proprieties from custom attribute to _fields parameter doesn't get nested proprieties from custom attribute
  • Version changed from 5.4 to 5.3

Switching to rest_is_field_included makes sense to me.

It seems like the global $wp_rest_additional_fields; $wp_rest_additional_fields = array(); pattern should probably be a part of the tearDown for that controller?

@kadamwhite
44 hours ago

Move cleanup to tearDown and resolve PHPCS/Syntax errors

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


44 hours ago

#4 @kadamwhite
43 hours ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 47511:

REST API: Fix _fields filtering of registered rest fields.

Use rest_is_field_included when determining which additional fields to include to permit filtering by nested field properties.

Props Dudo, kadamwhite, TimothyBlynJacobs.
Fixes #49648.

#5 @kadamwhite
10 hours ago

  • Keywords fixed-major added; has-patch has-unit-tests removed
  • Milestone changed from 5.5 to 5.4.1
  • Resolution fixed deleted
  • Status changed from closed to reopened

This ticket was mentioned in Slack in #core-committers by kadamwhite. View the logs.


10 hours ago

Note: See TracTickets for help on using tickets.