Skip to content

[Bug][Debug] Duplicated "default" ResourceMetadata when another attribute precedes #[AsResource] #1162

@stlgaits

Description

@stlgaits

Sylius ResourceBundle version affected: spotted on v1.14.1

Description
When running the sylius:debug:resource <resourceName> command with an Entity that has both #[ORM\Entity] and #[AsResource] attributes, the generated output is not the same depending on the order of the attributes. Namely, if the ORM\Entity attribute is placed before the AsResource one, the output of the command will give us a second, falsy, ResourceMetadata entry populated with default values.

Most likely, AttributesResourceMetadataCollectionFactory creates duplicate ResourceMetadata entries when attributes other than AsResource, including operation attributes such as #[Index], #[Create] etc come first.

⚠️ The issue seems to happen with any attribute (not just Doctrine).

Steps to reproduce
Here is an example.

With the following Doctrine Entity and Resource :

use Doctrine\ORM\Mapping as ORM;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;

#[ORM\Entity(repositoryClass: PlantRepository::class)]
#[AsResource(
    templatesDir: '@SyliusAdminUi/crud',
    routePrefix: 'admin',
)]
class Plant implements ResourceInterface {}

When running :

php bin/console sylius:debug:resource app.plant

Expected output should be a single "Resource Metadata" section with templatesDir and routePrefix populated like below.

Image

But instead, actual output has 2 Resource Metadata entries, one of which is populated with default null values.

Image

An easy 'fix' for the developer is simply to swap the attributes to make sure AsResource is top of the list like this :

use Doctrine\ORM\Mapping as ORM;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Model\ResourceInterface;

#[AsResource(
    templatesDir: '@SyliusAdminUi/crud',
    routePrefix: 'admin',
)]
#[ORM\Entity(repositoryClass: PlantRepository::class)]
class Plant  implements ResourceInterface

But obviously there's an underlying issue that needs to be fixed.

Possible Solution
Skipping all attributes other than #[AsResource] in src/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.php in buildResourceOperations() ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugConfirmed bugs or bugfixes.

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions