Skip to content

Commit

Permalink
Handle the bytes type during build OpenAPI Specification
Browse files Browse the repository at this point in the history
  • Loading branch information
tyzhnenko committed Jun 27, 2024
1 parent 026897f commit 06df67d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
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

0 comments on commit 06df67d

Please sign in to comment.