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

Add filename parameter to MultipartBody #400

Merged
merged 5 commits into from
Oct 28, 2024
Merged

Conversation

kaakaa
Copy link
Contributor

@kaakaa kaakaa commented Oct 28, 2024

Overview

Add support for MultipartBody filename parameter

Related Issue

Fixes #398

Demo

N/A

Notes

  • One possible option would have been to create an inner class such as Part, but I didn't adopt it because it would require relatively many changes to the codebase.

Testing Instructions

  • Add an unit test in serialization package
  • Set up reproducible environment for the issue, and confirm that this PR fix that
# FastAPI's endpoint
@app.post("/upload")
async def upload(name: str = Form(...), file: UploadFile = File(...)) -> int:
    return file.size
OpenAPI Spec
{
  "openapi": "3.0.2",
  "info": {
    "title": "Custom title",
    "summary": "This is a very custom OpenAPI schema",
    "description": "Here's a longer description of the custom **OpenAPI** schema",
    "version": "3.0.2"
  },
  "paths": {
    "/": {
      "get": {
        "summary": "Root",
        "operationId": "root__get",
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string",
                  "title": "Response Root  Get"
                }
              }
            }
          }
        }
      }
    },
    "/upload": {
      "post": {
        "summary": "Upload",
        "operationId": "upload_upload_post",
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "$ref": "#/components/schemas/Body_upload_upload_post"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "integer",
                  "title": "Response Upload Upload Post"
                }
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Body_upload_upload_post": {
        "properties": {
          "name": {
            "type": "string",
            "title": "Name"
          },
          "file": {
            "type": "string",
            "format": "binary",
            "title": "File"
          }
        },
        "type": "object",
        "required": [
          "name",
          "file"
        ],
        "title": "Body_upload_upload_post"
      },
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    }
  }
}

@kaakaa kaakaa requested a review from a team as a code owner October 28, 2024 13:07
Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

Copy link

sonarcloud bot commented Oct 28, 2024

@baywet baywet enabled auto-merge (squash) October 28, 2024 14:06
@baywet baywet merged commit 50aa7e3 into microsoft:main Oct 28, 2024
52 checks passed
@kaakaa kaakaa deleted the filename branch October 29, 2024 00:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done ✔️
Development

Successfully merging this pull request may close these issues.

Add support for MultipartBody filename parameter
2 participants