]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Handle regular form data requests for checks
authorDaniel Axtens <dja@axtens.net>
Tue, 30 Apr 2019 06:03:03 +0000 (16:03 +1000)
committerStephen Finucane <stephen@that.guru>
Tue, 30 Apr 2019 17:37:03 +0000 (11:37 -0600)
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.

NOTE(daxtens): 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.

Fixes: dc48fbce99ef ("REST: Handle JSON requests")
Signed-off-by: Daniel Axtens <dja@axtens.net>
(cherry picked from commit 666de29ebada5990a8d69f4d71d6bb271e1a68c3)

patchwork/api/check.py

index 67062132fef3cd55cbd561d97fef6cd7951e2e4b..62e6fd19e761d5a79b7baf798c1f2e320c46dddd 100644 (file)
@@ -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)