Skip to content

Commit

Permalink
fix: support legacy OpenAPI file upload format (#892)
Browse files Browse the repository at this point in the history
* fix: support legacy file upload format

* updated tests
  • Loading branch information
Goldziher authored Nov 30, 2022
1 parent 14089ad commit f6150cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion starlite/datastructures/upload_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def __modify_schema__(cls, field_schema: Dict[str, Any], field: Optional["ModelF
None
"""
if field:
field_schema.update({"type": OpenAPIType.STRING.value, "contentMediaType": "application/octet-stream"})
field_schema.update(
{"type": OpenAPIType.STRING.value, "contentMediaType": "application/octet-stream", "format": "binary"}
)

def __repr__(self) -> str:
return f"{self.filename} - {self.content_type}"
5 changes: 4 additions & 1 deletion starlite/openapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ def get_schema_for_field_type(field: ModelField, plugins: List["PluginProtocol"]
schema_class=plugin.to_pydantic_model_class(field_type, parameter_name=field.name)
)
if field_type is UploadFile:
return Schema(
# the following is a hack -https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0
# the format for OA 3.1 is type + contentMediaType, for 3.0.* is type + format, we do both.
return Schema( # type: ignore
type=OpenAPIType.STRING,
format="binary",
contentMediaType="application/octet-stream",
)
# this is a failsafe to ensure we always return a value
Expand Down
17 changes: 14 additions & 3 deletions tests/openapi/test_request_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,29 @@ async def handle_file_list_upload(
components = schema_dict["components"]
assert paths["/file-upload"]["post"]["requestBody"]["content"]["multipart/form-data"]["media_type_schema"] == {
"type": "string",
"schema_format": "binary",
"contentMediaType": "application/octet-stream",
}
assert paths["/file-list-upload"]["post"]["requestBody"]["content"]["multipart/form-data"]["media_type_schema"] == {
"items": {"type": "string", "contentMediaType": "application/octet-stream"},
"items": {"type": "string", "schema_format": "binary", "contentMediaType": "application/octet-stream"},
"type": "array",
}
assert components == {
"schemas": {
"FormData": {
"properties": {
"cv": {"type": "string", "contentMediaType": "application/octet-stream", "title": "Cv"},
"image": {"type": "string", "contentMediaType": "application/octet-stream", "title": "Image"},
"cv": {
"type": "string",
"schema_format": "binary",
"contentMediaType": "application/octet-stream",
"title": "Cv",
},
"image": {
"type": "string",
"schema_format": "binary",
"contentMediaType": "application/octet-stream",
"title": "Image",
},
},
"type": "object",
"required": ["cv", "image"],
Expand Down

0 comments on commit f6150cc

Please sign in to comment.