Skip to content

Cannot unserialize properties imported from other namespace in docblock #59650

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
fritz-gerneth opened this issue Jan 29, 2025 · 1 comment
Closed

Comments

@fritz-gerneth
Copy link

Symfony version(s) affected

7.2.x

Description

Trying to deserialize some DTO that receives an enum-array as parameter in the constructor. That enum is defined in a different namespace then the DTO. The type is annotated in the doc block.

How to reproduce

Setup

namespace App\AuthServer\Aggregate\AuthApp;

use App\AuthServer\Entity\OAuth\Scope;

class ClientCreated
{
    /**
     * @param Scope[] $scopes
     */
    public function __construct(
        public readonly array $scopes,
    ) {}
}
namespace App\AuthServer\Entity\OAuth;

enum Scope: string
{
    case OPENID = 'openid';
}

Call

$data = '{"scopes": ["openid"]}'
serializer->deserialize($data, ClientCreated::class, 'json');

Different variations fail as well (e.g. list<Scope>).

Possible Solution

I traced this down to the StringTypeResolver / TypeContext:

Image

The use is not respected here, and therefore this returns the wrong full class of App\AuthServer\Aggregate\AuthApp\Scope

Additional Context

Using the FQCN in the docblock works:

class ClientCreated
{
    /**
     * @param \App\AuthServer\Entity\OAuth\Scope[] $scopes
     */
    public function __construct(
        public readonly array $scopes,
    ) {}
}
@mtarld
Copy link
Contributor

mtarld commented Feb 3, 2025

Hey @fritz-gerneth, thanks for the report!

I think that it is rather linked to the fact that the property is promoted. Indeed, actually, promoted property's PHPDoc was unexpectedly ignored. This PR should fix it.

xabbuh added a commit that referenced this issue Feb 4, 2025
This PR was merged into the 7.2 branch.

Discussion
----------

[TypeInfo] Fix promoted property phpdoc reading

| Q             | A
| ------------- | ---
| Branch?       | 7.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #59650
| License       | MIT

The read of promoted property's PHPDoc was missing in `PhpDocAwareReflectionTypeResolver`.
This PR adds it.

Commits
-------

90dc4ee [TypeInfo] Fix promoted property phpdoc reading
@xabbuh xabbuh closed this as completed Feb 4, 2025
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

4 participants