Skip to content

Commit

Permalink
REST: Don't error if a versioned field we would remove is absent
Browse files Browse the repository at this point in the history
We remove fields that shouldn't be seen on old versions of the API.
This was done with `pop(field name)`, which will throw an exception
if the named field is absent from the data. However, sometimes if
a patch request is via an old API version, we hit this line without
ever having the field present.

This is odd, but not harmful and we definitely shouldn't 500.

Signed-off-by: Daniel Axtens <[email protected]>
[stephenfin: Merge test into bug fix]
Signed-off-by: Stephen Finucane <[email protected]>
Tested-by: Konstantin Ryabitsev <[email protected]>
Fixes: d944f17 ("REST: Use versioning for modified responses")
Closes: #423
(cherry picked from commit 5c22f9d)
  • Loading branch information
daxtens authored and stephenfin committed Oct 27, 2021
1 parent 6d159b1 commit 956f988
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion patchwork/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def to_representation(self, instance):
# field was added, we drop it
if not utils.has_version(request, version):
for field in self.Meta.versioned_fields[version]:
data.pop(field)
# After a PATCH with an older API version, we may not see
# these fields. If they don't exist, don't panic, return
# (and then discard) None.
data.pop(field, None)

return data
14 changes: 14 additions & 0 deletions patchwork/tests/api/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ def test_update_maintainer(self):
self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
self.assertIsNone(Patch.objects.get(id=patch.id).delegate)

def test_update_maintainer_version_1_0(self):
"""Update patch as maintainer on v1.1."""
project = create_project()
patch = create_patch(project=project)
state = create_state()
user = create_maintainer(project)

self.client.force_authenticate(user=user)
resp = self.client.patch(self.api_url(patch.id, version="1.1"),
{'state': state.slug, 'delegate': user.id})
self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
self.assertEqual(Patch.objects.get(id=patch.id).state, state)
self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)

@utils.store_samples('patch-update-error-bad-request')
def test_update_invalid_state(self):
"""Update patch with invalid fields.
Expand Down

0 comments on commit 956f988

Please sign in to comment.