[PATCH 1/2] REST: Don't error if a versioned field we would remove is absent

Daniel Axtens dja at axtens.net
Sat Aug 21 00:57:58 AEST 2021


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.

Fixes: d944f17ec059 ("REST: Use versioning for modified responses")
Signed-off-by: Daniel Axtens <dja at axtens.net>
---
 patchwork/api/base.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/patchwork/api/base.py b/patchwork/api/base.py
index 89a43114f9e7..6cb703b12bbb 100644
--- a/patchwork/api/base.py
+++ b/patchwork/api/base.py
@@ -96,6 +96,9 @@ class BaseHyperlinkedModelSerializer(HyperlinkedModelSerializer):
             # 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
-- 
2.30.2



More information about the Patchwork mailing list