Files
fastapi/tests/test_skip_defaults.py
Sebastián Ramírez 1f01ce9615 📝 Use Optional in docs (#1644)
* Updated .py files with Optional tag (up to body_nested_models)

* Update optionals

* docs_src/ all updates, few I was unsure of

* Updated markdown files with Optional param

* es: Add Optional typing to index.md

* Last of markdown files updated with Optional param

* Update highlight lines

* it: Add Optional typings

* README.md: Update with Optional typings

* Update more highlight increments

* Update highlights

* schema-extra-example.md: Update highlights

* updating highlighting on website to reflect .py changes

* Update highlighting for query-params & response-directly

* Address PR comments

* Get rid of unnecessary comment

*  Revert Optional in Chinese docs as it probably also requires changes in text

* 🎨 Apply format

*  Revert modified example

* ♻️ Simplify example in docs

* 📝 Update OpenAPI callback example to use Optional

*  Add Optional types to tests

* 📝 Update docs about query params, default to using Optional

* 🎨 Update code examples line highlighting

* 📝 Update nested models docs to use "type parameters" instead of "subtypes"

* 📝 Add notes about FastAPI usage of None

including:

= None

and

= Query(None)

and clarify relationship with Optional[str]

* 📝 Add note about response_model_by_alias

* ♻️ Simplify query param list example

* 🔥 Remove test for removed example

*  Update test for updated example

Co-authored-by: Christopher Nguyen <chrisngyn99@gmail.com>
Co-authored-by: yk396 <yk396@cornell.edu>
Co-authored-by: Kai Chen <kaichen120@gmail.com>
2020-06-28 20:13:30 +02:00

96 lines
1.9 KiB
Python

from typing import Optional
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
app = FastAPI()
class SubModel(BaseModel):
a: Optional[str] = "foo"
class Model(BaseModel):
x: Optional[int]
sub: SubModel
class ModelSubclass(Model):
y: int
z: int = 0
w: Optional[int] = None
class ModelDefaults(BaseModel):
w: Optional[str] = None
x: Optional[str] = None
y: str = "y"
z: str = "z"
@app.get("/", response_model=Model, response_model_exclude_unset=True)
def get() -> ModelSubclass:
return ModelSubclass(sub={}, y=1, z=0)
@app.get(
"/exclude_unset", response_model=ModelDefaults, response_model_exclude_unset=True
)
def get() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get(
"/exclude_defaults",
response_model=ModelDefaults,
response_model_exclude_defaults=True,
)
def get() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get(
"/exclude_none", response_model=ModelDefaults, response_model_exclude_none=True
)
def get() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
@app.get(
"/exclude_unset_none",
response_model=ModelDefaults,
response_model_exclude_unset=True,
response_model_exclude_none=True,
)
def get() -> ModelDefaults:
return ModelDefaults(x=None, y="y")
client = TestClient(app)
def test_return_defaults():
response = client.get("/")
assert response.json() == {"sub": {}}
def test_return_exclude_unset():
response = client.get("/exclude_unset")
assert response.json() == {"x": None, "y": "y"}
def test_return_exclude_defaults():
response = client.get("/exclude_defaults")
assert response.json() == {}
def test_return_exclude_none():
response = client.get("/exclude_none")
assert response.json() == {"y": "y", "z": "z"}
def test_return_exclude_unset_none():
response = client.get("/exclude_unset_none")
assert response.json() == {"y": "y"}