Skip to content

Allow multiple AsDecorator attributes on a decorating class #50228

Closed
@tjveldhuizen

Description

@tjveldhuizen

Description

I have a situation in API Platform, in which I want to decorate the api_platform.jsonld.normalizer.item and api_platform.hal.normalizer.item with the same decorator. In short, that decorator adds a property to the normalized item.

In #45834, in addition to the configuration of a decorator in the service definition, it's made possible to do this using a PHP attribute #[AsDecorator]. Unfortunately, it's not possible to repeat this attribute.

I've done a quick test making the attribute repeatable, but this results in the decorator applied to the first service being overwritten by the second one, I think because of the same reason as described here: the name of the decorating service is reused. Therefore, this feature requires more work than just making the attribute repeatable: generating (or demanding configuration) unique service names is required.

Example

<?php

namespace App\ApiPlatform\Normalizer;

use App\ApiPlatform\Attribute\HateoasLink;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerInterface;

#[AsDecorator('api_platform.jsonld.normalizer.item')]
#[AsDecorator('api_platform.hal.normalizer.item')]
class HateoasLinkNormalizer implements NormalizerInterface, SerializerAwareInterface
{
    public function __construct(
        private readonly NormalizerInterface $decorated,
        private readonly RouterInterface $router,
    ) {
    }

    // ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions