-
Notifications
You must be signed in to change notification settings - Fork 39
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
Deserializing relationships to an object #65
Comments
The behaviour of serialization is that if the relationship's attributes are populated (not just an To keep this behaviour on deserialize, if the Maybe a test is missing to check if the included data only contains "resource identifier object" (only Exemple: { id: 1, name: 'randomname', roles: 1 } |
The reason why I am asking is because I would like to use it with ObjectionJS's graphInsert (https://vincit.github.io/objection.js/#graph-inserts). This requires the relation to be an array of resource identifier objects. Would it make sense to add a check to see if the relationship is an array of "resource identifier objects" on deserialization? (i.e. if it contains |
Each ORM is different and has specifics needs. What about having an option In such case every specifics needs should be code here... |
@mattiloh I think you have already worked with Objection.js. Any thougths ? Thanks |
I understand that the ORM implementation should not be the concern of this serializer. However, imo it does not make sense to deserialize a resource identifier object to a simple ID if it is not in This change already solves the 'problem': So the question is: what is the correct way to deserialize a relation? The serialize example in the README also shows this problem: the array of strings in photos and the array of objects in comments are both serialized to the same result (resource identifier objects). So I guess there should be an option to specify the deserialization such that one can get both results. Edit: |
Hi @stefanvanherwijnen! Regarding your proposal: I can see that it could make sense for to-many relationships, because they are normally saved as dedicated rows in other tables (at least in relational databases). But belongs-to-one relationships are normally saved as single ids on the resource itself (e.g. a If we would just apply your proposed changes, the serialization of all belongs-to-one relationships would break and needed a refactor. A has-one relationship on the other hand could work fine with your proposed changes. I don't have a definitive opinion on this yet (since I didn't have the time to dive deeper today), but after these first thoughts, I would be careful with hard-coding a behavior that is working fine with one specific ORM. I think @danivek's idea of a E.g. Serializer.register("user", {
...
relationships: {
team: {
type: 'teams',
deserializer: data => data,
}
}
}); |
@mattiloh Thanks for the feedback. |
Yes, I had a look at stefanvanherwijnen@6e09d8f. Serializer.register("user", {
relationships: {
team: {
type: 'teams',
deserialize: data => ({ id: data.id })
}
}
}); It allows you to do what you need without being opinionated about the default output for objects. I think it's possible that somebody else in the future would like the deserialized object to look different from your proposal. Do you think that would solve your problem? At @danivek, what's your opinion on this? You imagined a |
Thanks, I think this would be a good solution. It should solve my problem and the added flexibility is indeed a better choice. |
I agree with @mattiloh for having a more flexible API. I imagined a Anyway, |
This seems to work: With regards to a |
@stefanvanherwijnen It's ok for me. |
The default behaviour of deserialize() seems to be to return an array of relationships keys. If the top-level key "included" is set, it returns the relationships as an object with an 'id' key however.
For example:
deserializes to
{ id: 1, name: 'randomname', roles: 1 }
.deserializes to:
{ id: 1, name: 'randomname', roles: { id: 1 } }
Now, I would like to get the second result, but the JSON API spec doesn't seem to require the "included" key on updates: https://jsonapi.org/format/#crud-updating-resource-relationships
So I am wondering, is there a specific reason why the result of deserialization differs when the "included" key exists in the request?
The text was updated successfully, but these errors were encountered: