Coverage for tests/test_union_inherited_body.py: 100%
26 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Optional, Union 1abcde
3from dirty_equals import IsDict 1abcde
4from fastapi import FastAPI 1abcde
5from fastapi.testclient import TestClient 1abcde
6from pydantic import BaseModel 1abcde
8app = FastAPI() 1abcde
11class Item(BaseModel): 1abcde
12 name: Optional[str] = None 1abcde
15class ExtendedItem(Item): 1abcde
16 age: int 1abcde
19@app.post("/items/") 1abcde
20def save_union_different_body(item: Union[ExtendedItem, Item]): 1abcde
21 return {"item": item} 1abcde
24client = TestClient(app) 1abcde
27def test_post_extended_item(): 1abcde
28 response = client.post("/items/", json={"name": "Foo", "age": 5}) 1abcde
29 assert response.status_code == 200, response.text 1abcde
30 assert response.json() == {"item": {"name": "Foo", "age": 5}} 1abcde
33def test_post_item(): 1abcde
34 response = client.post("/items/", json={"name": "Foo"}) 1abcde
35 assert response.status_code == 200, response.text 1abcde
36 assert response.json() == {"item": {"name": "Foo"}} 1abcde
39def test_openapi_schema(): 1abcde
40 response = client.get("/openapi.json") 1abcde
41 assert response.status_code == 200, response.text 1abcde
42 assert response.json() == { 1abcde
43 "openapi": "3.1.0",
44 "info": {"title": "FastAPI", "version": "0.1.0"},
45 "paths": {
46 "/items/": {
47 "post": {
48 "responses": {
49 "200": {
50 "description": "Successful Response",
51 "content": {"application/json": {"schema": {}}},
52 },
53 "422": {
54 "description": "Validation Error",
55 "content": {
56 "application/json": {
57 "schema": {
58 "$ref": "#/components/schemas/HTTPValidationError"
59 }
60 }
61 },
62 },
63 },
64 "summary": "Save Union Different Body",
65 "operationId": "save_union_different_body_items__post",
66 "requestBody": {
67 "content": {
68 "application/json": {
69 "schema": {
70 "title": "Item",
71 "anyOf": [
72 {"$ref": "#/components/schemas/ExtendedItem"},
73 {"$ref": "#/components/schemas/Item"},
74 ],
75 }
76 }
77 },
78 "required": True,
79 },
80 }
81 }
82 },
83 "components": {
84 "schemas": {
85 "Item": {
86 "title": "Item",
87 "type": "object",
88 "properties": {
89 "name": IsDict(
90 {
91 "title": "Name",
92 "anyOf": [{"type": "string"}, {"type": "null"}],
93 }
94 )
95 | IsDict(
96 # TODO: remove when deprecating Pydantic v1
97 {"title": "Name", "type": "string"}
98 )
99 },
100 },
101 "ExtendedItem": {
102 "title": "ExtendedItem",
103 "required": ["age"],
104 "type": "object",
105 "properties": {
106 "name": IsDict(
107 {
108 "title": "Name",
109 "anyOf": [{"type": "string"}, {"type": "null"}],
110 }
111 )
112 | IsDict(
113 # TODO: remove when deprecating Pydantic v1
114 {"title": "Name", "type": "string"}
115 ),
116 "age": {"title": "Age", "type": "integer"},
117 },
118 },
119 "ValidationError": {
120 "title": "ValidationError",
121 "required": ["loc", "msg", "type"],
122 "type": "object",
123 "properties": {
124 "loc": {
125 "title": "Location",
126 "type": "array",
127 "items": {
128 "anyOf": [{"type": "string"}, {"type": "integer"}]
129 },
130 },
131 "msg": {"title": "Message", "type": "string"},
132 "type": {"title": "Error Type", "type": "string"},
133 },
134 },
135 "HTTPValidationError": {
136 "title": "HTTPValidationError",
137 "type": "object",
138 "properties": {
139 "detail": {
140 "title": "Detail",
141 "type": "array",
142 "items": {"$ref": "#/components/schemas/ValidationError"},
143 }
144 },
145 },
146 }
147 },
148 }