Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#45220 closed defect (bug) (fixed)

register_rest_field() doesn't work when schema is empty

Reported by: dudo's profile Dudo Owned by: danielbachhuber's profile danielbachhuber
Milestone: 5.0 Priority: normal
Severity: normal Version: 5.0
Component: REST API Keywords: has-patch has-unit-tests fixed-5.0
Focuses: rest-api Cc:

Description

Just descovered that on the latest Nightly build (5.0-beta1-43832) the new field registerd with register_rest_field is not returned at all.

Works fine on 4.9.8

Attachments (2)

45220.1.diff (1.7 KB) - added by danielbachhuber 6 years ago.
45220.2.diff (2.8 KB) - added by danielbachhuber 6 years ago.

Download all attachments as: .zip

Change History (22)

#1 @swissspidy
6 years ago

  • Keywords reporter-feedback added

Hey there and welcome to WordPress Trac!

Could you please show us the code that you're using? Without that it's hard to reproduce.

#2 @Dudo
6 years ago

Hi, thank you for your response.

It is very simple:

<?php
add_action( 'rest_api_init', 'yasr_rest_comment_meta' );

function yasr_rest_comment_meta() {

        register_rest_field(
                'post',
                'yasr_overall_rating',
                array(
                        'get_callback' => 'yasr_rest_get_overall_rating',
                        'update_callback' => null,
                        'schema' => null,
                )

        );

}

function yasr_rest_get_overall_rating () {

        $overall_rating = yasr_get_overall_rating();

        return $overall_rating;

}

#3 @ocean90
6 years ago

Related: [43736]

Are you using the ?_fields= argument?

#4 @Dudo
6 years ago

nope, the js part is just this

    fetch('http://192.168.33.10/plugin_development/wp-json/wp/v2/posts/3569').then(response => {
        return response.json();
    }).then(data => {
        // Work with JSON data here
        console.log(data);
    }).catch(err => {
        // Do something for an error here
    });

#5 @TimothyBlynJacobs
6 years ago

This is because the schema is set to null so get_fields_for_resposne() doesn't pick it up since it isn't registered in the schema.

#6 @Dudo
6 years ago

seems like is working with this code

<?php
function yasr_rest_comment_meta() {

        register_rest_field(
                'post',
                'yasr_overall_rating',
                array(
                        'get_callback' => 'yasr_rest_get_overall_rating',
                        'update_callback' => null,
                        'schema' => $overall_rating_schema,
                )

        );

        $overall_rating_schema = array(
                'description'   => 'Overall Rating for the post',
                'type'          => 'string',
                'context'       =>  array( 'view' )
        );

}

I think the "schema" part needs more documentation.

#7 @Dudo
6 years ago

With version 5.0-beta2-43846 doesn't work with both 'schema' declared or set to null

#8 @daniloalvess
6 years ago

Until then the scheme argument was optional a and by default null. If i use the _fields , have to specify all fields, including the default fields (id, title, content...).
I would like to show just my custom fields. What is the best option? What should I do with the legacy projects without the scheme argument?

#9 @vrundakansara
6 years ago

With version, 5.0-beta4-43896 too doesn't work with both 'schema' declared or set to null.

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


6 years ago

#11 @danielbachhuber
6 years ago

  • Focuses rest-api added
  • Milestone changed from Awaiting Review to 5.0

#12 @Mahesh901122
6 years ago

I have also tested with 5.0-beta5-43907 and it doesn't work.

#13 @danielbachhuber
6 years ago

  • Keywords needs-patch needs-unit-tests added; reporter-feedback removed

I was able to reproduce this issue by registering a custom field like follows:

add_action( 'rest_api_init', function() {
	register_rest_field( 'post', 'test_field', array(
		'get_callback' => function() {
			return 'test_value';
		},
		'schema' => null,
	) );
});

In WP 4.9.8, the test_field attribute is present for GET http://wptest.test/wp-json/wp/v2/posts/1. In WP 5.0-beta5-43907, the test_field attribute is missing.

Where did it go?!

#14 @danielbachhuber
6 years ago

  • Keywords has-patch has-unit-tests commit added; needs-patch needs-unit-tests removed
  • Summary changed from register_rest_field doesn't work to register_rest_field() doesn't work when schema is empty

The culprit was r43736.

In 45220.1.diff, ?_fields= filtering is only applied when ?_fields= is present in the request. I think this is the proper way to go about this, but it's the Friday of a very long week so I'm going to let it sit until early next.

#15 @danielbachhuber
6 years ago

In 45220.2.diff, any fields registered with register_rest_field() that have a null schema are included in WP_REST_Controller::get_fields_for_response(). I think this is a more appropriate fix because:

  1. It ensures get_fields_for_response() has the expected return values.
  2. It includes my_custom_int in the response when the request is ?_fields=title,my_custom_int.

#16 @danielbachhuber
6 years ago

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

In 43908:

REST API: Include fields with null schema in get_fields_for_response().

In [43736], we prevented rendering fields when not present in ?_fields=. However, because get_fields_for_response() is dependent on get_item_schema(), any custom fields registered with a null schema would be incorrectly excluded from the response. Because the REST API permits a null schema for register_rest_field(), those fields should be included in the available fields for a response.

Fixes #45220.

#17 @danielbachhuber
6 years ago

  • Keywords fixed-5.0 added; commit removed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for merge to trunk.

#18 @SergeyBiryukov
6 years ago

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

In 44254:

REST API: Include fields with null schema in get_fields_for_response().

In [43736], we prevented rendering fields when not present in ?_fields=. However, because get_fields_for_response() is dependent on get_item_schema(), any custom fields registered with a null schema would be incorrectly excluded from the response. Because the REST API permits a null schema for register_rest_field(), those fields should be included in the available fields for a response.

Props danielbachhuber.
Merges [43908] to trunk.
Fixes #45220.

#19 @SergeyBiryukov
6 years ago

In 44256:

Tests: Replace use of $this->server with rest_get_server() in test_get_additional_field_registration_null_schema().

In [42724], $this->server was replaced with rest_get_server() for better memory recycling.

[43908], from the 5.0 branch, was merged into trunk in [44254] and used the now unavailable $this->server.

This updates the new test from the 5.0 branch to use the expected rest_get_server().

See #45220, #41641.

#20 @dlh
6 years ago

#45635 was marked as a duplicate.

Note: See TracTickets for help on using tickets.