Coverage for tests / test_union_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 OtherItem(BaseModel): 1adbc
14 price: int 1abc
17@app.post("/items/") 1adbc
18def save_union_body(item: OtherItem | Item): 1adbc
19 return {"item": item} 1efghijk
22client = TestClient(app) 1adbc
25def test_post_other_item(): 1adbc
26 response = client.post("/items/", json={"price": 100}) 1fgik
27 assert response.status_code == 200, response.text 1fgik
28 assert response.json() == {"item": {"price": 100}} 1fgik
31def test_post_item(): 1adbc
32 response = client.post("/items/", json={"name": "Foo"}) 1elhj
33 assert response.status_code == 200, response.text 1elhj
34 assert response.json() == {"item": {"name": "Foo"}} 1elhj
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 Body",
64 "operationId": "save_union_body_items__post",
65 "requestBody": {
66 "content": {
67 "application/json": {
68 "schema": {
69 "title": "Item",
70 "anyOf": [
71 {"$ref": "#/components/schemas/OtherItem"},
72 {"$ref": "#/components/schemas/Item"},
73 ],
74 }
75 }
76 },
77 "required": True,
78 },
79 }
80 }
81 },
82 "components": {
83 "schemas": {
84 "OtherItem": {
85 "title": "OtherItem",
86 "required": ["price"],
87 "type": "object",
88 "properties": {"price": {"title": "Price", "type": "integer"}},
89 },
90 "Item": {
91 "title": "Item",
92 "type": "object",
93 "properties": {
94 "name": {
95 "title": "Name",
96 "anyOf": [{"type": "string"}, {"type": "null"}],
97 }
98 },
99 },
100 "ValidationError": {
101 "title": "ValidationError",
102 "required": ["loc", "msg", "type"],
103 "type": "object",
104 "properties": {
105 "loc": {
106 "title": "Location",
107 "type": "array",
108 "items": {
109 "anyOf": [{"type": "string"}, {"type": "integer"}]
110 },
111 },
112 "msg": {"title": "Message", "type": "string"},
113 "type": {"title": "Error Type", "type": "string"},
114 "input": {"title": "Input"},
115 "ctx": {"title": "Context", "type": "object"},
116 },
117 },
118 "HTTPValidationError": {
119 "title": "HTTPValidationError",
120 "type": "object",
121 "properties": {
122 "detail": {
123 "title": "Detail",
124 "type": "array",
125 "items": {
126 "$ref": "#/components/schemas/ValidationError"
127 },
128 }
129 },
130 },
131 }
132 },
133 }
134 )