1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
class Model1(BaseModel):
foo: str
bar: str
class Model2(BaseModel):
ref: Model1
baz: str
class Model3(BaseModel):
name: str
age: int
ref2: Model2
app = FastAPI()
@app.get(
"/simple_include",
response_model=Model2,
response_model_include={"baz": ..., "ref": {"foo"}},
)
def simple_include():
return Model2(
ref=Model1(foo="simple_include model foo", bar="simple_include model bar"),
baz="simple_include model2 baz",
)
@app.get(
"/simple_include_dict",
response_model=Model2,
response_model_include={"baz": ..., "ref": {"foo"}},
)
def simple_include_dict():
return {
"ref": {
"foo": "simple_include_dict model foo",
"bar": "simple_include_dict model bar",
},
"baz": "simple_include_dict model2 baz",
}
@app.get(
"/simple_exclude",
response_model=Model2,
response_model_exclude={"ref": {"bar"}},
)
def simple_exclude():
return Model2(
ref=Model1(foo="simple_exclude model foo", bar="simple_exclude model bar"),
baz="simple_exclude model2 baz",
)
@app.get(
"/simple_exclude_dict",
response_model=Model2,
response_model_exclude={"ref": {"bar"}},
)
def simple_exclude_dict():
return {
"ref": {
"foo": "simple_exclude_dict model foo",
"bar": "simple_exclude_dict model bar",
},
"baz": "simple_exclude_dict model2 baz",
}
@app.get(
"/mixed",
response_model=Model3,
response_model_include={"ref2", "name"},
response_model_exclude={"ref2": {"baz"}},
)
def mixed():
return Model3(
name="mixed model3 name",
age=3,
ref2=Model2(
ref=Model1(foo="mixed model foo", bar="mixed model bar"),
baz="mixed model2 baz",
),
)
@app.get(
"/mixed_dict",
response_model=Model3,
response_model_include={"ref2", "name"},
response_model_exclude={"ref2": {"baz"}},
)
def mixed_dict():
return {
"name": "mixed_dict model3 name",
"age": 3,
"ref2": {
"ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
"baz": "mixed_dict model2 baz",
},
}
client = TestClient(app)
def test_nested_include_simple():
response = client.get("/simple_include")
assert response.status_code == 200, response.text
assert response.json() == {
"baz": "simple_include model2 baz",
"ref": {"foo": "simple_include model foo"},
}
def test_nested_include_simple_dict():
response = client.get("/simple_include_dict")
assert response.status_code == 200, response.text
assert response.json() == {
"baz": "simple_include_dict model2 baz",
"ref": {"foo": "simple_include_dict model foo"},
}
def test_nested_exclude_simple():
response = client.get("/simple_exclude")
assert response.status_code == 200, response.text
assert response.json() == {
"baz": "simple_exclude model2 baz",
"ref": {"foo": "simple_exclude model foo"},
}
def test_nested_exclude_simple_dict():
response = client.get("/simple_exclude_dict")
assert response.status_code == 200, response.text
assert response.json() == {
"baz": "simple_exclude_dict model2 baz",
"ref": {"foo": "simple_exclude_dict model foo"},
}
def test_nested_include_mixed():
response = client.get("/mixed")
assert response.status_code == 200, response.text
assert response.json() == {
"name": "mixed model3 name",
"ref2": {
"ref": {"foo": "mixed model foo", "bar": "mixed model bar"},
},
}
def test_nested_include_mixed_dict():
response = client.get("/mixed_dict")
assert response.status_code == 200, response.text
assert response.json() == {
"name": "mixed_dict model3 name",
"ref2": {
"ref": {"foo": "mixed_dict model foo", "bar": "mixed_dict model bar"},
},
}
|