Skip to content

Commit 956f988

Browse files
daxtensstephenfin
authored andcommitted
REST: Don't error if a versioned field we would remove is absent
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)
1 parent 6d159b1 commit 956f988

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

patchwork/api/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def to_representation(self, instance):
9696
# field was added, we drop it
9797
if not utils.has_version(request, version):
9898
for field in self.Meta.versioned_fields[version]:
99-
data.pop(field)
99+
# After a PATCH with an older API version, we may not see
100+
# these fields. If they don't exist, don't panic, return
101+
# (and then discard) None.
102+
data.pop(field, None)
100103

101104
return data

patchwork/tests/api/test_patch.py

+14
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,20 @@ def test_update_maintainer(self):
334334
self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
335335
self.assertIsNone(Patch.objects.get(id=patch.id).delegate)
336336

337+
def test_update_maintainer_version_1_0(self):
338+
"""Update patch as maintainer on v1.1."""
339+
project = create_project()
340+
patch = create_patch(project=project)
341+
state = create_state()
342+
user = create_maintainer(project)
343+
344+
self.client.force_authenticate(user=user)
345+
resp = self.client.patch(self.api_url(patch.id, version="1.1"),
346+
{'state': state.slug, 'delegate': user.id})
347+
self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
348+
self.assertEqual(Patch.objects.get(id=patch.id).state, state)
349+
self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)
350+
337351
@utils.store_samples('patch-update-error-bad-request')
338352
def test_update_invalid_state(self):
339353
"""Update patch with invalid fields.

0 commit comments

Comments
 (0)