Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Readable links is not working for arrays-backed collection #2957

@josuealcalde-utamed

Description

@josuealcalde-utamed

API Platform version(s) affected: 4.2.6

Description
Using symfony 7.3 and Api platform 4.2.6, in a ManyToMany collection with array backed setter and getter, readable links is not working.

I have an entity "Client" with a ManyToMany collection to other entity "Api" called exchangeApis.
I also have another ManyToOne collection between the same entities called "api".

I am trying to get them serialized as URI, and i have no problem with the ManyToOne: readableLink is working (in this case it is a readonly property for the api).

For the ManyToMany i use array-backed getter and setter, because when I use collections setter does not work. With this configuration I cannot make "readableLink: false" work and I am getting and embbeded object for the ManyToMany collection.

How to reproduce

[...]
#[ORM\ManyToOne(targetEntity: Api::class)]
#[ORM\JoinColumn(nullable: false)]
#[Assert\NotNull]
#[Groups([self::GROUP_DEFAULT])]
#[ApiProperty(readableLink: false, writableLink: false, example: '/api/apis/12345678-1234-5678-1234-567812345678')]
private Api $api;

/**
 * @var Collection<array-key, Api>
 */
#[ORM\ManyToMany(targetEntity: Api::class)]
#[ORM\JoinTable(name: 'clients__exchange_apis')]
private Collection $exchangeApisCollection;

[...]

public function getApi(): Api
{
    return $this->api;
}

/**
 * @return list<Api>
 */
#[Groups([self::GROUP_DEFAULT])]
#[ApiProperty(readableLink: false, writableLink: false)]
#[Context(normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
public function getExchangeApis(): array
{
    return array_values($this->exchangeApisCollection->toArray());
}

/**
 * @param list<Api> $apis
 */
public function setExchangeApis(array $apis): self
{
    $this->exchangeApisCollection->clear();
    foreach ($apis as $api) {
        $this->exchangeApisCollection->add($api);
    }

    return $this;
}

Posible solution
I am investigating the posibility to implement a custom serializer to get around.

Additional Context
If my getter returns a collections it works, but the setter fails when using collections.
Also, I prefer to mantain an array API since the number of APIs in the collection is low and it is simple to understand.

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