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