Skip to content

[Serializer] Allow using generic annotations and not parsing them as an array #41996

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
awd-studio opened this issue Jul 5, 2021 · 14 comments
Closed

Comments

@awd-studio
Copy link

Symfony version(s) affected: 5.3.3

Description
Denormalizer breaks with an error Data expected to be an array, string given. when it handles an object, which contains a generic-like annotation, but is not an array or iterable.

How to reproduce

interface Foo {}

/** @template TFoo of Foo */
interface Bar {}

final class FooBar {
    /** @var Bar<Foo> */
    public Bar $bar; // This prop will fail
    // ...
}

Possible Solution
I digged it in PhpDocTypeHelper, which is part of phpDocumentor, so I don't know how to solve it. 😟

@xabbuh
Copy link
Member

xabbuh commented Jul 7, 2021

Can you create a small example application that allows to reproduce your issue?

@awd-studio
Copy link
Author

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@awd-studio
Copy link
Author

As I know, it steal can be represented.
I'm using psalm-related syntax to describe values for such cases.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@simPod
Copy link
Contributor

simPod commented Jul 12, 2022

.

@carsonbot carsonbot removed the Stalled label Jul 12, 2022
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@simPod
Copy link
Contributor

simPod commented Mar 2, 2023

ds

@carsonbot carsonbot removed the Stalled label Mar 2, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@simPod
Copy link
Contributor

simPod commented Sep 22, 2023

answer

@carsonbot carsonbot removed the Stalled label Sep 22, 2023
@mtarld
Copy link
Contributor

mtarld commented Nov 23, 2023

I think it is because PhpDoctExtractor and PhpStanExtractor do not support generics, therefore, they interpret that type as a collection.

This will be fixed by the introduction of the TypeInfo component, but I think that we still need to implement a fix for older versions.

@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

fabpot added a commit that referenced this issue Jun 9, 2024
…rface generics (mtarld)

This PR was merged into the 5.4 branch.

Discussion
----------

[Serializer] [PropertyAccessor] Ignore non-collection interface generics

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #41996 #49924
| License       | MIT

`PhpDocExtractor` and `PhpStanDocExtractor` are following an old version of PSR-5 with used to define collections as the following:
```
generic = collection-type "<" [type-expression "," *SP] type-expression ">"
```
But, it does conflict with non-collection generics.

This issue will automatically be solved if the `TypeInfo` is merged in Symfony. But for older versions (<7.1 at least), it needs a fix.

~I was not able to find a proper bug fix without introducing a BC break, so this PR proposes to opt-on the "non-collection generics" parsing, such as `stcClass<int>` for example.~

~To opt-out from parsing these generics, it'll be required to set `ignore_docblock_generics` in the context. And this key/value will become obsolete as soon as the `TypeInfo` is introduced.~

~I'm not sure how to proceed though, should we only merge it in 5.4, and 6.3 supposing that the `TypeInfo` might be merged in 7.x? Should we document it only in these branches?~

EDIT: I finally ignored PHPDoc generics that aren't well known collection generic types so that the process will fall back to other resolvers (such as reflection resolver for example)

Commits
-------

e31aeeb [PropertyAccessor] Fix unexpected collection when generics
@fabpot fabpot closed this as completed Jun 9, 2024
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

6 participants