mysiar
mysiar

Reputation: 484

API-PLATFORM - model with object serialized to string at endpoint

I faced the problem with generating React components with api-platform-generate-crud.
Model has property that is object email. I have serializer that make my email object a string. API endpoint is serving string.

It works for GET & POST.
When I try to generate React components error message is

    TypeError: Cannot read property '0' of undefined

Looking deeper into it, looks like that generator still see my email as object not a string.

Any idea how I can force API to 'see' email property as string not object ?

Upvotes: 1

Views: 850

Answers (1)

Kévin Dunglas
Kévin Dunglas

Reputation: 3024

The data model you define is authoritative. Types in the Hydra documentation reflect the ones in PHP classes. Here, the email property is of type object. If you set the related data as a string somewhere, you don't respect this contract anymore. The Hydra documentation is not in sync with the returned data.

You can change the type of the email property in the Hydra documentation by decorating the api_platform.hydra.normalizer.documentation service.

But I would recommend to keep the PHP classes' structure of your entities as close as possible of the structure exposed through the API. Your classes should reflect the output of the API. You can use custom data providers to deal with more complex data structure (ex: ORM entities) before hydrating the structure to expose.

Upvotes: 2

Related Questions