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.
But instead, actual output has 2 Resource Metadata entries, one of which is populated with default null values.
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() ?
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 theORM\Entityattribute is placed before theAsResourceone, the output of the command will give us a second, falsy,ResourceMetadataentry populated with default values.Most likely,
AttributesResourceMetadataCollectionFactorycreates duplicateResourceMetadataentries when attributes other thanAsResource, including operation attributes such as#[Index],#[Create]etc come first.Steps to reproduce
Here is an example.
With the following Doctrine Entity and Resource :
When running :
Expected output should be a single "Resource Metadata" section with templatesDir and routePrefix populated like below.
But instead, actual output has 2 Resource Metadata entries, one of which is populated with default null values.
An easy 'fix' for the developer is simply to swap the attributes to make sure AsResource is top of the list like this :
But obviously there's an underlying issue that needs to be fixed.
Possible Solution
Skipping all attributes other than
#[AsResource]insrc/Component/src/Metadata/Resource/Factory/AttributesResourceMetadataCollectionFactory.phpinbuildResourceOperations()?