Skip to content

Patching {"data": null} on To-One relationship fails #46

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

Open
tpansino opened this issue Mar 8, 2019 · 7 comments
Open

Patching {"data": null} on To-One relationship fails #46

tpansino opened this issue Mar 8, 2019 · 7 comments

Comments

@tpansino
Copy link
Contributor

tpansino commented Mar 8, 2019

Describe the bug
See https://jsonapi.org/format/#crud-updating-to-one-relationships

Specifically, this case:

PATCH /articles/1/relationships/author HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": null
}

Isn't working properly. The code seems to be expecting the data: object to have a type, so I think it needs to be updated to skip this check in the event we're patching a To-One relationship and the passed value is null. (The relationship ForeignKey or OneToOneField would also need to be nullable, but I have a feeling that's beyond the scope of what this package cares to check and is probably handled by Django at a lower level. Wouldn't hurt to test though to be sure...)

To Reproduce
Define two models, one with a ForeignKey linking it to the other, and two corresponding endpoints with a relationship endpoint between the resources. Attempt to PATCH {"data": null} on the To-One side of the relationship endpoint, observe an Exception is thrown.

Expected behavior
The response should be a 204 No Content code with the relationship successfully nullified.

@gargrave
Copy link

gargrave commented Apr 5, 2019

@tpansino and I already discussed this IRL, but I just wanted to leave a comment and mention that we have run into this issue in https://github.com/Vacasa/housekeeping-web, where I cannot clear an existing to-one relationship by sending { data: null } as describe in the spec.

Technically, we can work around this by making a DELETE request on the relationship from the opposite direction, but that is far from ideal, and is a bit confusing.

If it's possible to make any progress on this issue, it would be extremely helpful, because I imagine we will continue running into this as the API grows and we consume more endpoints.

@tpansino
Copy link
Contributor Author

tpansino commented Apr 5, 2019

@gargrave For completeness - can you also try PATCHing { data: [] } on the opposite direction? Does that work? If not then I have 2 bugs to fix 😄

Edit: Whoops, I meant [], not null.

@gargrave
Copy link

gargrave commented Apr 5, 2019

@tpansino Hopefully I am understanding correctly--the other direction is a to-many, so rather than taking null to clear the relationship(s), it takes an empty array. That indeed does work.

If I completely missed your point, feel free to give me a shout. 👍

@tpansino
Copy link
Contributor Author

tpansino commented Apr 5, 2019

Yep, that's correct, sorry for the confusion and thanks for checking.

@tpansino tpansino changed the title Patching {"data": null} on To-One relationship fails Patching {"data": <anything>} on To-One relationship fails Apr 19, 2019
@tpansino
Copy link
Contributor Author

tpansino commented Apr 19, 2019

After some digging, I've determined that this is a regression that happened during #29, specifically this line: https://github.com/Vacasa/drf-jsonapi/pull/32/files#diff-9a2180fee48df47db205fc59be17fa76L380

By removing the resource.save(), any To-One relationship change never actually gets committed to the database. That applies both to null as well as any real resource identifier object.

A better place to put this line would be right after the RelationshipHandler sets the To-One relationship in it's set_related method. I'll open a PR for that.

@tpansino tpansino changed the title Patching {"data": <anything>} on To-One relationship fails Patching {"data": null} on To-One relationship fails Apr 19, 2019
@tpansino
Copy link
Contributor Author

I just realized I'm conflating two unrelated issues here. The original reason I opened this Issue was because PATCHing {"data": null} on a To-One fails. That's still the case even if we fix the resource.save() line I mentioned in my previous comment.

So these are related issues, but have different fixes. I'll open another issue.

@tpansino
Copy link
Contributor Author

Needs to also be fixed on master, reopening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants