Deduplicate resource objects in includeds#136
Conversation
|
There's something unclear about this. MapSet should take care of duplication automatically. So:
iex(1)> a = MapSet.new([%{type: "a", id: 1}])
MapSet.new([%{id: 1, type: "a"}])
iex(3)> MapSet.put(a, %{id: 1, type: "a"})
MapSet.new([%{id: 1, type: "a"}])
iex(5)> MapSet.union(a, a)
MapSet.new([%{id: 1, type: "a"}])
@treere ideas? |
IIUC, the root cause would be in the I think that's iex(1)> a = MapSet.new([%{type: "a", id: 1}])
MapSet.new([%{id: 1, type: "a"}])
iex(2)> b = MapSet.new([%{type: "a", id: 1, relationships: %{"company" => :loaded}}])
MapSet.new([%{id: 1, type: "a", relationships: %{"company" => :loaded}}])
iex(3)> MapSet.union(a, b)
MapSet.new([
%{id: 1, type: "a"},
%{id: 1, type: "a", relationships: %{"company" => :loaded}}
])
I've tried implementing a solution with this approach, let me know if you had something different in mind. Thank you! |
lucacorti
left a comment
There was a problem hiding this comment.
Ok, so this only happens on many relationships when loaded relationships differ from relationships for an already selected include from a different include path. It should not be necessary to request the same include twice with two different paths. However, I see this can be a problem if you do.
|
@ivan-sibilla there's an unused function. If you can fix it and test the changes work as intended for you we can merge and release an updated version |
|
@lucacorti Removed the unused function and double checked that this should fix the problem we encountered. Thank you 🙏 |
|
@lucacorti Would you mind merging yourself? I don't have merge permissions. |
|
@ivan-sibilla done, if you can test this against main in production I can then release a patch version. |
|
@lucacorti Tested in production against the current revision on main, and the fix is working as intended; I think we can proceed with the patch version. Thank you so much! |
|
@ivan-sibilla 2.0.4 is out including your changes. Thanks ❤️ |
A resource reachable through multiple
includepaths gets serialized once per path. When those paths load different relationships (e.g.author.companyloads the user'scompanybutcomments.userdoes not) the variants differ, so accumulating them in aMapSetkeeps all of them.The result is more than one resource object for the same
typeandidinincluded, which violates the JSON:API compound documents rule:This PR makes sure that after building
included, we collapse entries bytype+id, merging their attributes and relationships into a single object.