[wp-trac] [WordPress Trac] #50301: Missing args properties when WP_REST_Controller::get_endpoint_args_for_item_schema being used

WordPress Trac noreply at wordpress.org
Tue Jun 2 05:47:44 UTC 2020


#50301: Missing args properties when
WP_REST_Controller::get_endpoint_args_for_item_schema being used
----------------------------+-----------------------------
 Reporter:  pentatonicfunk  |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  REST API        |    Version:  5.4.1
 Severity:  normal          |   Keywords:
  Focuses:  rest-api        |
----------------------------+-----------------------------
 Some args properties are removed / ignored when
 `WP_REST_Controller::get_endpoint_args_for_item_schema` being used. Known
 removed/ignored properties are :
 * `minimum`
 * `maximum`
 * `exclusiveMinimum`
 * `exclusiveMaximum`

 === Test Case
 {{{#!php
 <?php
 class DummyApiController extends WP_REST_Controller {

     public function __construct() {
         $this->namespace = 'my-name-space';
         $this->rest_base = 'my-rest-base';

         $this->schema = array(
             '$schema'    => 'http://json-schema.org/draft-04/schema#',
             'title'      => 'my-schema',
             'type'       => 'object',
             'properties' => array(
                 'some_number' => array(
                     'type'     => 'integer',
                     'minimum'  => 10,
                     'maximum'  => 20,
                     'required' => true,
                 ),
             ),
         );

         add_action( 'rest_api_init', array( $this, 'register_routes' ) );
     }

     public function get_item_schema() {
         return $this->add_additional_fields_schema( $this->schema );
     }

     public function register_routes() {
         register_rest_route(
             $this->namespace,
             '/' . $this->rest_base,
             array(
                 array(
                     'methods'             => WP_REST_Server::CREATABLE,
                     'callback'            => array( $this, 'create_item'
 ),
                     'permission_callback' => array( $this,
 'create_item_permissions_check' ),
                     'args'                =>
 $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
                 ),
                 'schema' => array( $this, 'get_public_item_schema' ),
             )
         );
     }

     public function create_item_permissions_check( $request ) {
         return true;
     }

     public function create_item( $request ) {
         return rest_ensure_response( true );
     }

 }

 $dummy = new DummyApiController();

 do_action( 'rest_api_init' );

 // test
 $dummy_request = new WP_REST_Request(
     WP_REST_Server::CREATABLE,
     '/my-name-space' . '/' . 'my-rest-base'
 );

 $dummy_request->set_param( 'some_number', 1 );

 $handler  = rest_get_server()->dispatch( $dummy_request
 )->get_matched_handler();
 $response = rest_get_server()->dispatch( $dummy_request );
 print_r( $handler['args']['some_number'] ); // minimum and maximum
 properties missing
 print_r( $response->get_data() ); // should be validation wp_error instead
 of true
 }}}


 === Expected
 * Extra properties should not be removed/ignored
 * Default args validation works

 === Actual
 * `minimum`, `maximum`, `exclusiveMinimum` and `exclusiveMaximum` are
 removed/ignored
 * Validation does not works because of properties above removed/ignored

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


More information about the wp-trac mailing list