core icon indicating copy to clipboard operation
core copied to clipboard

Interfaced object relations do not resolve JsonLd inheriting resource types

Open ambroisemaupate opened this issue 1 year ago • 3 comments

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.

interface CommonResource {

}

#[ApiResource( types: [ 'https://schema.org/Thing' ] )]
class MyResource implements CommonResource {

}

class MyDto {
    #[ApiProperty]
    public ?CommonResource $resource = null;
}


$dto = (new MyDto())->resource = new MyResource();

Actual output:

{
    "@type": "MyDto",
    "resource": {
        "@type": "MyResource"
    }
}

Expected output:

{
    "@type": "MyDto",
    "resource": {
        "@type": "https://schema.org/Thing"
    }
}

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 interface CommonResource which is not an ApiResource.

$resourceClass = $this->getObjectClass($object); // Here : MyResource
$previousResourceClass = $context['resource_class'] ?? null; // Here: CommonResource

So this condition cannot be true (line 84), and resource is normalized as an anonymous resource:

if($isResourceClass = $this->resourceClassResolver->isResourceClass($resourceClass) && (null === $previousResourceClass || $this->resourceClassResolver->isResourceClass($previousResourceClass)))
{
    //...
}

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.

ambroisemaupate avatar Mar 27 '24 12:03 ambroisemaupate

use #[ApiProperty(iris: ['https://schema.org/Thing'])]

soyuka avatar Mar 29 '24 16:03 soyuka

use #[ApiProperty(iris: ['https://schema.org/Thing'])]

That's not possible as we want polymorphism here.

public ?CommonResource $resource = null;

will accept any CommonResource sub-class, and each sub-class may have a different iris configuration.

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();

ambroisemaupate avatar Apr 02 '24 08:04 ambroisemaupate

I see I'd just declare everything as resources with the NotExposed metadata attribute. I'm :+1: for the suggested change, would you be able to make the patch?

soyuka avatar Apr 02 '24 20:04 soyuka

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 01 '24 22:06 stale[bot]