Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions blacksheep/server/openapi/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
Binder,
BodyBinder,
CookieBinder,
FilesBinder,
HeaderBinder,
QueryBinder,
RouteBinder,
Expand Down Expand Up @@ -1058,6 +1059,15 @@ def _get_body_binder(self, handler: Any) -> BodyBinder | None:
None,
)

def _get_files_binder(self, handler: Any) -> FilesBinder | None:
"""Returns the FilesBinder from a handler, if present."""
if not hasattr(handler, "binders"):
return None
return next(
(binder for binder in handler.binders if isinstance(binder, FilesBinder)),
None,
)

def _get_binder_by_name(self, handler: Any, name: str) -> Binder | None:
return next(
(binder for binder in handler.binders if binder.parameter_name == name),
Expand All @@ -1080,9 +1090,29 @@ def _get_body_binder_content_type(
def get_request_body(self, handler: Any) -> RequestBody | Reference | None:
if not hasattr(handler, "binders"):
return None
# TODO: improve this code to support more scenarios!
# https://github.com/Neoteroi/BlackSheep/issues/546

body_binder = self._get_body_binder(handler)
files_binder = self._get_files_binder(handler)

# If there's no body binder but there is a files binder, document the files
if files_binder and not body_binder:
docs = self.get_handler_docs(handler)
body_info = docs.request_body if docs else None

# Create schema for file upload
schema = Schema(
type=ValueType.ARRAY,
items=Schema(
type=ValueType.STRING,
format=ValueFormat.BINARY,
),
)

return RequestBody(
content={"multipart/form-data": MediaType(schema=schema)},
required=files_binder.required,
description=body_info.description if body_info else "File upload",
)

if body_binder is None:
return None
Expand All @@ -1096,6 +1126,7 @@ def get_request_body(self, handler: Any) -> RequestBody | Reference | None:
else None
)

# Original behavior for body binder
return RequestBody(
content=self._get_body_binder_content_type(body_binder, body_examples),
required=body_binder.required,
Expand Down
Loading
Loading