-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Fix input validation when required arguments are missing #15533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
If I used a foreach loop instead of array_filter, I could improve the exception message and say 'Not enough arguments. "name" is missing.'. What do you think? |
This broke tests in FrameworkBundle. Looking into this. |
I had a look at the tests for HelpCommand:
What is passed as arguments: HelpCommand has no required arguments:
However, the |
I had a quick look at these failing tests as well. |
return $definition->hasArgument($argument) && $definition->getArgument($argument)->isRequired(); | ||
}); | ||
|
||
if (count($requiredArguments) < $this->definition->getArgumentRequiredCount()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (count($requiredArguments) < $definition->getArgumentRequiredCount()) {
would be shorter
@jakzal Any news on this one? And to answer your question, I would indeed give more information in the error message if possible. |
Previous rule was only working when arguments are passed from command line, as in command line there is no way of skipping an argument. The rule does not work for arguments set on the Input after a command is run.
30cbe80
to
f12a4c1
Compare
I added a better exception message: Solving the problem with failing command tests is a bit more complex. The The previous version of To fix this we need to either:
First solution would break lots of existing command tests, and the second doesn't seem like the right place (as the command argument is added by the application). |
I went with option 2. Happy to receive feedback. Ping @symfony/deciders. |
@@ -241,6 +241,10 @@ public function run(InputInterface $input, OutputInterface $output) | |||
$this->interact($input, $output); | |||
} | |||
|
|||
if ($input->hasArgument('command') && null === $input->getArgument('command')) { | |||
$input->setArgument('command', $this->getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain when this happens? I think it deserves a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion. I'll add:
// The command name argument is often omitted when a command is executed directly with its run() method.
// It will fail the validation since the command argument is required by the application.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writing this comment gave me another idea: is the command argument really a required argument? What if we made it optional instead of setting it here?
f2145bd
to
45c1cc6
Compare
45c1cc6
to
4982b02
Compare
👍 |
1 similar comment
👍 |
@jakzal Usually, when using the |
@xabbuh that's not gonna fix the case of calling commands directly, like here: https://github.com/symfony/symfony/blob/2.3/src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php#L83-83 |
Hm, indeed. 👍 Status: Reviewed |
Thank you @jakzal. |
… missing (jakzal) This PR was merged into the 2.3 branch. Discussion ---------- [Console] Fix input validation when required arguments are missing | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15505 | License | MIT | Doc PR | - The rule that was here in place previously only works when arguments are passed from command line, as in command line there is no way of skipping an argument. The rule does not work for arguments set on the Input after a command is run. Commits ------- 4982b02 [Console] Add the command name to input arguments if it's missing f12a4c1 [Console] Fix input validation when required arguments are missing
This makes test pass again given the improvements to the message of the exception that is thrown since symfony/symfony#15533 when a required argument is missing.
The rule that was here in place previously only works when arguments are passed from command line, as in command line there is no way of skipping an argument. The rule does not work for arguments set on the Input after a command is run.