openapi-generators / openapi-generators/openapi-python-client
`File.file_name` marked as `Optional` but is required when used in `multipart/form-data`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2k
- Forks
- 293
- Avg merge
- 34m
- Merged PRs (30d)
- 1
Description
Describe the bug
In types.py:
file_name is marked Optional, so it seems like the user could omit it. But if they do to a multipart/form endpoint, backends such as DjangoRestFramework will return:
Status Code: 400 (Bad Request)
Content : {"file":["The submitted data was not a file. Check the encoding type on the form."]}
This means that users will not get a type checking error if they forget to include a file_name in this scenario.
It seems like perhaps a MultipartFile type is needed to be used to correctly model this?
@define
class MultipartFile:
"""Contains information for multipart file uploads"""
payload: BinaryIO
file_name: str
mime_type: Optional[str] = None
...
OpenAPI Spec File
/upload/:
post:
operationId: upload
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/UploadRequest'
required: true
components:
schemas:
UploadRequest:
type: object
properties:
file:
type: string
format: binary
required:
- file
Desktop (please complete the following information):
- OS: macOS 13.6.4
- Python Version: 3.12.0
- openapi-python-client version: 0.16.1
Additional context
The generated Swagger UI page for this endpoint looks like:
Backend versions:
- Django 4.2.11
- djangorestframework 3.14.0
- drf-spectacular 0.27.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with templates/types.py.jinja at the linked definition and trace how multipart/form-data file fields are represented in generated clients. Reproduce the provided UploadRequest specification, then verify that a required multipart file also requires file_name in the generated type and avoids the reported backend error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100