Skip to content
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

Open
ambroisemaupate opened this issue Mar 27, 2024 · 3 comments · May be fixed by #6284
Open

Interfaced object relations do not resolve JsonLd inheriting resource types #6251

ambroisemaupate opened this issue Mar 27, 2024 · 3 comments · May be fixed by #6284

Comments

@ambroisemaupate
Copy link
Contributor

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 ambroisemaupate changed the title Interfaced object relations does not resolve JsonLd inheriting resource types Interfaced object relations do not resolve JsonLd inheriting resource types Mar 27, 2024
@soyuka
Copy link
Member

soyuka commented Mar 29, 2024

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

@ambroisemaupate
Copy link
Contributor Author

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

@soyuka
Copy link
Member

soyuka commented Apr 2, 2024

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

ambroisemaupate added a commit to ambroisemaupate/api-platform-core that referenced this issue Apr 3, 2024
ambroisemaupate added a commit to ambroisemaupate/api-platform-core that referenced this issue Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants