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