phpsymfonydeserialization

Use Symfonys MapRequestPayload to deserialize array of int


The payload for an API endpoint in my application is an array of numbers. Is it possible to deserialize it using MapRequestPayload?

What I tried so far isn't obviously working, since the deserializer used by MapRequestPayload gets its target type from the parameter type (which cant be something like int[]):

...
public function endpoint(
  #[MapRequestPayload] array $listOfIds
) {
...

Solution

  • I know this question is already a few months old, but I will answer it anyway, in case someone gets here looking for a solution.

    If your payload is an array, you can provide the type property in MapRequestPayload like the following:

    public function endpoint(
      #[MapRequestPayload(type: int)] array $listOfIds
    ) {