Opened 5 years ago
Closed 5 years ago
#49648 closed defect (bug) (fixed)
_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)
Change History (11)
#1
@
5 years 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
#2
@
5 years 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?
This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.
5 years ago
#5
@
5 years 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
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 newrest_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.