core: Multiple Links to the same ApiResource in IRI doesn't work

(retrying here - originally posted in api-platform/api-platform)

API Platform version(s) affected: 3.0.8

Description
I’m trying to add an ApiResource for a relation between an ApiResource and itself, and i can’t get it to give me correct item IRIs

How to reproduce
Create a basic resource :

#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/resources/{id}',
            provider   : ResourceProvider::class //just returns a dummy Resource
        ),
    ]
)]
class Resource
{
    #[ApiProperty(identifier: true)]
    public int $id;

    public string $name;
}

Then another for the relation :

#[ApiResource(
    operations: [
        new Get(
            uriTemplate : '/resources/{first}/relations/{second}',
            uriVariables: [
                'first' => new Link(toProperty: 'first', fromClass: Resource::class),
                'second' => new Link(toProperty: 'second', fromClass: Resource::class),
            ],
        ),
        new GetCollection(
            uriTemplate : '/resources/{first}/relations',
            uriVariables: [
                'first' => new Link(toProperty: 'first', fromClass: Resource::class),
            ],
            provider    : RelationProvider::class //builds a dummy Relation[] array
        ),
    ]
)]
class Relation
{
    #[ApiProperty(identifier: true)]
    public Resource $first;
    #[ApiProperty(identifier: true)]
    public Resource $second;

    public string $something;
}

Then call /resources/1/relations. You’ll get something like that :

{
  "@context": "/contexts/Relation",
  "@id": "/resources/1/relations",
  "@type": "hydra:Collection",
  "hydra:member": [
    {
      "@id": "/resources/1/relations/1",
      "@type": "Relation",
      "first": "/resources/1",
      "second": "/resources/2",
      "something": "ok"
    }
  ],
  "hydra:totalItems": 1
}

Notice the ending id in @id is the one from “first”, not the one from “second”.

Possible Solution
No clue, i may just not understand this correctly. I tried all i could think of, but i either get this or “unable to create IRI for item…”.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20 (20 by maintainers)

Commits related to this issue

Most upvoted comments

thanks I’ll look into that