Coverage for tests / test_response_model_include_exclude.py: 100%
57 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 fastapi import FastAPI 1abcd
2from fastapi.testclient import TestClient 1abcd
3from pydantic import BaseModel 1abcd
6class Model1(BaseModel): 1abcd
7 foo: str 1abcd
8 bar: str 1abcd
11class Model2(BaseModel): 1abcd
12 ref: Model1 1abcd
13 baz: str 1abcd
16class Model3(BaseModel): 1abcd
17 name: str 1abcd
18 age: int 1abcd
19 ref2: Model2 1abcd
22app = FastAPI() 1abcd
25@app.get( 1abcd
26 "/simple_include",
27 response_model=Model2,
28 response_model_include={"baz": ..., "ref": {"foo"}},
29)
30def simple_include(): 1abcd
31 return Model2( 1efg
32 ref=Model1(foo="simple_include model foo", bar="simple_include model bar"),
33 baz="simple_include model2 baz",
34 )
37@app.get( 1abcd
38 "/simple_include_dict",
39 response_model=Model2,
40 response_model_include={"baz": ..., "ref": {"foo"}},
41)
42def simple_include_dict(): 1abcd
43 return { 1hij
44 "ref": {
45 "foo": "simple_include_dict model foo",
46 "bar": "simple_include_dict model bar",
47 },
48 "baz": "simple_include_dict model2 baz",
49 }
52@app.get( 1abcd
53 "/simple_exclude",
54 response_model=Model2,
55 response_model_exclude={"ref": {"bar"}},
56)
57def simple_exclude(): 1abcd
58 return Model2( 1klm
59 ref=Model1(foo="simple_exclude model foo", bar="simple_exclude model bar"),
60 baz="simple_exclude model2 baz",
61 )
64@app.get( 1abcd
65 "/simple_exclude_dict",
66 response_model=Model2,
67 response_model_exclude={"ref": {"bar"}},
68)
69def simple_exclude_dict(): 1abcd
70 return { 1nop
71 "ref": {
72 "foo": "simple_exclude_dict model foo",
73 "bar": "simple_exclude_dict model bar",
74 },
75 "baz": "simple_exclude_dict model2 baz",
76 }
79@app.get( 1abcd
80 "/mixed",
81 response_model=Model3,
82 response_model_include={"ref2", "name"},
83 response_model_exclude={"ref2": {"baz"}},
84)
85def mixed(): 1abcd
86 return Model3( 1qrs
87 name="mixed model3 name",
88 age=3,
89 ref2=Model2(
90 ref=Model1(foo="mixed model foo", bar="mixed model bar"),
91 baz="mixed model2 baz",
92 ),
93 )
96@app.get( 1abcd
97 "/mixed_dict",
98 response_model=Model3,
99 response_model_include={"ref2", "name"},
100 response_model_exclude={"ref2": {"baz"}},
101)
102def mixed_dict(): 1abcd
103 return { 1tuv
104 "name": "mixed_dict model3 name",
105 "age": 3,
106 "ref2": {
107 "ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
108 "baz": "mixed_dict model2 baz",
109 },
110 }
113client = TestClient(app) 1abcd
116def test_nested_include_simple(): 1abcd
117 response = client.get("/simple_include") 1efg
119 assert response.status_code == 200, response.text 1efg
121 assert response.json() == { 1efg
122 "baz": "simple_include model2 baz",
123 "ref": {"foo": "simple_include model foo"},
124 }
127def test_nested_include_simple_dict(): 1abcd
128 response = client.get("/simple_include_dict") 1hij
130 assert response.status_code == 200, response.text 1hij
132 assert response.json() == { 1hij
133 "baz": "simple_include_dict model2 baz",
134 "ref": {"foo": "simple_include_dict model foo"},
135 }
138def test_nested_exclude_simple(): 1abcd
139 response = client.get("/simple_exclude") 1klm
140 assert response.status_code == 200, response.text 1klm
141 assert response.json() == { 1klm
142 "baz": "simple_exclude model2 baz",
143 "ref": {"foo": "simple_exclude model foo"},
144 }
147def test_nested_exclude_simple_dict(): 1abcd
148 response = client.get("/simple_exclude_dict") 1nop
149 assert response.status_code == 200, response.text 1nop
150 assert response.json() == { 1nop
151 "baz": "simple_exclude_dict model2 baz",
152 "ref": {"foo": "simple_exclude_dict model foo"},
153 }
156def test_nested_include_mixed(): 1abcd
157 response = client.get("/mixed") 1qrs
158 assert response.status_code == 200, response.text 1qrs
159 assert response.json() == { 1qrs
160 "name": "mixed model3 name",
161 "ref2": {
162 "ref": {"foo": "mixed model foo", "bar": "mixed model bar"},
163 },
164 }
167def test_nested_include_mixed_dict(): 1abcd
168 response = client.get("/mixed_dict") 1tuv
169 assert response.status_code == 200, response.text 1tuv
170 assert response.json() == { 1tuv
171 "name": "mixed_dict model3 name",
172 "ref2": {
173 "ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
174 },
175 }