0

I am in the middle of upgrading a project from API 2.7 to API 3.0 and have trouble with this POST route

This is my request body:

[
  {
    "name": "firstName",
    "width": "10%",
    "priority": 1
  },
  {
    "name": "lastName",
    "width": "10%",
    "priority": 1
  }
]

The error output is:

 "@context": "/api/contexts/Error",
 "@type": "hydra:Error",
 "hydra:title": "An error occurred",
 "hydra:description": "ApiPlatform\\Serializer\\AbstractItemNormalizer::canAccessAttributePostDenormalize(): Argument #3 ($attribute) must be of type string, int given

I had a similar issue on another route and defining uriVariables solved it, but that did not work this time.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
fuqeba
  • 1
  • 1
  • I'm not sure about you exactly want, what :)), but for this step you can use `deserialize: false` . – Ali Yousefi Dec 19 '22 at 23:00
  • yeah you were right , kinda , I setted allow extra attributes to false in normalozation context and deserialize to false , and it works fine now – fuqeba Dec 26 '22 at 16:36

1 Answers1

0

For me the problem turned out to be that I was sending a plain array without a key prefix, not a property which was incorrect.

$client->request('PUT',  'some-url', [
    'body' => json_encode(['a', 'b', 'c'])
]);

That means the keys of the array are indexed (eg the [0], [1], ...), which seems to be the int from the "got int, expected string". For us the solution was to give it a key

$client->request('PUT',  'some-url', [
    'body' => json_encode(['example' => ['a', 'b', 'c']])
]);

This ment we had to update the code too, to also use that key, but that was a minimal amount of work.

Martijn
  • 15,791
  • 4
  • 36
  • 68