Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probable bug in GraphQL JSON request handling #45

Closed
artemgordinskiy opened this issue Oct 14, 2018 · 2 comments
Closed

Probable bug in GraphQL JSON request handling #45

artemgordinskiy opened this issue Oct 14, 2018 · 2 comments

Comments

@artemgordinskiy
Copy link
Contributor

There seems to be a bug in parsing of JSON GraphQL requests.

Test case (disregard the fact that it would also fail if parsed successfully):

def test_graphql_schema_json_query(api, schema):
    api.add_route("/", schema)

    r = api.session().post("http://;/", headers={"Accept": "json", "Content-type": "json"}, data={'query': '{ hello }'})
    assert r.ok

Result on the latest master:

tests/test_responder.py:123:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.env/lib/python3.7/site-packages/requests/sessions.py:559: in post
    return self.request('POST', url, data=data, json=json, **kwargs)
.env/lib/python3.7/site-packages/starlette/testclient.py:312: in request
    json=json,
.env/lib/python3.7/site-packages/requests/sessions.py:512: in request
    resp = self.send(prep, **send_kwargs)
.env/lib/python3.7/site-packages/requests/sessions.py:622: in send
    r = adapter.send(request, **kwargs)
.env/lib/python3.7/site-packages/starlette/testclient.py:159: in send
    raise exc from None
.env/lib/python3.7/site-packages/starlette/testclient.py:156: in send
    loop.run_until_complete(connection(receive, send))
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:568: in run_until_complete
    return future.result()
responder/api.py:71: in asgi
    resp = await self._dispatch_request(req)
responder/api.py:112: in _dispatch_request
    self.graphql_response(req, resp, schema=view)
responder/api.py:200: in graphql_response
    query = self._resolve_graphql_query(req)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

req = <responder.models.Request object at 0x105f20c18>

    @staticmethod
    def _resolve_graphql_query(req):
        if "json" in req.mimetype:
>           return req.json()["query"]
E           AttributeError: 'Request' object has no attribute 'json'

I tried fixing it myself, but got tangled in the web of async/await-s and ended up breaking other stuff 😅
I will try again later unless someone else wants to pick it up.

@kennethreitz
Copy link
Owner

I got it!

@artemgordinskiy
Copy link
Contributor Author

@kennethreitz Thank you!

There seems to be a different error now though, this time when non-async format_json() tries to pass Request.content coroutine to json.loads(). I guess format_json() needs to be async as well?

tests/test_responder.py:123:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.env/lib/python3.7/site-packages/requests/sessions.py:559: in post
    return self.request('POST', url, data=data, json=json, **kwargs)
.env/lib/python3.7/site-packages/starlette/testclient.py:312: in request
    json=json,
.env/lib/python3.7/site-packages/requests/sessions.py:512: in request
    resp = self.send(prep, **send_kwargs)
.env/lib/python3.7/site-packages/requests/sessions.py:622: in send
    r = adapter.send(request, **kwargs)
.env/lib/python3.7/site-packages/starlette/testclient.py:159: in send
    raise exc from None
.env/lib/python3.7/site-packages/starlette/testclient.py:156: in send
    loop.run_until_complete(connection(receive, send))
/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py:568: in run_until_complete
    return future.result()
responder/api.py:71: in asgi
    resp = await self._dispatch_request(req)
responder/api.py:112: in _dispatch_request
    await self.graphql_response(req, resp, schema=view)
responder/api.py:200: in graphql_response
    query = await self._resolve_graphql_query(req)
responder/api.py:180: in _resolve_graphql_query
    return await req.media("json")["query"]
responder/models.py:197: in media
    return self.formats[format](self)
responder/formats.py:23: in format_json
    return json.loads(r.content)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

s = <coroutine object Request.content at 0x104cc49c8>, encoding = None, cls = None, object_hook = None, parse_float = None, parse_int = None, parse_constant = None
object_pairs_hook = None, kw = {}

    def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
            parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):

            if not isinstance(s, (bytes, bytearray)):
>               raise TypeError(f'the JSON object must be str, bytes or bytearray, '
                                f'not {s.__class__.__name__}')
E               TypeError: the JSON object must be str, bytes or bytearray, not coroutine

/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py:341: TypeError
==================================================================== 1 failed, 43 passed in 0.44 seconds =====================================================================

sys:1: RuntimeWarning: coroutine 'Request.content' was never awaited

Sorry I'm not fixing this myself – I tried yesterday but got too deep into asyncio hole and some of the changes didn't feel right to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants