Skip to content

[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

Merged
merged 1 commit into from
Sep 10, 2014

Conversation

stof
Copy link
Member

@stof stof commented Jul 11, 2014

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

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));
Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Member Author

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

Copy link
Member

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?

@romainneutron
Copy link
Contributor

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));
Copy link
Member

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 :).

Copy link
Member Author

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)

Copy link
Member

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? :)

@stof
Copy link
Member Author

stof commented Jul 15, 2014

Some of the exceptions you added end with a dot, some other not.

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

@fabpot
Copy link
Member

fabpot commented Jul 23, 2014

@stof Can you fixed the typo and add dots everywhere?

@stof stof force-pushed the fix_yaml_di_validation branch from 0c2cb00 to 908fe5c Compare September 2, 2014 01:25
@stof
Copy link
Member Author

stof commented Sep 2, 2014

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));
Copy link
Member

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
@stof stof force-pushed the fix_yaml_di_validation branch from 908fe5c to 5183501 Compare September 2, 2014 07:31
@@ -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));
Copy link
Contributor

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.

Copy link
Member Author

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

Copy link
Contributor

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. 😉

@stof
Copy link
Member Author

stof commented Sep 4, 2014

ping @symfony/deciders

@fabpot
Copy link
Member

fabpot commented Sep 4, 2014

👍

@fabpot
Copy link
Member

fabpot commented Sep 10, 2014

Thank you @stof.

@fabpot fabpot merged commit 5183501 into symfony:2.3 Sep 10, 2014
fabpot added a commit that referenced this pull request Sep 10, 2014
…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
@stof stof deleted the fix_yaml_di_validation branch September 10, 2014 13:18
@JakubKohout
Copy link

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:

@stof
Copy link
Member Author

stof commented Sep 18, 2014

what is the BC break exactly ?

@pavelkucera
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants