Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function register_routes(): void {
'callback' => array( $this, 'execute_ability' ),
'permission_callback' => array( $this, 'check_ability_permissions' ),
'args' => $this->get_run_args(),

/*
* Serve the ability result as returned, so it follows the ability's output
* schema whether it is an object, an array, or a scalar value: `_fields`,
* `_embed` and response links leave it unchanged.
*/
'data_processing' => false,
),
'schema' => array( $this, 'get_run_schema' ),
)
Expand Down Expand Up @@ -96,18 +103,10 @@ public function execute_ability( $request ) {
}

/*
* Preserve empty objects in nested properties when encoding an associative
* result as JSON. Preparation can convert empty PHP arrays to stdClass.
*
* The REST server's `_fields` and `_embed` processing expects arrays at the
* response root and for numeric collection items. Converting those values
* to objects would cause type errors, so skip root empty arrays and numeric
* collections until that processing supports objects. An empty object result
* therefore still encodes as [], and a list containing one encodes as [[]].
* Preserve empty objects when encoding the result as JSON. Preparation can
* convert empty PHP arrays to stdClass, also within object results.
*/
if ( is_array( $result ) && ! wp_is_numeric_array( $result ) ) {
$result = wp_prepare_json_value_for_client( $result, $ability->get_output_schema() );
}
$result = wp_prepare_json_value_for_client( $result, $ability->get_output_schema() );

return rest_ensure_response( $result );
}
Expand Down
23 changes: 21 additions & 2 deletions tests/phpunit/tests/rest-api/wpRestAbilitiesSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Tests_REST_API_WpRestAbilitiesSettingsController extends WP_UnitTestCase {
/**
* The REST server instance for the current test.
*
* @var WP_REST_Server
* @var Spy_REST_Server
*/
protected $server;

Expand Down Expand Up @@ -108,7 +108,7 @@ public function set_up(): void {
parent::set_up();

global $wp_rest_server;
$wp_rest_server = new WP_REST_Server();
$wp_rest_server = new Spy_REST_Server();
$this->server = $wp_rest_server;
do_action( 'rest_api_init' );

Expand Down Expand Up @@ -196,6 +196,25 @@ public function test_fields_filter_from_query_string_limits_settings(): void {
$this->assertEqualSets( array( 'title', 'posts_per_page' ), array_keys( $data ), 'Only the settings from the comma-separated list should be returned.' );
}

/**
* An empty result is served as a JSON object, which the output schema requires.
*
* @ticket 66267
*/
public function test_no_matching_setting_is_encoded_as_an_empty_object(): void {
$_GET = array(
'input' => array(
'group' => 'reading',
'fields' => 'title',
),
);

$this->server->serve_request( self::RUN_ROUTE );

$this->assertSame( 200, $this->server->status, 'Filters that match no setting should be accepted.' );
$this->assertSame( '{}', $this->server->sent_body, 'The empty result should be served as an empty object.' );
}

/**
* @ticket 64605
*/
Expand Down
251 changes: 242 additions & 9 deletions tests/phpunit/tests/rest-api/wpRestAbilitiesV1RunController.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,118 @@ private function register_test_abilities(): void {
)
);

// Ability that returns an object result with an empty object property.
$this->register_test_ability(
'test/nested-empty-object-in-object-output',
array(
'label' => 'Nested Empty Object In Object Output',
'description' => 'Returns an object result with an empty object property.',
'category' => 'general',
'output_schema' => array(
'type' => 'object',
'properties' => array(
'settings' => array( 'type' => 'object' ),
),
),
'execute_callback' => static function () {
return (object) array( 'settings' => array() );
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

// Ability that returns a JsonSerializable result with an empty object property.
$this->register_test_ability(
'test/nested-empty-object-in-json-serializable-output',
array(
'label' => 'Nested Empty Object In JsonSerializable Output',
'description' => 'Returns a JsonSerializable result with an empty object property.',
'category' => 'general',
'output_schema' => array(
'type' => 'object',
'properties' => array(
'settings' => array( 'type' => 'object' ),
),
),
'execute_callback' => static function () {
return new class() implements JsonSerializable {
public function jsonSerialize(): array {
return array( 'settings' => array() );
}
};
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

// Ability that returns an empty list.
$this->register_test_ability(
'test/empty-list-output',
array(
'label' => 'Empty List Output',
'description' => 'Returns an empty list.',
'category' => 'general',
'output_schema' => array(
'type' => 'array',
'items' => array( 'type' => 'object' ),
),
'execute_callback' => static function () {
return array();
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

// Ability that returns a list of strings.
$this->register_test_ability(
'test/string-list-output',
array(
'label' => 'String List Output',
'description' => 'Returns a list of strings.',
'category' => 'general',
'output_schema' => array(
'type' => 'array',
'items' => array( 'type' => 'string' ),
),
'execute_callback' => static function () {
return array( 'a', 'b' );
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

// Ability that returns a string.
$this->register_test_ability(
'test/string-output',
array(
'label' => 'String Output',
'description' => 'Returns a string.',
'category' => 'general',
'output_schema' => array(
'type' => 'string',
),
'execute_callback' => static function () {
return 'value';
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

// Read-only ability for query params testing.
$this->register_test_ability(
'test/query-params',
Expand Down Expand Up @@ -637,15 +749,19 @@ public function test_output_validation(): void {
*/
public static function data_output_is_prepared_for_json_clients(): array {
return array(
'root empty object stays unchanged' => array( 'test/empty-object-output', '[]' ),
'nested empty object' => array( 'test/nested-empty-object-output', '{"settings":{}}' ),
'numeric collection stays unchanged' => array( 'test/empty-object-list-output', '[[]]' ),
'root empty object' => array( 'test/empty-object-output', '{}' ),
Comment thread
gziolo marked this conversation as resolved.
Resolved
'nested empty object' => array( 'test/nested-empty-object-output', '{"settings":{}}' ),
'numeric collection' => array( 'test/empty-object-list-output', '[{}]' ),
'object result' => array( 'test/nested-empty-object-in-object-output', '{"settings":{}}' ),
'JsonSerializable result' => array( 'test/nested-empty-object-in-json-serializable-output', '{"settings":{}}' ),
'root empty list' => array( 'test/empty-list-output', '[]' ),
'list of strings' => array( 'test/string-list-output', '["a","b"]' ),
'string' => array( 'test/string-output', '"value"' ),
);
}

/**
* Tests that ability results are prepared for JSON clients without breaking
* the global `_fields` and `_embed` parameters.
* Tests that ability results are prepared for JSON clients.
*
* @ticket 66267
* @dataProvider data_output_is_prepared_for_json_clients
Expand All @@ -657,15 +773,132 @@ public function test_output_is_prepared_for_json_clients( string $ability_name,
$request = new WP_REST_Request( 'POST', "/wp-abilities/v1/abilities/{$ability_name}/run" );
$response = $this->server->dispatch( $request );

$this->assertSame( 200, $response->get_status() );
$this->assertSame( $expected, wp_json_encode( $response->get_data() ) );
$this->assertSame( 200, $response->get_status(), 'The ability should run successfully.' );
$this->assertSame( $expected, wp_json_encode( $response->get_data() ), 'The result should be encoded as expected.' );
}

/**
* Tests that the global `_fields` and `_embed` parameters and response links leave
* ability results unchanged.
*
* @ticket 66267
*/
public function test_global_parameters_and_response_links_leave_output_unchanged(): void {
$request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/nested-empty-object-output/run' );

// The result has no `other` field, so filtering would leave none of the result.
$request->set_query_params(
array(
'_fields' => 'other',
'_embed' => '1',
)
);

$request->set_param( '_fields', 'settings' );
add_filter(
'rest_post_dispatch',
static function ( $response ) {
$response->add_link( 'alternate', rest_url( '/wp-abilities/v1/abilities' ), array( 'embeddable' => true ) );
return $response;
}
);

$response = $this->server->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The ability should run successfully.' );

add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
$response = apply_filters( 'rest_post_dispatch', $response, $this->server, $request );
$data = $this->server->response_to_data( $response, rest_parse_embed_param( $request['_embed'] ) );

$this->assertSame( '{"settings":{}}', wp_json_encode( $data ), 'The result should be left unchanged.' );
}

/**
* Tests that the global `_embed` parameter leaves the `_links` returned by an ability
* unchanged, without requesting the linked resources.
*
* @ticket 66267
*/
public function test_links_in_the_result_are_not_embedded(): void {
$result = array(
'id' => 1,
'_links' => array(
'author' => array(
array(
'href' => rest_url( '/wp-abilities/v1/abilities' ),
'embeddable' => true,
),
),
),
);
$this->register_test_ability(
'test/links-output',
array(
'label' => 'Links Output',
'description' => 'Returns an object with its own embeddable link.',
'category' => 'general',
'output_schema' => array(
'type' => 'object',
),
'execute_callback' => static function () use ( $result ) {
return $result;
},
'permission_callback' => '__return_true',
'meta' => array(
'show_in_rest' => true,
),
)
);

$request = new WP_REST_Request( 'POST', '/wp-abilities/v1/abilities/test/links-output/run' );
$request->set_query_params( array( '_embed' => '1' ) );

$response = $this->server->dispatch( $request );

$this->assertSame( 200, $response->get_status(), 'The ability should run successfully.' );

$dispatch = new MockAction();
add_filter( 'rest_pre_dispatch', array( $dispatch, 'filter' ) );
$data = $this->server->response_to_data( $response, rest_parse_embed_param( $request['_embed'] ) );

$this->assertSame( $result, $data, 'The links returned by the ability should be left unchanged.' );
$this->assertSame( 0, $dispatch->get_call_count(), 'No linked resource should be requested.' );
}

/**
* Tests that preloading a read-only ability run keeps the result prepared for
* JSON clients, also with the global `_fields` and `_embed` parameters.
*
* @ticket 66267
*/
public function test_preloaded_output_is_prepared_for_json_clients(): void {
$this->register_test_ability(
'test/readonly-output',
array(
'label' => 'Read-only Output',
'description' => 'Returns an empty object from a read-only ability.',
'category' => 'general',
'output_schema' => array(
'type' => 'object',
),
'execute_callback' => static function () {
return array();
},
'permission_callback' => '__return_true',
'meta' => array(
'annotations' => array(
'readonly' => true,
),
'show_in_rest' => true,
),
)
);

$path = '/wp-abilities/v1/abilities/test/readonly-output/run?_fields=other&_embed=1';
$preload = rest_preload_api_request( array(), $path );

$this->assertSame( $expected, wp_json_encode( $this->server->response_to_data( $response, true ) ) );
$this->assertArrayHasKey( $path, $preload, 'The ability run should be preloaded.' );
$this->assertSame( '{}', wp_json_encode( $preload[ $path ]['body'] ), 'The preloaded result should be encoded as an empty object.' );
}

/**
Expand Down
Loading