mirror of
https://github.com/fastapi/fastapi.git
synced 2026-01-25 14:31:00 -05:00
* Make compatible with pydantic v1 * Remove unused import * Remove unused ignores * Update pydantic version * Fix minor formatting issue * ⏪ Revert removing iterate_in_threadpool * ✨ Add backwards compatibility with Pydantic 0.32.2 with deprecation warnings * ✅ Update tests to not break when using Pydantic < 1.0.0 * 📝 Update docs for Pydantic version 1.0.0 * 📌 Update Pydantic range version to support from 0.32.2 * 🎨 Format test imports * ✨ Add support for Pydantic < 1.2 for populate_validators * ✨ Add backwards compatibility for Pydantic < 1.2.0 with required fields * 📌 Relax requirement for Pydantic to < 2.0.0 🎉 🚀 * 💚 Update pragma coverage for older Pydantic versions
34 lines
582 B
Python
34 lines
582 B
Python
from typing import Optional
|
|
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
from starlette.testclient import TestClient
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class SubModel(BaseModel):
|
|
a: Optional[str] = "foo"
|
|
|
|
|
|
class Model(BaseModel):
|
|
x: Optional[int]
|
|
sub: SubModel
|
|
|
|
|
|
class ModelSubclass(Model):
|
|
y: int
|
|
|
|
|
|
@app.get("/", response_model=Model, response_model_exclude_unset=True)
|
|
def get() -> ModelSubclass:
|
|
return ModelSubclass(sub={}, y=1)
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_return_defaults():
|
|
response = client.get("/")
|
|
assert response.json() == {"sub": {}}
|