Repository navigation
Description
Describe the bug
When an API endpoint accepts data and files as a part of a multipart form data, the generated code puts the data fields (expected by httpx) into files, which leads to a TypeError in httpx when trying to encode the multipart form data.
To Reproduce
Steps to reproduce the behavior:
- Generate client
- Try to use the endpoint
Expected behavior
Should not encounter TypeError in httpx. More specifically, the _get_kwargs() function of the module for the endpoint needs to return an additional field, data, in addition to files for the plain text fields. That probably means multipart_data.to_multipart() should return both data and files. Currently, it only returns files.
OpenAPI Spec File
The openapi.yaml is private, but I can do my best to produce a snippet that would reproduce the error. The actual endpoint shouldn't matter because the TypeError is encountered before sending any requests.
openapi: 3.0.1
info:
title: Rest API
version: "3"
servers:
- url: /rest
security:
- JwtAuth: []
- ApiKeyAuth: []
paths:
/api/projects/{projectId}/depositFile:
post:
tags:
- Deposit File
summary: Deposit a file.
operationId: depositFile
parameters:
- name: projectId
in: path
description: The ID of the project
required: true
schema:
type: string
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/DepositFileDto'
responses:
"201":
description: The file is deposited successfully.
content:
application/json: {}
components:
schemas:
DepositFileDto:
required:
- code
- description
- mainFile
- parametersXmlFile
type: object
properties:
code:
maxLength: 255
minLength: 1
type: string
description: The code of the file
description:
maxLength: 4000
minLength: 1
type: string
description: The description of the file
mainFile:
type: string
description: The main file.
format: binary
versionComment:
type: string
nullable: trueDesktop (please complete the following information):
- OS: Windows 10
- Python Version: 3.9.4
- openapi-python-client version: 0.10.4
Activity
dbanty commentedon Oct 12, 2021
Thanks for reporting! I'm going to do a sweep of PRs and bugs this weekend so I'll try and get this one in too.
PKizzle commentedon Oct 19, 2021
For me this issue is still present in version 0.10.5.
Instead of producing something like this:
I receive this output instead:
dbanty commentedon Oct 31, 2021
Looks like this was broken in 0.10.0 with #372. That was a really big PR, I should have done more testing on it 😞. Of course I don't want to break the feature that was added either. Sounds like in order to fix this properly is going to require really overhauling how multipart/form works (which will require me spending a bunch of time learning about it).
If anyone else is really familiar with multipart/form and wants to take a crack at righting the various wrongs in this client, I'd be really happy to see another PR on this. Otherwise, it might be a bit before I can spend enough time to really get this right.
kairntech commentedon Dec 8, 2021
I wonder if the explanation of the issue is linked to the fact that with httpx version 0.19.0 a breaking change was made that forces file upload content to be bytes type and won't accept str type anymore.
So my suggestion would be to replace everywhere in templates the tuples
(None, string, 'text/plain')or(None, string, 'application/json')by their bytes counterpart, i.e.(None, string.encode(), 'text/plain')or(None, string.encode(), 'application/json')For example in enum_property.py.jinja
In list_property.py.jinja
And in
In model_property.py.jinja
What do you think?
kairntech commentedon Dec 9, 2021
FYI I did this modification in my custom templates directory and it works well with httpx > 0.20
dbanty commentedon Dec 18, 2021
Great find @kairntech! I confirmed that using the non-bytes variant (so as-generated today) in http 0.18.* works just fine, but I get the type error in the latest 0.21.1. I then added that little
.encode()on the properties and confirmed it works for both 0.18.* and 0.21.1, so that seems like the fix (and a darn good one)!I'll whip up a patch with those changes and get them in, but for all interested before the new release gets out, you can work around this by using httpx < 0.19.0
dbanty commentedon Dec 18, 2021
Unfortunately I think the fix is going to be a breaking change, because I believe that the payload of a file can now only be
BinaryIOand notTextIO.fix: Multipart uploads for httpx >= 0.19.0
fix: Multipart uploads for httpx >= 0.19.0
dbanty commentedon Dec 18, 2021
I've got a fix over in #548 which I've tested locally with some simple cases (though CI is annoyingly failing for everything right now). I'd really appreciate it if some of you could take a look, test it out with your own clients, and give it a review. It is a breaking change, so it'll wait until I'm ready to ship 0.11.0. Again, folks can work around this issue for now by using an older version of
httpx(before 0.19.0).fix: Multipart uploads for httpx >= 0.19.0
fix: Multipart uploads for httpx >= 0.19.0
fix: Multipart uploads for httpx >= 0.19.0 [#508, #548]. Thanks @skuo…
fix: Multipart uploads for httpx >= 0.19.0 [#508, #548]. Thanks @skuo…