Make WordPress Core


Ignore:
Timestamp:
02/13/2024 01:46:45 PM (3 months ago)
Author:
spacedmonkey
Message:

REST API: Set maximum 'per_page' for embedded REST API requests.

This enhancement refines the REST API server to automatically establish the maximum 'per_page' value for embedded objects, adhering to the endpoint's schema when not explicitly defined in the request. This adjustment elevates the limit from the default of 10 items to 100 items, significantly improving the likelihood of receiving the complete dataset of embedded objects.

Props manyourisms, lpawlik, spacedmonkey, kadamwhite, TimothyBlynJacobs.
Fixes #43439.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r57108 r57623  
    1919    protected static $supported_formats;
    2020    protected static $post_ids    = array();
     21    protected static $terms       = array();
    2122    protected static $total_posts = 30;
    2223    protected static $per_page    = 50;
     
    2930    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    3031        self::$post_id = $factory->post->create();
     32        self::$terms   = $factory->term->create_many( 15, array( 'taxonomy' => 'category' ) );
     33        wp_set_object_terms( self::$post_id, self::$terms, 'category' );
    3134
    3235        self::$superadmin_id  = $factory->user->create(
     
    222225        sort( $keys );
    223226        $this->assertSame( array( 'context', 'id', 'password' ), $keys );
     227    }
     228
     229    public function test_registered_get_items_embed() {
     230        $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     231        $request->set_param( 'include', array( self::$post_id ) );
     232        $response = rest_get_server()->dispatch( $request );
     233        $response = rest_get_server()->response_to_data( $response, true );
     234        $this->assertArrayHasKey( '_embedded', $response[0], 'The _embedded key must exist' );
     235        $this->assertArrayHasKey( 'wp:term', $response[0]['_embedded'], 'The wp:term key must exist' );
     236        $this->assertCount( 15, $response[0]['_embedded']['wp:term'][0], 'Should should be 15 terms and not the default 10' );
     237        $i = 0;
     238        foreach ( $response[0]['_embedded']['wp:term'][0] as $term ) {
     239            $this->assertSame( self::$terms[ $i ], $term['id'], 'Check term id existing in response' );
     240            ++$i;
     241        }
    224242    }
    225243
Note: See TracChangeset for help on using the changeset viewer.