-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Enable Pydantic's serialization mode for responses, add support for…
… Pydantic's `computed_field`, better OpenAPI for response models, proper required attributes, better generated clients (#10011) * ✨ Enable Pydantic's serialization mode for responses * ✅ Update tests with new Pydantic v2 serialization mode * ✅ Add a test for Pydantic v2's computed_field
- Loading branch information
Showing
31 changed files
with
1,446 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import pytest | ||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
|
||
from .utils import needs_pydanticv2 | ||
|
||
|
||
@pytest.fixture(name="client") | ||
def get_client(): | ||
app = FastAPI() | ||
|
||
from pydantic import BaseModel, computed_field | ||
|
||
class Rectangle(BaseModel): | ||
width: int | ||
length: int | ||
|
||
@computed_field | ||
@property | ||
def area(self) -> int: | ||
return self.width * self.length | ||
|
||
@app.get("/") | ||
def read_root() -> Rectangle: | ||
return Rectangle(width=3, length=4) | ||
|
||
client = TestClient(app) | ||
return client | ||
|
||
|
||
@needs_pydanticv2 | ||
def test_get(client: TestClient): | ||
response = client.get("/") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == {"width": 3, "length": 4, "area": 12} | ||
|
||
|
||
@needs_pydanticv2 | ||
def test_openapi_schema(client: TestClient): | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == { | ||
"openapi": "3.1.0", | ||
"info": {"title": "FastAPI", "version": "0.1.0"}, | ||
"paths": { | ||
"/": { | ||
"get": { | ||
"summary": "Read Root", | ||
"operationId": "read_root__get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/Rectangle"} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"Rectangle": { | ||
"properties": { | ||
"width": {"type": "integer", "title": "Width"}, | ||
"length": {"type": "integer", "title": "Length"}, | ||
"area": {"type": "integer", "title": "Area", "readOnly": True}, | ||
}, | ||
"type": "object", | ||
"required": ["width", "length", "area"], | ||
"title": "Rectangle", | ||
} | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.