-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V1 models for response and body #10223
Open
chbndrhnns
wants to merge
10
commits into
fastapi:master
Choose a base branch
from
chbndrhnns:v1-response-model
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c29340c
Failing test for v1.BaseModel
chbndrhnns b9d2e9b
Allow v1.BaseModels to be used when pydantic v2 is installed
chbndrhnns 9fda784
Inline imports which only exist in v2
chbndrhnns a26d4ca
Black
chbndrhnns 16a8e8d
Require pydantic v2 for tests
chbndrhnns 17c7181
Import Annotated from typing_extensions
chbndrhnns 1b1346c
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] b72c7f0
Black
chbndrhnns 4eb2bc4
Use Optional
chbndrhnns 7cf3213
Use List instead of list
chbndrhnns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,99 @@ | ||
from typing import List, Optional | ||
|
||
import pytest | ||
from fastapi import Body, FastAPI | ||
from fastapi._compat import PYDANTIC_V2 | ||
from fastapi.exceptions import ResponseValidationError | ||
from fastapi.testclient import TestClient | ||
from typing_extensions import Annotated | ||
|
||
from tests.utils import needs_pydanticv2 | ||
|
||
if PYDANTIC_V2: | ||
from pydantic import v1 | ||
|
||
class Item(v1.BaseModel): | ||
name: str | ||
description: Optional[str] = None | ||
price: float | ||
tax: Optional[float] = None | ||
tags: list = [] | ||
|
||
class Model(v1.BaseModel): | ||
name: str | ||
|
||
else: | ||
from pydantic import BaseModel | ||
|
||
class Item(BaseModel): | ||
name: str | ||
description: Optional[str] = None | ||
price: float | ||
tax: Optional[float] = None | ||
tags: list = [] | ||
|
||
class Model(BaseModel): | ||
name: str | ||
|
||
|
||
app = FastAPI() | ||
|
||
|
||
@app.post("/request_body") | ||
async def request_body(body: Annotated[Item, Body()]): | ||
return body | ||
|
||
|
||
@app.get("/response_model", response_model=Model) | ||
async def response_model(): | ||
return Model(name="valid_model") | ||
|
||
|
||
@app.get("/response_model__invalid", response_model=Model) | ||
async def response_model__invalid(): | ||
return 1 | ||
|
||
|
||
@app.get("/response_model_list", response_model=List[Model]) | ||
async def response_model_list(): | ||
return [Model(name="valid_model")] | ||
|
||
|
||
@app.get("/response_model_list__invalid", response_model=List[Model]) | ||
async def response_model_list__invalid(): | ||
return [1] | ||
|
||
|
||
client = TestClient(app) | ||
|
||
|
||
@needs_pydanticv2 | ||
class TestResponseModel: | ||
def test_simple__valid(self): | ||
response = client.get("/response_model") | ||
assert response.status_code == 200 | ||
assert response.json() == {"name": "valid_model"} | ||
|
||
def test_simple__invalid(self): | ||
with pytest.raises(ResponseValidationError): | ||
client.get("/response_model__invalid") | ||
|
||
def test_list__valid(self): | ||
response = client.get("/response_model_list") | ||
assert response.status_code == 200 | ||
assert response.json() == [{"name": "valid_model"}] | ||
|
||
def test_list__invalid(self): | ||
with pytest.raises(ResponseValidationError): | ||
client.get("/response_model_list__invalid") | ||
|
||
|
||
@needs_pydanticv2 | ||
class TestRequestBody: | ||
def test_model__valid(self): | ||
response = client.post("/request_body", json={"name": "myname", "price": 1.0}) | ||
assert response.status_code == 200, response.text | ||
|
||
def test_model__invalid(self): | ||
response = client.post("/request_body", json={"name": "myname"}) | ||
assert response.status_code == 422, response.text |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if there is a better way to handle the case where there is a container of model instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isinstance()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least for Python 3.11+, I expect its zero-overhead exception handling to be a bit more efficient. In case that's neglectable, an if/else branch with
isinstance
might be more readable.