Fix return of stale data on PATCH of to-one relationship #247
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that PATCH requests made to relationship views with to-one relationships correctly update the underlying relationship with the passed data, but the response I get back is stale (it returns the original relationship instead of reflecting the new one).
This is only a problem with to-one relationships, but not to-many, as that code patch updates the
related_instance_or_manager
directly, while in the case of a to-one relationship it is returning a serialized version of the relationship it fetches in the beginning of the function.I have fixed this by refreshing the
related_instance_or_manager
at the end of the block that updates to-one relationships. I have additionally added asserts to the methods that test updating to-one and to-many relationships to make sure that the response data is equal to the data passed in through the request.As an aside, I'm not entirely sure if these updates should return a 200 (as they do now) or a 204. The spec has the following to say about the response codes when updating relationships:
My reading is that maybe this should be a 204 response as the new relationship is exactly the one that has been passed in through the PATCH request, and nothing else about the relationship has changed. That said, regardless of whether it should be a 204 or 200, it shouldn't be returning stale relationship data which is what this PR aims to fix.