openapi-generators / openapi-generators/openapi-python-client

`File.file_name` marked as `Optional` but is required when used in `multipart/form-data`

Open
#993 1 comment 6 reactions 0 assignees View on GitHub

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:

https://github.com/openapi-generators/openapi-python-client/blob/0399271be4eb012f83ac3023939ff48e364634e9/openapi_python_client/templates/types.py.jinja#L24

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:

Screenshot 2024-03-08 at 8 06 45 AM

Backend versions:

  • Django 4.2.11
  • djangorestframework 3.14.0
  • drf-spectacular 0.27.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.