Skip to content

[Bug]: Chunked request from request libraly #554

@jitka

Description

@jitka
Contributor

Actual Behavior

When it shout check response where request data was chunked it fails.

    @property
    def body(self) -> Optional[str]:
        if self.request.body is None:
            return None
        if isinstance(self.request.body, bytes):
            return self.request.body.decode("utf-8")
>       assert isinstance(self.request.body, str)
E       AssertionError

Expected Behavior

Check everithing which can, ideally chunked body itself.

Steps to Reproduce

    @staticmethod
    def chunk_generator(data):
        for block in raw:
            yield block
request = Request(method, url, headers=headers, data=chunk_generator(data), **kwargs)
response = session.send(session.prepare_request(request))  
openapi_request = RequestsOpenAPIRequest(request)
openapi_response = RequestsOpenAPIResponse(response)
unmarshal_response(openapi_request, openapi_response, self.spec)

OpenAPI Core Version

0.17.1

OpenAPI Core Integration

requests

Affected Area(s)

unmarshaling

References

No response

Anything else we need to know?

No response

Would you like to implement a fix?

None

Activity

jitka

jitka commented on Apr 11, 2023

@jitka
ContributorAuthor

possible fix

import types
class RequestsOpenAPIRequestFix(RequestsOpenAPIRequest):
    ...

    @property
    def body(self) -> Optional[str]:
        if self.request.body is None or isinstance(self.request.body, types.GeneratorType):
            return None
        if isinstance(self.request.body, bytes):
            return self.request.body.decode("utf-8")
        assert isinstance(self.request.body, str)
        # TODO: figure out if request._body_position is relevant
        return self.request.body
p1c2u

p1c2u commented on Apr 13, 2023

@p1c2u
Collaborator

Generator should be resolved to string and passed further to the request if we want to validate the body.

linked a pull request that will close this issue on Apr 13, 2023
p1c2u

p1c2u commented on Apr 13, 2023

@p1c2u
Collaborator

It can be done with Request object but if user has Request object and pass only PreparedRequest to request factory we will not be able to recreate generator and the original generator will be expired in the Request object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jitka@p1c2u

      Issue actions

        [Bug]: Chunked request from request libraly · Issue #554 · python-openapi/openapi-core