File tree 3 files changed +34
-4
lines changed
3 files changed +34
-4
lines changed Original file line number Diff line number Diff line change 1
- from fastapi import FastAPI
1
+ from fastapi import FastAPI , Depends
2
2
from fastapi import File
3
3
from fastapi import UploadFile
4
4
@@ -28,3 +28,15 @@ async def example_foo_body_handler(
28
28
"date" : data .date ,
29
29
"file_name" : document .filename
30
30
}
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
+ }
Original file line number Diff line number Diff line change @@ -22,6 +22,23 @@ def date_validator(cls, v: datetime):
22
22
return v .date ()
23
23
24
24
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
+
25
32
@PydanticConverter .body
26
33
class PostContractBodySchema (PostContractJSONSchema ):
27
34
pass
35
+
36
+
37
+ @PydanticConverter .body
38
+ class PostContractSmallBodySchema (PostContractSmallJSONSchema ):
39
+ pass
40
+
41
+
42
+ @PydanticConverter .body
43
+ class PostContractSmallDoubleBodySchema (PostContractSmallJSONSchema ):
44
+ pass
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ def form(cls, field: ModelField) -> Body:
19
19
@classmethod
20
20
def __form (cls , model_field : ModelField , default : Any ):
21
21
return Form (
22
- default = default ,
22
+ default = default or None ,
23
23
alias = model_field .alias or None ,
24
24
title = model_field .field_info .title or None ,
25
25
description = model_field .field_info .description or None ,
@@ -48,11 +48,12 @@ def body(cls, parent_cls: Type[BaseModel]):
48
48
)
49
49
)
50
50
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
53
53
54
54
sig = inspect .signature (as_form_func )
55
55
sig = sig .replace (parameters = new_parameters )
56
56
as_form_func .__signature__ = sig
57
57
setattr (cls , 'body' , as_form_func )
58
58
return cls
59
+
You can’t perform that action at this time.
0 commit comments