Skip to content

Commit 3b0ef42

Browse files
authored
Merge pull request #1 from dotX12/dev
Fixes
2 parents d92d720 + e435744 commit 3b0ef42

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

examples/main.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import FastAPI
1+
from fastapi import FastAPI, Depends
22
from fastapi import File
33
from fastapi import UploadFile
44

@@ -28,3 +28,15 @@ async def example_foo_body_handler(
2828
"date": data.date,
2929
"file_name": document.filename
3030
}
31+
32+
33+
@app.post("/form-data-body-two")
34+
async def example_foo_body_handler(
35+
data: PostContractBodySchema = FormBody(),
36+
document: UploadFile = File(...),
37+
):
38+
return {
39+
"title": data.title,
40+
"date": data.date,
41+
"file_name": document.filename
42+
}

examples/models.py

+17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ def date_validator(cls, v: datetime):
2222
return v.date()
2323

2424

25+
class PostContractSmallJSONSchema(BaseModel):
26+
title: str = Field(..., description="Description title")
27+
date: Optional[datetime] = Field(
28+
None, description="Example: 2021-12-14T09:56:31.056Z"
29+
)
30+
31+
2532
@PydanticConverter.body
2633
class PostContractBodySchema(PostContractJSONSchema):
2734
pass
35+
36+
37+
@PydanticConverter.body
38+
class PostContractSmallBodySchema(PostContractSmallJSONSchema):
39+
pass
40+
41+
42+
@PydanticConverter.body
43+
class PostContractSmallDoubleBodySchema(PostContractSmallJSONSchema):
44+
pass

pyfa_converter/utils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def form(cls, field: ModelField) -> Body:
1919
@classmethod
2020
def __form(cls, model_field: ModelField, default: Any):
2121
return Form(
22-
default=default,
22+
default=default or None,
2323
alias=model_field.alias or None,
2424
title=model_field.field_info.title or None,
2525
description=model_field.field_info.description or None,
@@ -48,11 +48,12 @@ def body(cls, parent_cls: Type[BaseModel]):
4848
)
4949
)
5050

51-
def as_form_func(**data):
52-
return parent_cls(**data)
51+
async def as_form_func(*args, **kwargs):
52+
return parent_cls(*args, **kwargs) # noqa
5353

5454
sig = inspect.signature(as_form_func)
5555
sig = sig.replace(parameters=new_parameters)
5656
as_form_func.__signature__ = sig
5757
setattr(cls, 'body', as_form_func)
5858
return cls
59+

0 commit comments

Comments
 (0)