-
-
Notifications
You must be signed in to change notification settings - Fork 586
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
How to pass MetadataFactory::create() into SerializationVisitorInterface::startVisitingObject()? #1226
Comments
hmmm... did not get it. :-( can you explain your goal rather than the technical consequence of it? |
I'm trying to create class metadata so I can pass them to the visitor. For that I'm using metadata factory. But it does not return compatible type to be passed into I need to pass Currently what I'm doing is that I have to assert the type to be safe, because I'm not guaranteed by the MetadataFactory I'll receive $classMetadata = $context->getMetadataFactory()->getMetadataForClass(My::class);
Assert::isInstanceOf($classMetadata, \JMS\Serializer\Metadata\ClassMetadata::class);
$visitor->startVisitingObject($classMetadata, $entity, ['name' => get_class($entity)], $context); So I'm looking for a proper way to get something like interface MetadataFactoryInterface
{
public function getMetadataForClass(string $className) : ?\JMS\Serializer\Metadata\ClassMetadata; That said to receive ClassMetadata instance that I can safely pass into a visitor. Passing |
|
The null is not issue. The issue is that it returns |
did you customize |
Yes, it gives correct type actually. When executed, it returns I'd like to rely on the interfaces and these two are incompatible: MetadataFactoryInterface::getMetadataForClass(...) : ClassHierarchyMetadata|MergeableClassMetadata|null;
SerializationVisitorInterface:: startVisitingObject(\JMS\Serializer\Metadata\ClassMetadata, ...); so it works but it's not correct in terms of interface type compatibility. I cannot pass return type |
well, I'm aware that this library is not fully type safe. Adding phpstan/psalm is a lot of work (and making it type-safe would require BC breaks for sure). Is one day someone will be interested to work on adding static analysis, that would be nice! |
Ah ok, I was wondering whether I'm doing something wrong or not. Thanks! |
I'm creating metadata for class through
$classMetadata = $context->getMetadataFactory()->getMetadataForClass(My::class);
So the return type is
ClassHierarchyMetadata|MergeableClassMetadata|null
Though
SerializationVisitorInterface::startVisitingObject()
expectsClassMetadata
which is not contravariant to whatMetadataFactoryInterface::getMetadataForClass()
returns.How to properly create those metadata and pass them then?
The text was updated successfully, but these errors were encountered: