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

aiohttp: json payload ignored when data keyword is set to None instead of being absent. #623

Open
leorochael opened this issue Dec 2, 2021 · 1 comment · May be fixed by #624
Open

aiohttp: json payload ignored when data keyword is set to None instead of being absent. #623

leorochael opened this issue Dec 2, 2021 · 1 comment · May be fixed by #624

Comments

@leorochael
Copy link

In vcr/stubs/aiohttp_stubs.py:vcr_request(), the new_request() coroutine checks for data or jsonwith this:

        data = kwargs.get("data", kwargs.get("json"))

But this line assumes that json can only be sent if data is absent. But data could be present and set to None, along with a json parameter that is not None.

In the case of a None in data and a payload in json, the json payload is ignored, and the cassete is recorded as:

interactions:
- request:
    body: null

The equivalent logic in aiohttp itself is tolerant of a None being present in data (as it must, since None is the default value of both arguments):

        if data is not None and json is not None:
            raise ValueError(
                "data and json parameters can not be used at the same time"
            )
        elif json is not None:
            data = payload.JsonPayload(json, dumps=self._json_serialize)

I recommend the logic in vcr/stubs/aiohttp_stubs.py:vcr_request() be changed to match aiohttp.client:

        data = kwargs.get("data")
        if data is None:
            data = kwargs.get("json")
        elif kwargs.get("json") is not None:
            raise ValueError(
                "data and json parameters can not be used at the same time"
            )
@leorochael leorochael linked a pull request Dec 2, 2021 that will close this issue
@djlambert
Copy link

The json needs to be dumped back to a string otherwise an exception is thrown in the matcher. So:

        if data is None:
            data = json.dumps(kwargs.get("json"))
        elif kwargs.get("json") is not None:
            raise ValueError(
                "data and json parameters can not be used at the same time"
            )

I had hoped this bug was resolved with all the other aiohttp fixes. I can provide additional details, need to fix the tests in my project first.

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

Successfully merging a pull request may close this issue.

2 participants