0

I have an entity User like this:

use SerendipityHQ\Component\ValueObjects\Email\Email;

#[Entity]
#[Table(name: 'users', schema: 'my_schema')]
#[API\ApiResource(
    openapiContext: ['tags' => [self::OPENAPI_TAG]],
    normalizationContext: [
        AbstractNormalizer::GROUPS                 => [self::GROUP_NOR_READ],
        AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
    ],
    denormalizationContext: [
        AbstractNormalizer::GROUPS                 => [self::GROUP_DENOR_WRITE],
        AbstractObjectNormalizer::ENABLE_MAX_DEPTH => true,
    ],
    operations: [
        new API\Get(
            uriTemplate: self::API_RESOURCE,
            uriVariables: [
                UserAware::USER_KEY => new API\Link(fromClass: self::class, identifiers: ['id']),
            ],
        ),
    ],
)]
class User implements UserInterface
{
    public const OPENAPI_TAG       = 'My App > User';
    public const API_ENDPOINT      = 'users';
    public const API_RESOURCE      = '/' . self::API_ENDPOINT . '/{' . UserAware::USER_KEY . '}';
    public const GROUP_NOR_READ    = UserAware::USER_KEY . ':read';
    public const GROUP_DENOR_WRITE = UserAware::USER_KEY . ':write';
    public const GROUP_VAL_POST    = UserAware::USER_KEY . ':post';
    public const API_AUTH_ENDPOINT = 'login';
    public const API_GROUP_JWT     = 'jwt';

    #[Id]
    #[Column(type: Types::INTEGER, unique: true)]
    #[GeneratedValue(strategy: 'IDENTITY')]
    #[Groups(self::API_GROUP_JWT, self::GROUP_NOR_READ)]
    protected int $id;

    #[Column(name: 'email', type: 'email', unique: true)]
    #[Groups(self::GROUP_NOR_READ)]
    private Email $email;
}

This is the object SerendipityHQ\Component\ValueObjects\Email\Email (simplified for question purposes):

final class Email implements EmailInterface
{
    private string $email;
    private string $mailBox;
    private string $host;

    ...
}

Now, when I GET the User entity via ApiPlatform, I want also the Email::$email property to be included in the payload.

How can I set serialization group User::GROUP_NOR_READ (that is the string user:read) for the external entity Email?

In fact, I cannot set attributes on the Email class and so I cannot set the serialization groups directly in it.

I should use Yaml for this, but I'm not able to find a proper configuration able to set serialization groups on external entities.

What I tried

resources:
   App\Entity\User:
        properties:
            email:
                subresource:
                    resourceClass: SerendipityHQ\Component\ValueObjects\Email
                    properties:
                        email:
                            groups: ['user:read']

Unfortunately, this configuration completely breaks the configuration set by attributes on the class User, as it activates all operations and completely ignores what I've configured via attributes.

Any ideas?

Aerendir
  • 6,152
  • 9
  • 55
  • 108

0 Answers0