diff mbox series

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

Message ID 20190430060308.10432-6-dja@axtens.net
State Accepted
Headers show
Series Patchwork 2.1.2 review series | expand

Commit Message

Daniel Axtens April 30, 2019, 6:03 a.m. UTC
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@axtens.net>
---
 patchwork/api/check.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Stephen Finucane April 30, 2019, 5:50 p.m. UTC | #1
On Tue, 2019-04-30 at 16:03 +1000, Daniel Axtens wrote:
> 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@axtens.net>

Applied.
diff mbox series

Patch

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)