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] Extract types of constructor parameters from docblock comment #25524

Closed
sergeyfedotov opened this issue Dec 17, 2017 · 8 comments
Closed

Comments

@sergeyfedotov
Copy link
Contributor

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version ?

Currenly PhpDocExtractor works only with class properties. It's not possible to clarify the type of the constructor parameter through docblock comment.

class A
{
    private $b;

    /**
     * @param B[] $b
     */
    public function __construct(array $b)
    {
        $this->b = $b;
    }

    public function getB()
    {
        return $this->b;
    }
}

class B
{
    private $c;

    public function __construct($c)
    {
        $this->c = $c;
    }

    public function getC()
    {
        return $this->c;
    }
}

$a = $serializer->denormalize(['b' => [['c' => 1], ['c' => 2], ['c' => 3]]], A::class);
print_r($a);

Expected result:

App\A Object
(
    [b:App\A:private] => Array
        (
            [0] => App\B Object
                (
                    [c:App\B:private] => 1
                )
            [1] => App\B Object
                (
                    [c:App\B:private] => 2
                )
            [2] => App\B Object
                (
                    [c:App\B:private] => 3
                )
        )
)

Actual result:

App\A Object
(
    [b:App\A:private] => Array
        (
            [0] => Array
                (
                    [c] => 1
                )
            [1] => Array
                (
                    [c] => 2
                )
            [2] => Array
                (
                    [c] => 3
                )
        )
)
@nicolas-grekas
Copy link
Member

@lyrixx maybe something you'd like to do since you're in that kind of business these days? :)

@lyrixx
Copy link
Member

lyrixx commented Dec 28, 2017

Ahah, Good try ;)

I'm currently moving an API to api platform and I'm hitting E_TOO_MANY_BUGS (symfony, flex/recipes, api-platform/core and api-platform/docs)

So I'm sorry but I think I will not be able to implement this feature

@xabbuh
Copy link
Member

xabbuh commented Dec 31, 2017

Isn't this related to #25605?

@sergeyfedotov
Copy link
Contributor Author

@xabbuh AbstractNormalizer already extracts this kind of information through reflection without the PropertyInfo component. It uses the extracted types to instantiate an object. The change proposed in the #25605 doesn't affect the current behaviour.

@Simperfit
Copy link
Contributor

@dunglas any thoughs on how to fix this ?

@dvc
Copy link

dvc commented May 31, 2019

I found in one normalizer test:

        $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
        $normalizer = new ObjectNormalizer(null, null, null, $extractor);

Using PhpDocExtractor, activates reading blocks like:

    /**
     * @param B[] $b
     */

It supports after 3.4 (#29513)

Only update documentation required

@xabbuh xabbuh closed this as completed Jun 1, 2019
@piotrooo
Copy link

@dvc day-saver thx! :)

@CaliforniaMountainSnake

Just some link that I was inspired by:
https://medium.com/@rebolon/the-symfony-serializer-a-great-but-complex-component-fbc09baa65a0

My finally serializer that can successfully deserialize nested arrays of objects is:

$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
$converter = new CamelCaseToSnakeCaseNameConverter();
$normalizers = [
    new DateTimeNormalizer(),
    new ArrayDenormalizer(),
    new ObjectNormalizer(null, $converter, null, $extractor),
];
$encoders = [new JsonEncoder()];
return new Serializer($normalizers, $encoders);

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

No branches or pull requests

8 participants