Skip to content

[Serializer] ObjectNormalizer: throw if PropertyAccess isn't installed #24563

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

Closed
wants to merge 2 commits into from

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Oct 15, 2017

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #24553
License MIT
Doc PR n/a

@xabbuh
Copy link
Member

xabbuh commented Oct 16, 2017

I think we should also not register the ObjectNormalizer as a service in the FrameworkBundle when the PropertyAccess component is not installed. We did that in several other places.

@dunglas
Copy link
Member Author

dunglas commented Oct 16, 2017

@xabbuh if we do that, will not it be very hard to debug?

@@ -33,6 +34,10 @@ class ObjectNormalizer extends AbstractNormalizer

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null)
{
if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
throw new RuntimeException('Unable to use ObjectNormalizer as the Symfony PropertyAcess component is not installed.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropertyAccess

@BoShurik
Copy link
Contributor

@dunglas otherwise impossible to use

composer create-project symfony/skeleton
composer req serializer

@@ -33,6 +34,10 @@ class ObjectNormalizer extends AbstractNormalizer

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null)
{
if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
throw new RuntimeException('Unable to use ObjectNormalizer as the Symfony PropertyAccess component is not installed.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you guys think about:

The ObjectNormalizer class requires the "PropertyAccess" component. Install symfony/property-access to use it.

The "Install symfony/property-access" seems important now that even FW users are responsible in general for installing packages that they need.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, however it will be many other texts to change (I've adapted an existing one, coming from FrameworkBundle IIRC).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've just added extra quotes around symfony/property-access.

@dunglas
Copy link
Member Author

dunglas commented Oct 16, 2017

@xabbuh, after double checking ObjectNormalizer is the only generic normalizer registered. It has a low property: it is called only if no other normalizer is matching. I think it's better in term of DX to get this exception than a silent fail like "no normalizer if able to handle "Foo\Bar".

@fabpot
Copy link
Member

fabpot commented Oct 16, 2017

Thank you @dunglas.

fabpot added a commit that referenced this pull request Oct 16, 2017
…'t installed (dunglas)

This PR was squashed before being merged into the 2.7 branch (closes #24563).

Discussion
----------

[Serializer] ObjectNormalizer: throw if PropertyAccess isn't installed

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #24553
| License       | MIT
| Doc PR        | n/a

Commits
-------

b5246a7 [Serializer] ObjectNormalizer: throw if PropertyAccess isn't installed
@fabpot fabpot closed this Oct 16, 2017
fabpot added a commit that referenced this pull request Oct 16, 2017
…t installed (dunglas)

This PR was squashed before being merged into the 3.3 branch (closes #24565).

Discussion
----------

[Serializer] YamlEncoder: throw if the Yaml component isn't installed

| Q             | A
| ------------- | ---
| Branch?       | 3.3 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | no
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | n/a

Similar to #24563 but for the Yaml encoder (not the same branch).

Commits
-------

7d21caf [Serializer] YamlEncoder: throw if the Yaml component isn't installed
@BoShurik
Copy link
Contributor

@dunglas this exception does not rise when using flex. Is it ok? I've got an exception when trying to access serializer component

PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "PropertyAccessor" from namespace "Symfony\Component\PropertyAccess".
Did you forget a "use" statement for another namespace? in /Users/boshurik/Projects/symfony-property-access-issue/var/cache/dev/srcDevDebugProjectContainer.php:435

@ogizanagi
Copy link
Contributor

See #24605

fabpot added a commit that referenced this pull request Oct 28, 2017
…omponent isn't installed (ogizanagi)

This PR was squashed before being merged into the 2.7 branch (closes #24605).

Discussion
----------

[FrameworkBundle] Do not load property_access.xml if the component isn't installed

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | #24563 (comment) <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

This PR actually aims to fix #24563 (comment) as the exception introduced in the PR can't be reached anyway when using the FrameworkBundle without the property access component as you'll get:

> Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "PropertyAccessor" from namespace "Symfony\Component\PropertyAccess".

With this fix, you properly get:

> The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.

Not sure this change really belongs to a patch release, but the original PR was accepted in the 2.7 branch.

Also, I'd rather remove the ObjectNormalizer definition if the component isn't available, as suggested by @xabbuh (#24563 (comment)). But in 2.7, this is the only normalizer registered by default and the [`SerializerPass` throws an exception if no normalizer is registered.](https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php#L46)

To sum up, either:

1. we completly prevent using the FrameworkBundle and the serializer without the property access component, even if you don't really care about the ObjectNormalizer because you only use your owns specific ones. (and you'll get the exception hinting to install the property access component)
2. we allow using the FrameworkBundle and the serializer without the property access component, so we remove the ObjectNormalizer definition, but the user'll get a `You must tag at least one service as "serializer.normalizer" to use the Serializer service` exception until he configures a normalizer (and we don't get the hint about installing the property access component to enable the ObjectNormalizer. We already have a suggest entry in the composer.json file, though).

To me option 2 looks better. WDYT?

Commits
-------

d297e27 [FrameworkBundle] Do not load property_access.xml if the component isn't installed
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Oct 28, 2017
…omponent isn't installed (ogizanagi)

This PR was squashed before being merged into the 2.7 branch (closes #24605).

Discussion
----------

[FrameworkBundle] Do not load property_access.xml if the component isn't installed

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see comment below -->
| Bug fix?      | yes
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | symfony/symfony#24563 (comment) <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | N/A

This PR actually aims to fix symfony/symfony#24563 (comment) as the exception introduced in the PR can't be reached anyway when using the FrameworkBundle without the property access component as you'll get:

> Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "PropertyAccessor" from namespace "Symfony\Component\PropertyAccess".

With this fix, you properly get:

> The ObjectNormalizer class requires the "PropertyAccess" component. Install "symfony/property-access" to use it.

Not sure this change really belongs to a patch release, but the original PR was accepted in the 2.7 branch.

Also, I'd rather remove the ObjectNormalizer definition if the component isn't available, as suggested by @xabbuh (symfony/symfony#24563 (comment)). But in 2.7, this is the only normalizer registered by default and the [`SerializerPass` throws an exception if no normalizer is registered.](https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/SerializerPass.php#L46)

To sum up, either:

1. we completly prevent using the FrameworkBundle and the serializer without the property access component, even if you don't really care about the ObjectNormalizer because you only use your owns specific ones. (and you'll get the exception hinting to install the property access component)
2. we allow using the FrameworkBundle and the serializer without the property access component, so we remove the ObjectNormalizer definition, but the user'll get a `You must tag at least one service as "serializer.normalizer" to use the Serializer service` exception until he configures a normalizer (and we don't get the hint about installing the property access component to enable the ObjectNormalizer. We already have a suggest entry in the composer.json file, though).

To me option 2 looks better. WDYT?

Commits
-------

d297e27600 [FrameworkBundle] Do not load property_access.xml if the component isn't installed
@fabpot fabpot mentioned this pull request Oct 30, 2017
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