Coverage for tests / test_union_inherited_body.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from fastapi import FastAPI 1adbc
2from fastapi.testclient import TestClient 1adbc
3from inline_snapshot import snapshot 1adbc
4from pydantic import BaseModel 1adbc
6app = FastAPI() 1adbc
9class Item(BaseModel): 1adbc
10 name: str | None = None 1adbc
13class ExtendedItem(Item): 1adbc
14 age: int 1abc
17@app.post("/items/") 1adbc
18def save_union_different_body(item: ExtendedItem | Item): 1adbc
19 return {"item": item} 1efghijk
22client = TestClient(app) 1adbc
25def test_post_extended_item(): 1adbc
26 response = client.post("/items/", json={"name": "Foo", "age": 5}) 1eghj
27 assert response.status_code == 200, response.text 1eghj
28 assert response.json() == {"item": {"name": "Foo", "age": 5}} 1eghj
31def test_post_item(): 1adbc
32 response = client.post("/items/", json={"name": "Foo"}) 1flik
33 assert response.status_code == 200, response.text 1flik
34 assert response.json() == {"item": {"name": "Foo"}} 1flik
37def test_openapi_schema(): 1adbc
38 response = client.get("/openapi.json") 1mnop
39 assert response.status_code == 200, response.text 1mnop
40 assert response.json() == snapshot( 1mnop
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 )