-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DI] Added safeguards against invalid config in the YamlFileLoader #11374
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
foreach ($content['imports'] as $import) { | ||
if (!is_array($import)) { | ||
throw new InvalidArgumentException(sprintf('The values in "imports" key should contain be arrays in file "%s"', $file)); |
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.
typo here should contain be
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.
we could perhaps output also what was given instead to give more hints of what the error is?
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.
Dumping values in the exception message is a bad idea IMO. It can make it totally unreadable
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.
@stof Can you fix the typo?
Some of the exceptions you added end with a dot, some other not. |
@@ -55,6 +55,10 @@ public function load($file, $type = null) | |||
|
|||
// parameters | |||
if (isset($content['parameters'])) { | |||
if (!is_array($content['parameters'])) { | |||
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in file "%s"', $file)); |
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.
What about adding one additional quick note at the end: Check your YAML syntax.
I think beginners don't think about YAML in terms of the data structure it will compile down to - so I a little extra pointer that they have a syntax issue might help. Great PR btw :).
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.
The messages I added are matching the existing safeguards (some places were guarded in the loader, but not all)
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.
I see! If we like this little addition (i.e. Check your YAML syntax
), then why not add it everywhere? :)
each code throwing exceptions is copy-pasted from similar safeguards already existing in the class. So this means that these inconsistencies already exist in the current exception messages |
@stof Can you fixed the typo and add dots everywhere? |
0c2cb00
to
908fe5c
Compare
PR updated. |
foreach ($content['imports'] as $import) { | ||
if (!is_array($import)) { | ||
throw new InvalidArgumentException(sprintf('The values in "imports" key should be arrays in %s. Check your YAML syntax.', $file)); |
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.
[...] in the "Imports" [...]?
Exceptions explaining the mistake are better than fatal errors or weird notices appearing when trying to deal with such invalid data. Closes symfony#11333
908fe5c
to
5183501
Compare
@@ -55,6 +55,10 @@ public function load($file, $type = null) | |||
|
|||
// parameters | |||
if (isset($content['parameters'])) { | |||
if (!is_array($content['parameters'])) { | |||
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $file)); |
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.
Shouldn't the text be The "parameters" key should be an array in %s.
?
Because parameters
does not need to contain an array, but just be one? Even the code above says so.
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.
no, the key is not an array. The key is a string
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.
Ah, I actually missed the key
part. Still early in the morning. 😉
ping @symfony/deciders |
👍 |
Thank you @stof. |
…leLoader (stof) This PR was merged into the 2.3 branch. Discussion ---------- [DI] Added safeguards against invalid config in the YamlFileLoader | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11333 | License | MIT | Doc PR | n/a Exceptions explaining the mistake are better than fatal errors or weird notices appearing when trying to deal with such invalid data. The XML file loader is not affected by this because the data are validated with the XSD before being processed Commits ------- 5183501 [DI] Added safeguards against invalid config in the YamlFileLoader
This is BC break for this bundle https://github.com/janmarek/autowiring-bundle If you follow coding style where service name reflects namespace+classname then you don't need to declare class again. It would be nice if you can overpass this check. e.g. buzz.message.factory.factory:
class: Buzz\Message\Factory\Factory can be simplified into buzz.message.factory.factory: |
what is the BC break exactly ? |
Services configuration may look like services:
buzz.message.factory.factory:
some.other.class: which leads to an InvalidArgumentException as the definition isn't an array when being loaded. |
Exceptions explaining the mistake are better than fatal errors or weird notices appearing when trying to deal with such invalid data.
The XML file loader is not affected by this because the data are validated with the XSD before being processed