-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Force link relations to an array for HAL #199
Comments
It is also better to return an empty array when there is no links. I am not really sure how to address the issue, given that some links are added at runtime |
Would you not be happy if we converted the relations to an array as soon as we got any Relation that said it was an array item? This would mean that adding a relation at runtime as an array item would convert the serialized representation to an array. Empty arrays would be possible, but the restriction of a relation requiring an embedded or a href would have to be removed. |
👍. I believe there's the same issue with CollectionRepresentation/PaginatedRepresentation (probably because they also create relations). |
I solved this for our own requirements in our codebase by extending the JsonHalSerializer class and checking whether the relationship name contains "[]" at the end. This requires modification to all other serializers being used though (For example, the XML serializer needs to remove the "[]" from the relationship name). It also doesn't advertise empty arrays when no links are provided. Example:
A hacky solution though :( |
I'm running into this too - sometimes the relation only has a single entry, and then we get an object, but it's a link that has the capacity for multiple entries. This means our callers need to check which it is and handle both cases. That doesn't seem right. |
There's a note in the HAL specification located at https://stateless.co/hal_specification.html:
At the moment, serializeLinks and serializeEmbeddeds only serialize links/items to an array when there is more than one. With the application I'm working on it makes sense to force certain relations to always be an array (for example: a user can have many role relationships, but sometimes might only have one role).
It doesn't appear there's a way to currently do this, or easily do it without either passing some kind of configuration information into the serializeLinks/serializeEmbeddeds methods, or attach it to Model\Link and Model\Embedded classes.
I'm happy to implement this if you can provide some direction as to how you'd want it to look. My preference at the moment would be to maybe change the constructor for Link and Embedded to include an array $options that we can populate with configurables that the serializers can use.
The text was updated successfully, but these errors were encountered: