Skip to content

Commit 8977ae8

Browse files
CopilotswissspidyCopilot
authored
Handle wp ability run missing input based on input_schema (#7)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 9e2b34e commit 8977ae8

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

features/ability.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ Feature: Manage abilities registered via the WordPress Abilities API.
191191
},
192192
'permission_callback' => '__return_true',
193193
) );
194+
195+
wp_register_ability( 'test-plugin/simple-no-input', array(
196+
'label' => 'Simple No Input',
197+
'description' => 'Returns an empty array.',
198+
'category' => 'test-category',
199+
'output_schema' => array(
200+
'type' => 'array',
201+
),
202+
'execute_callback' => function( $input = null ) {
203+
return array();
204+
},
205+
'permission_callback' => '__return_true',
206+
) );
194207
} );
195208
"""
196209

@@ -224,6 +237,12 @@ Feature: Manage abilities registered via the WordPress Abilities API.
224237
{"sum":30}
225238
"""
226239

240+
When I run `wp ability run test-plugin/simple-no-input`
241+
Then STDOUT should be:
242+
"""
243+
[]
244+
"""
245+
227246
When I run `wp ability run test-plugin/get-site-title --format=yaml`
228247
Then STDOUT should contain:
229248
"""

src/Ability_Command.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ public function run( $args, $assoc_args ): void {
369369

370370
// Build input data (with stdin support).
371371
$input = $this->build_input_with_stdin( $assoc_args );
372+
if ( null === $input && [] !== $ability->get_input_schema() ) {
373+
$input = [];
374+
}
372375

373376
// Execute the ability.
374377
$result = $ability->execute( $input );
@@ -593,7 +596,7 @@ private function build_input( $assoc_args ) {
593596
* Builds input data from associative arguments with stdin support.
594597
*
595598
* @param array $assoc_args Associative arguments.
596-
* @return array<string,mixed> The input data.
599+
* @return array<string,mixed>|null The input data, or null if none provided.
597600
*/
598601
private function build_input_with_stdin( $assoc_args ) {
599602
$input = [];
@@ -626,7 +629,8 @@ private function build_input_with_stdin( $assoc_args ) {
626629
$input[ $key ] = $value;
627630
}
628631

629-
return $input;
632+
// Return null only when no input was provided (no --input flag and no field args).
633+
return ( empty( $input ) && null === $json_input ) ? null : $input;
630634
}
631635

632636
/**

0 commit comments

Comments
 (0)