Skip to content
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

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

Open
johnthagen opened this issue Mar 8, 2024 · 0 comments

Comments

@johnthagen
Copy link
Collaborator

johnthagen commented Mar 8, 2024

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:

Screenshot 2024-03-08 at 8 06 45 AM

Backend versions:

  • Django 4.2.11
  • djangorestframework 3.14.0
  • drf-spectacular 0.27.1
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

No branches or pull requests

1 participant