|
|
|
@ -661,6 +661,32 @@ class Form(Body): # type: ignore[misc] |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
class _FileUploadMarker: |
|
|
|
"Pydantic metadata marker to tag bytes CoreSchemas as file uploads." |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def __get_pydantic_core_schema__( |
|
|
|
cls, source: type[Any], handler: Any |
|
|
|
) -> dict[str, Any]: |
|
|
|
schema = handler(source) |
|
|
|
|
|
|
|
# Find the inner type schema (if nullable or list) |
|
|
|
inner_type_schema = schema |
|
|
|
if inner_type_schema.get("type") != "bytes": |
|
|
|
if inner_type_schema.get("type") == "list": |
|
|
|
inner_type_schema = inner_type_schema["items_schema"] |
|
|
|
elif "schema" in inner_type_schema: |
|
|
|
inner_type_schema = inner_type_schema["schema"] |
|
|
|
if inner_type_schema.get("type") == "list": |
|
|
|
inner_type_schema = inner_type_schema["items_schema"] |
|
|
|
|
|
|
|
# If the inner type is bytes, add the file upload marker metadata |
|
|
|
if inner_type_schema.get("type") == "bytes": |
|
|
|
metadata: dict[str, Any] = inner_type_schema.setdefault("metadata", {}) |
|
|
|
metadata["fastapi_file_upload"] = True |
|
|
|
return schema |
|
|
|
|
|
|
|
|
|
|
|
class File(Form): # type: ignore[misc] |
|
|
|
def __init__( |
|
|
|
self, |
|
|
|
@ -741,6 +767,7 @@ class File(Form): # type: ignore[misc] |
|
|
|
json_schema_extra=json_schema_extra, |
|
|
|
**extra, |
|
|
|
) |
|
|
|
self.metadata.append(_FileUploadMarker()) |
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True) |
|
|
|
|