Debian Codegraph @

tests/test_union_inherited_body.py

raw
1 from fastapi import FastAPI
2 from fastapi.testclient import TestClient
3 from inline_snapshot import snapshot
4 from pydantic import BaseModel
5
6 app = FastAPI()
7
8
9 class Item(BaseModel):
10 name: str | None = None
11
12
13 class ExtendedItem(Item):
14 age: int
15
16
17 @app.post("/items/")
18 def save_union_different_body(item: ExtendedItem | Item):
19 return {"item": item}
20
21
22 client = TestClient(app)
23
24
25 def test_post_extended_item():
26 response = client.post("/items/", json={"name": "Foo", "age": 5})
27 assert response.status_code == 200, response.text
28 assert response.json() == {"item": {"name": "Foo", "age": 5}}
29
30
31 def test_post_item():
32 response = client.post("/items/", json={"name": "Foo"})
33 assert response.status_code == 200, response.text
34 assert response.json() == {"item": {"name": "Foo"}}
35
36
37 def test_openapi_schema():
38 response = client.get("/openapi.json")
39 assert response.status_code == 200, response.text
40 assert response.json() == snapshot(
41 {
42 "openapi": "3.1.0",
43 "info": {"title": "FastAPI", "version": "0.1.0"},
44 "paths": {
45 "/items/": {
46 "post": {
47 "responses": {
48 "200": {
49 "description": "Successful Response",
50 "content": {"application/json": {"schema": {}}},
51 },
52 "422": {
53 "description": "Validation Error",
54 "content": {
55 "application/json": {
56 "schema": {
57 "$ref": "#/components/schemas/HTTPValidationError"
58 }
59 }
60 },
61 },
62 },
63 "summary": "Save Union Different Body",
64 "operationId": "save_union_different_body_items__post",
65 "requestBody": {
66 "content": {
67 "application/json": {
68 "schema": {
69 "title": "Item",
70 "anyOf": [
71 {
72 "$ref": "#/components/schemas/ExtendedItem"
73 },
74 {"$ref": "#/components/schemas/Item"},
75 ],
76 }
77 }
78 },
79 "required": True,
80 },
81 }
82 }
83 },
84 "components": {
85 "schemas": {
86 "Item": {
87 "title": "Item",
88 "type": "object",
89 "properties": {
90 "name": {
91 "title": "Name",
92 "anyOf": [{"type": "string"}, {"type": "null"}],
93 }
94 },
95 },
96 "ExtendedItem": {
97 "title": "ExtendedItem",
98 "required": ["age"],
99 "type": "object",
100 "properties": {
101 "name": {
102 "title": "Name",
103 "anyOf": [{"type": "string"}, {"type": "null"}],
104 },
105 "age": {"title": "Age", "type": "integer"},
106 },
107 },
108 "ValidationError": {
109 "title": "ValidationError",
110 "required": ["loc", "msg", "type"],
111 "type": "object",
112 "properties": {
113 "loc": {
114 "title": "Location",
115 "type": "array",
116 "items": {
117 "anyOf": [{"type": "string"}, {"type": "integer"}]
118 },
119 },
120 "msg": {"title": "Message", "type": "string"},
121 "type": {"title": "Error Type", "type": "string"},
122 "input": {"title": "Input"},
123 "ctx": {"title": "Context", "type": "object"},
124 },
125 },
126 "HTTPValidationError": {
127 "title": "HTTPValidationError",
128 "type": "object",
129 "properties": {
130 "detail": {
131 "title": "Detail",
132 "type": "array",
133 "items": {
134 "$ref": "#/components/schemas/ValidationError"
135 },
136 }
137 },
138 },
139 }
140 },
141 }
142 )