Skip to content

Mutliple embedded values #342

@flohw

Description

@flohw

Hi,

I am consuming a rest api (created by me) which returns a paginated representation, embedding a collection representation.
I also need some statistics about that pagination, on the same page.
Until now I tried the "dumb" version: one api call for the pagination and another for the statistics.

I would like to reduce the number of api calls. So I was wondering if there is a way to add more than one embedded relation with the bundle.

I will pass the details of my research but it appears that the yaml configuration I provide for my new CollectionWithStatisticsRepresentation does not work and I don't understand why...

The representation looks like this:

class CollectionWithStatisticsRepresentation extends CollectionRepresentation
{
    public function __construct($resources, private readonly array $statistics)
    {
        parent::__construct($resources);
    }

    public function getStatistics(): array
    {
        return $this->statistics;
    }
}

Some context: I use custom groups for serialization and I created a specific one for Hateoas. So I override the jms serialization provided by the bundle in the annotations/attributes and relations into my own yaml config files. I have CollectionRepresentation for instance:

Hateoas\Representation\CollectionRepresentation:
    exclusion_policy: all
    exclude: false
    relations:
        - rel: items
          embedded:
            content: "expr(object.getResources())"
          exclusion:
            groups: [Default, Hateoas]

Which is working correctly on this type of representation as my serialization tells to serialize the Hateoas group. The logic is inverted for some reason I don't understand too, but it works as I expect.

So I created a new configuration for my CollectionWithStatisticsRepresentation (which extends the Hateoas CollectionRepresentation):

App\Infrastructure\Hateoas\Representation\CollectionWithStatisticsRepresentation:
    exclusion_policy: all
    exclude: false
    relations:
        - rel: items
          embedded:
            content: "expr(object.getStatistics())"
          exclusion:
            groups: [Default, Hateoas]

But I don't get my new embedded statistics serialized property in my response... I checked via symfony profiler if my file is loaded properly by JMS and it is.

One of the working things is to add annotation/attributes on at my representation class level but for consistency of my large code base I would like to be able to configure it via yaml.

#[Serializer\Exclude(if: false)]
#[Serializer\ExclusionPolicy(policy: Serializer\ExclusionPolicy::ALL)]
#[Hateoas\Relation(
    name: 'statistics',
    embedded: new Hateoas\Embedded(content: 'expr(object.statistics)'),
    exclusion: new Hateoas\Exclusion(groups: ['Default', 'Hateoas'])
)]
// ...

Is there something I miss? Is it even achievable?

Thank you for your help

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions