Skip to content

use prepared request to format payload before converting #271

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

Merged
merged 3 commits into from
Mar 24, 2021
Merged

use prepared request to format payload before converting #271

merged 3 commits into from
Mar 24, 2021

Conversation

eyadgaran
Copy link
Contributor

Use requests.PreparedRequest object to convert requests.Request into a formatted payload before converting into an OpenAPI request.

Fixes bug where one of the many requests.Request convenience methods (ex json) are used and payload is not stored in Request.data. Should also handle scenarios where persistent sessions have expected headers not applied to the request object until it is prepared and sent

@remram44
Copy link

I ran into this same problem with 'json'. I have to prepare the request either way to send it, so it makes sense for the OpenAPI Request object to be created from that.

@remram44
Copy link

remram44 commented Jan 12, 2021

I ran into problems trying to support requests with multipart/form-data. Nothing else worked, so I tried using this version. It does not solve the problem:

openapi_core.schema.media_types.exceptions.InvalidContentType: Content for following mimetype not found: multipart/form-data; boundary=6c20c84fd95c1d9ac73bcc7ed47fa1c0

edit: or does it hmmmm

@remram44
Copy link

remram44 commented Feb 20, 2021

I was using this code successfully, but it stopped working after updated openapi-core 0.13.6 to 0.13.7 with urlencoded bodies. It seems the urlencoded string gets validated rather than the decoded dictionary of values. I suspect it never worked but didn't signal an error because of abfd0c6.

Edit: I had to add a deserializer:

openapi_request_validator = RequestValidator(
    openapi_spec,
    custom_media_type_deserializers={
        'application/x-www-form-urlencoded': lambda v: v if isinstance(v, dict) else dict(urllib.parse.parse_qsl(v)),
    },
)

@codecov
Copy link

codecov bot commented Mar 24, 2021

Codecov Report

Merging #271 (ff4a6c8) into master (6a222b9) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #271      +/-   ##
==========================================
+ Coverage   96.75%   96.77%   +0.01%     
==========================================
  Files         122      122              
  Lines        2436     2448      +12     
==========================================
+ Hits         2357     2369      +12     
  Misses         79       79              
Impacted Files Coverage Δ
openapi_core/contrib/requests/requests.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6a222b9...ff4a6c8. Read the comment docs.

Copy link
Collaborator

@p1c2u p1c2u left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests, removed py3 specific typing. LGTM

@p1c2u
Copy link
Collaborator

p1c2u commented Mar 24, 2021

@eyadgaran thank you for the contribution.

@p1c2u p1c2u merged commit 695f4e6 into python-openapi:master Mar 24, 2021
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 this pull request may close these issues.

3 participants