[PATCH 05/10] REST: Handle regular form data requests for checks

Daniel Axtens dja at axtens.net
Tue Apr 30 16:03:03 AEST 2019


08d1459a4a40 ("Add REST API validation using OpenAPI schema") moved
all API requests to JSON blobs rather than form data.

dc48fbce99ef ("REST: Handle JSON requests") attempted to change the
check serialiser to handle this. However, because both a JSON dict
and a QueryDict satisfy isinstance(data, dict), everything was handled
as JSON and the old style requests were broken.

Found in the process of debugging issues from the OzLabs PW & Snowpatch
crew - I'm not sure if they actually hit this one, but kudos to them
anyway as we wouldn't have found it without them.

Fixes: dc48fbce99ef ("REST: Handle JSON requests")
(backported from commit 666de29ebada5990a8d69f4d71d6bb271e1a68c3
 This does not need the new tests as we do not have 08d1459a4a40, so we
 just need the fix to the api. We do not add a JSON test to stable.)
Signed-off-by: Daniel Axtens <dja at axtens.net>
---
 patchwork/api/check.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/patchwork/api/check.py b/patchwork/api/check.py
index 67062132fef3..62e6fd19e761 100644
--- a/patchwork/api/check.py
+++ b/patchwork/api/check.py
@@ -19,6 +19,7 @@
 
 from django.http import Http404
 from django.shortcuts import get_object_or_404
+from django.http.request import QueryDict
 from rest_framework.exceptions import PermissionDenied
 from rest_framework.generics import ListCreateAPIView
 from rest_framework.generics import RetrieveAPIView
@@ -53,9 +54,7 @@ class CheckSerializer(HyperlinkedModelSerializer):
             if label != data['state']:
                 continue
 
-            if isinstance(data, dict):  # json request
-                data['state'] = val
-            else:  # form-data request
+            if isinstance(data, QueryDict):  # form-data request
                 # NOTE(stephenfin): 'data' is essentially 'request.POST', which
                 # is immutable by default. However, there's no good reason for
                 # this to be this way [1], so temporarily unset that mutability
@@ -66,6 +65,8 @@ class CheckSerializer(HyperlinkedModelSerializer):
                 data._mutable = True  # noqa
                 data['state'] = val
                 data._mutable = mutable  # noqa
+            else:  # json request
+                data['state'] = val
 
             break
         return super(CheckSerializer, self).run_validation(data)
-- 
2.19.1



More information about the Patchwork mailing list