11from typing import Union
22
3- from ellar .common import post
3+ from ellar .common import Body , post
44from ellar .common .serializer import serialize_object
55from ellar .openapi import OpenAPIDocumentBuilder
66from ellar .testing import Test
1111
1212
1313@post ("/items/" )
14- def save_union_body (item : Union [OtherItem , Item ]):
15- return {"item" : item }
14+ def save_union_body_and_embedded_body (
15+ item : Union [OtherItem , Item ], qty : int = Body (12 )
16+ ):
17+ return {"item" : item , "qty" : qty }
1618
1719
1820app = tm .create_application ()
19- app .router .append (save_union_body )
21+ app .router .append (save_union_body_and_embedded_body )
2022
2123client = tm .get_test_client ()
2224
@@ -32,11 +34,7 @@ def save_union_body(item: Union[OtherItem, Item]):
3234 "content" : {
3335 "application/json" : {
3436 "schema" : {
35- "title" : "Item" ,
36- "anyOf" : [
37- {"$ref" : "#/components/schemas/OtherItem" },
38- {"$ref" : "#/components/schemas/Item" },
39- ],
37+ "$ref" : "#/components/schemas/body_save_union_body_items__post"
4038 }
4139 }
4240 },
@@ -104,6 +102,21 @@ def save_union_body(item: Union[OtherItem, Item]):
104102 "type" : {"title" : "Error Type" , "type" : "string" },
105103 },
106104 },
105+ "body_save_union_body_items__post" : {
106+ "title" : "body_save_union_body_items__post" ,
107+ "required" : ["item" ],
108+ "type" : "object" ,
109+ "properties" : {
110+ "item" : {
111+ "title" : "Item" ,
112+ "anyOf" : [
113+ {"$ref" : "#/components/schemas/OtherItem" },
114+ {"$ref" : "#/components/schemas/Item" },
115+ ],
116+ },
117+ "qty" : {"title" : "Qty" , "type" : "integer" , "default" : 12 },
118+ },
119+ },
107120 }
108121 },
109122 "tags" : [],
@@ -116,12 +129,12 @@ def test_item_openapi_schema():
116129
117130
118131def test_post_other_item ():
119- response = client .post ("/items/" , json = {"price" : 100 })
132+ response = client .post ("/items/" , json = {"item" : { " price" : 100 } })
120133 assert response .status_code == 200 , response .text
121- assert response .json () == {"item" : {"price" : 100 }}
134+ assert response .json () == {"item" : {"price" : 100 }, "qty" : 12 }
122135
123136
124137def test_post_item ():
125- response = client .post ("/items/" , json = {"name" : "Foo" })
138+ response = client .post ("/items/" , json = {"item" : { " name" : "Foo" } })
126139 assert response .status_code == 200 , response .text
127- assert response .json () == {"item" : {"name" : "Foo" }}
140+ assert response .json () == {"item" : {"name" : "Foo" }, "qty" : 12 }
0 commit comments