Skip to content
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

[Serializer] Call to a normalize() function on SerializerInterface in PropertyNormalizer #13909

Closed
Gasol opened this issue Mar 12, 2015 · 2 comments
Assignees
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Good first issue Ideal for your first contribution! (some Symfony experience may be required) Serializer

Comments

@Gasol
Copy link

Gasol commented Mar 12, 2015

I encounter the problem as following error message. Apparently, SerializerInterface does not have normalize function. It must be mistake here.

PHP Fatal error:  Call to a member function normalize() on a non-object in 
/path/to/project/vendor/symfony/serializer/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php 
on line 71

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php#L71

@Gasol
Copy link
Author

Gasol commented Mar 12, 2015

Symfony\Component\Serializer\SerializerInterface does not have normalize function, But Symfony\Component\Serializer\Serializer does.

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php#L108

<?php
if (!$this->serializer instanceof NormalizerInterface) {
    throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute));
}

So, I think the problem is:

  1. Wrong type hinting in setSerializer, should be Symfony\Component\Serializer\Serializer, not Symfony\Component\Serializer\SerializerInterface
  2. misuse of SerializerInterface (Obviously, $serializer is not a normalizer)

@dunglas
Copy link
Member

dunglas commented Mar 15, 2015

@Gasol can you provide the code you made to initialize your serializer and normalizer? Is your Serializer an instance of NormalizerInterface?

  1. The typehint is correct. If you don't typehint on the interface, use of custom serializers will be impossible.
  2. The default Serializer implements the NormalizerInterface: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Serializer/Serializer.php#L38

IMO the proper fix is to add a check that the given Serializer implements NormalizerInterface in PropertyNormalizer the same way it is done in ObjectNormalizer.

@dunglas dunglas self-assigned this Mar 15, 2015
@dunglas dunglas added Serializer Bug Good first issue Ideal for your first contribution! (some Symfony experience may be required) DX DX = Developer eXperience (anything that improves the experience of using Symfony) and removed Bug labels Mar 15, 2015
fabpot added a commit that referenced this issue Mar 17, 2015
This PR was merged into the 2.6 branch.

Discussion
----------

[Serializer] Fix object normalization exceptions

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

Commits
-------

500df47 [Serializer] Fix object normalization exceptions
@fabpot fabpot closed this as completed Mar 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Good first issue Ideal for your first contribution! (some Symfony experience may be required) Serializer
Projects
None yet
Development

No branches or pull requests

3 participants