API Platform custom IRI with value objects

1.7k views Asked by At

I am currently trying to create a custom IRI for one of my entities in API Platform. I know there is page in the documentation describing how to use a custom IRI (https://api-platform.com/docs/core/identifiers/), but I can't get it working.

My entity uses a value object for the id (currently used for IRI) and also for the name (should be used for IRI). But the values themself are priviate and scalar in the entity.

API Platform seems to get the information what should be used as the identifier, from my XML Doctrine mapping. I already tried to overwrite it by usung annotations, attributues and YAML definitions. Without luck.

The returned error reads:

preg_match(): Argument #2 ($subject) must be of type string

(at this point it receives the value object instead of the actual value)

best regards, spigandromeda

1

There are 1 answers

0
SpigAndromeda On

I solved my problem.

To explain the solution, I have to dig a little into API Platform response generation.

  • API platform generates an IRI for every entity it returns (colelction and item operation)
  • it's using the Symfony router go generate the URI
  • all the necessary information can draw API Platform from different sources (YAML, XML, annotations, attributes)
  • the information include the identifier(s) defined for the entities resource
  • API Platform gets the value for the identifier via Symfony property accessor
  • because the property accessor is using getters before accessing private properties via reflection, it will return the VO
  • an ordinary VO cannot be used by the Symfony URL generator to create the URL

As I explained, I am using a VO for my Id as well. So I tried to figure out why it was working with the Id VO but not with the name VO.

Simple answer: the Id VO implemented the __toString method and the name VO didn't. So the solution was to let the name VO implement this method as well.

It was interesing to dig into the internal process of API Platform, but I also feel a little stupid :D