Make WordPress Core

Changeset 60301


Ignore:
Timestamp:
06/11/2025 05:40:44 PM (14 months ago)
Author:
johnbillion
Message:

REST API: Return a more appropriate HTTP 400 response code when attempting to create or update a non-existent setting.

This switches the response from a 200, which is not appropriate for invalid requests.

Props sheldorofazeroth, johnbillion

Fixes #41604

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    r58230 r60301  
    147147
    148148                $params = $request->get_params();
     149
     150                if ( empty( $params ) || ! empty( array_diff_key( $params, $options ) ) ) {
     151                        $message = empty( $params )
     152                                ? __( 'Request body cannot be empty.' )
     153                                : __( 'Invalid parameter(s) provided.' );
     154
     155                        return new WP_Error(
     156                                'rest_invalid_param',
     157                                $message,
     158                                array( 'status' => 400 )
     159                        );
     160                }
    149161
    150162                foreach ( $options as $name => $args ) {
  • trunk/tests/phpunit/tests/rest-api/rest-settings-controller.php

    r58230 r60301  
    386386
    387387        /**
    388          * @doesNotPerformAssertions
     388         * Settings can't be created
    389389         */
    390390        public function test_create_item() {
    391                 // Controller does not implement create_item().
     391                wp_set_current_user( self::$administrator );
     392
     393                $request = new WP_REST_Request( 'POST', '/wp/v2/settings' );
     394                $request->set_param( 'new_setting', 'New value' );
     395                $response = rest_get_server()->dispatch( $request );
     396
     397                $this->assertSame( 400, $response->get_status() );
    392398        }
    393399
    394400        public function test_update_item() {
    395401                wp_set_current_user( self::$administrator );
     402
    396403                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
    397404                $request->set_param( 'title', 'The new title!' );
     
    402409                $this->assertSame( 'The new title!', $data['title'] );
    403410                $this->assertSame( get_option( 'blogname' ), $data['title'] );
     411        }
     412
     413        public function test_update_nonexistent_item() {
     414                wp_set_current_user( self::$administrator );
     415
     416                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     417                $request->set_param( 'i_do_no_exist', 'New value' );
     418                $response = rest_get_server()->dispatch( $request );
     419
     420                $this->assertSame( 400, $response->get_status() );
     421        }
     422
     423        public function test_update_partially_valid_items() {
     424                wp_set_current_user( self::$administrator );
     425
     426                $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' );
     427                $request->set_param( 'title', 'The new title!' );
     428                $request->set_param( 'i_do_no_exist', 'New value' );
     429                $response = rest_get_server()->dispatch( $request );
     430
     431                $this->assertSame( 400, $response->get_status() );
    404432        }
    405433
Note: See TracChangeset for help on using the changeset viewer.