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

Handle the bytes type during build OpenAPI Specification #506

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions blacksheep/server/openapi/v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ def _try_get_schema_for_simple_type(self, object_type: Type) -> Optional[Schema]
if object_type is str:
return Schema(type=ValueType.STRING)

if object_type is bytes:
return Schema(type=ValueType.STRING, format=ValueFormat.BINARY)

if object_type is int:
return Schema(type=ValueType.INTEGER, format=ValueFormat.INT64)

Expand Down
78 changes: 78 additions & 0 deletions tests/test_openapi_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class CatDetails(Cat):
friends: List[int]


@dataclass
class CreateCatImages:
images: List[str]


@dataclass
class Combo(Generic[T, U]):
item_one: T
Expand Down Expand Up @@ -248,6 +253,9 @@ def create_cat(input: CreateCatInput) -> Cat: ...
@delete("/api/cats/{cat_id}")
def delete_cat(cat_id: int) -> None: ...

@post("/api/cats/{cat_id}/images")
def upload_images(cat_id: int, images: FromForm[CreateCatImages]) -> None: ...

return app


Expand Down Expand Up @@ -1593,6 +1601,30 @@ async def test_cats_api(docs: OpenAPIHandler, serializer: Serializer):
nullable: false
description: ''
required: true
/api/cats/{cat_id}/images:
post:
responses:
'204':
description: Success response
operationId: upload_images
parameters:
- name: cat_id
in: path
schema:
type: integer
format: int64
nullable: false
description: ''
required: true
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/CreateCatImages'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/CreateCatImages'
required: true
components:
schemas:
Cat:
Expand Down Expand Up @@ -1672,6 +1704,17 @@ async def test_cats_api(docs: OpenAPIHandler, serializer: Serializer):
type: integer
format: int64
nullable: false
CreateCatImages:
type: object
required:
- images
properties:
images:
type: array
nullable: false
items:
type: string
nullable: false
tags: []
""".strip()
)
Expand Down Expand Up @@ -1757,6 +1800,30 @@ async def test_cats_api_capital_operations_ids(
nullable: false
description: ''
required: true
/api/cats/{cat_id}/images:
post:
responses:
'204':
description: Success response
operationId: Upload images
parameters:
- name: cat_id
in: path
schema:
type: integer
format: int64
nullable: false
description: ''
required: true
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/CreateCatImages'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/CreateCatImages'
required: true
components:
schemas:
Cat:
Expand Down Expand Up @@ -1836,6 +1903,17 @@ async def test_cats_api_capital_operations_ids(
type: integer
format: int64
nullable: false
CreateCatImages:
type: object
required:
- images
properties:
images:
type: array
nullable: false
items:
type: string
nullable: false
tags: []
""".strip()
)
Expand Down
Loading