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
Interfaced object relations do not resolve JsonLd inheriting resource types #6251
Comments
use |
That's not possible as we want polymorphism here. public ?CommonResource $resource = null; will accept any interface CommonResource {
}
#[ApiResource( types: [ 'https://schema.org/Thing' ] )]
class MyResource implements CommonResource {
}
#[ApiResource( types: [ 'https://schema.org/WebPage' ] )]
class MySecondResource implements CommonResource {
}
class MyDto {
#[ApiProperty]
public ?CommonResource $resource = null;
}
// https://schema.org/Thing
$dto = (new MyDto())->resource = new MyResource();
// https://schema.org/WebPage
$dto2 = (new MyDto())->resource = new MySecondResource(); |
I see I'd just declare everything as resources with the |
API Platform version(s) affected: 3.2.18
Description
If a DTO expose a property with a API Resource but this property is typed using an Interface to allow polymorphism: Api Platform won't be able to resolve the final resource'
types
and will serialize the short className in@type
for JsonLd format.Actual output:
Expected output:
Possible cause : \ApiPlatform\JsonLd\Serializer\ItemNormalizer::normalize (line 84)
I think that's because in
\ApiPlatform\JsonLd\Serializer\ItemNormalizer::normalize
,$context['resource_class']
is populated with the interfaceCommonResource
which is not anApiResource
.So this condition cannot be true (line 84), and resource is normalized as an anonymous resource:
Possible solution:
We should test if
$previousResourceClass
is an interface or abstract class and$resourceClass
is an instanceof$previousResourceClass
then build context against$resourceClass
, not$previousResourceClass
.The text was updated successfully, but these errors were encountered: