-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
fix(doctrine): denormalization of properties with embeds many that omit target document directive #4315
Conversation
…mit target document directive
Hello, |
Ok, I'll try my best to find my way in this |
@alanpoulain |
@@ -65,6 +65,10 @@ public function getTypes($class, $property, array $context = []) | |||
if ($metadata->hasAssociation($property)) { | |||
$class = $metadata->getAssociationTargetClass($property); | |||
|
|||
if (!\is_string($class) && null === $class) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some ugly attempt to please PHPStan and I don't know if this will work.
I'm not familiar with PHPStan so if someone knows how to fix it I'm all ears.
This problem here is the interface $metadata->getAssociationTargetClass
returns a string
in phpDoc, but implementation of it returns ?string
.
See https://github.com/doctrine/mongodb-odm/blob/ccf26d9e023387e9aae86951186c043b3222fe70/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php#L1797
and https://github.com/doctrine/persistence/blob/79e97bcb1dfe3b242961253d81b8357a04e96ca1/lib/Doctrine/Persistence/Mapping/ClassMetadata.php#L128
The only solution I can figure out is:
if (!\is_string($class) && null === $class) { | |
if ($metadata instanceof \Doctrine\ODM\MongoDB\Mapping\ClassMetadata && null === $class) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best way to solve this is to add a PHPDoc on the returned variable to force PHPStan to consider it as ?string
.
You can also launch PHPStan locally (vendor/bin/phpstan analyze .
) in order to verify if it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! It works.
Thanks.
Thank you @wuchen90. |
…it target document directive (api-platform#4315)
Hello, I have a use case where i need to store a document as a ReferenceOne, but it can be different types of documents.
So is there a workaround to that ? |
Hi @nass59 |
Hi @wuchen90, it works very well ;) Thanks |
…it target document directive (api-platform#4315)
According to doctrine/mongodb-odm docs, it's possible to embed many without declaring the
target-document
directive.In api-platform, if we omit this directive, the deserialization won't work.
This fix makes the type guesser of MongoDB return null so other guessers will be used.