Skip to content

Abilities without input_schema return an error when run via the CLI #6

@meszarosrob

Description

@meszarosrob

Bug Report

Describe the current, buggy behavior

The input_schema is optional, and I think for "simple" abilities, it's fairly common to skip it, especially for those that are "just" returning data.

https://github.com/WordPress/WordPress/blob/24dac1cd906af3e6c06a5c52b5ca8629b4c91080/wp-includes/abilities-api/class-wp-ability.php#L148

However, when I run the ability for an ability that doesn't have an input schema defined, e.g. wp ability run foo/simple-no-input, I get the following error:

Error: Ability "foo/simple-no-input" does not define an input schema required to validate the provided input.

Describe how other contributors can replicate this bug

Register an ability as follows:

add_action(
    'wp_abilities_api_categories_init',
    function () {
        wp_register_ability_category(
            'foo',
            [
                'label' => 'Foo',
                'description' => 'Foo...',
            ],
        );
    },
);

add_action(
    'wp_abilities_api_init',
    function () {
        wp_register_ability(
            'foo/simple-no-input',
            [
                'label' => 'Simple No Input',
                'description' => 'Simple No Input...',
                'category' => 'foo',
                'output_schema' => [
                    'type' => 'array',
                ],
                'execute_callback' => function () {
                    return [];
                },
                'permission_callback' => '__return_true',
            ],
        );
    },
);

Then run the wp ability run foo/simple-no-input via the CLI.

Describe what you would expect as the correct outcome

I would expect that if I register an ability without an input schema and call it with the CLI, I would receive an output, in this case [].

wp_get_ability('foo/simple-no-input')->execute() works perfectly.

Let us know what environment you are running this on

PHP version: 8.4.22
WP-CLI version: 2.13.0-alpha-c807ba0
WP version: 7.0

Provide a possible solution

...

Provide additional context/Screenshots

After a quick look, the problem seems to be that an empty array is passed to execute rather than null which is the default of the execute method:

// Build input data (with stdin support).
$input = $this->build_input_with_stdin( $assoc_args );
// Execute the ability.
$result = $ability->execute( $input );

https://github.com/WordPress/WordPress/blob/24dac1cd906af3e6c06a5c52b5ca8629b4c91080/wp-includes/abilities-api/class-wp-ability.php#L734

To make a "simple" ability work with the CLI the following input_schema can be added:

[
    'input_schema' => [
        'type' => 'object',
    ],
   
],

However, the consequence of this is that when wp_get_ability is used, it has to be executed with an input:

wp_get_ability('foo/simple-no-input')->execute([]);

The following would result in an error, even though for a "simple" ability this would be the ideal situation:

wp_get_ability('foo/simple-no-input')->execute();
object(WP_Error)#1124 (3) {
  ["errors"]=>
  array(1) {
    ["ability_invalid_input"]=>
    array(1) {
      [0]=>
      string(85) "Ability "foo/simple-no-input" has invalid input. Reason: input is not of type object."
    }
  }
  ["error_data"]=>
  array(0) {
  }
  ["additional_data":protected]=>
  array(0) {
  }
}

From what I see in the tests, there's no case where the input_schema is not defined:

'input_schema' => array(
'type' => 'object',
'properties' => array(),
),

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions