Closed
Description
Description
I faced a stupid issue this morning. I have an API with a new property called metadata
. It's an array in PHP, and it's stored as JSON in our DB. We can store whatever we want in it.
Fore example: {"hidePinCrawlExplanation":true}
.
When the property is empty, its value is []
(in PHP or JSON).
But, in JS, I have an issue. If I write the following code:
user.metadata.hidePinCrawlExplanation = true;
I end up with the following "shape":
As you can see, this does not make any sens:
- it's an array
- but is has key
So when I serialized it back to JSON I got: metadata: []
🤦🏼
Summary in one image:
(Thanks JS )
So an idea (from @pyrech): Add an annotation to the property to tell the serializer to serialize it as {}
and not []
when the collection is empty.
Example
/**
* @ORM\Column(type="json_array")
* @Groups({"user:read", "user:edit_metadata"})
* @SerializeEmptyCollectionAsObject()
*/
private array $metadata;
/cc @dunglas
Metadata
Metadata
Assignees
Labels
RFC = Request For Comments (proposals about features that you want to be discussed)RFC = Request For Comments (proposals about features that you want to be discussed)