-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Update docs and examples for Response Model with Return Type Annota…
…tions, and update runtime error (#5873)
- Loading branch information
Showing
18 changed files
with
757 additions
and
1 deletion.
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,11 @@ | ||
from fastapi import FastAPI, Response | ||
from fastapi.responses import JSONResponse, RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/portal") | ||
async def get_portal(teleport: bool = False) -> Response: | ||
if teleport: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") | ||
return JSONResponse(content={"message": "Here's your interdimensional portal."}) |
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,9 @@ | ||
from fastapi import FastAPI | ||
from fastapi.responses import RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/teleport") | ||
async def get_teleport() -> RedirectResponse: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") |
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,13 @@ | ||
from typing import Union | ||
|
||
from fastapi import FastAPI, Response | ||
from fastapi.responses import RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/portal") | ||
async def get_portal(teleport: bool = False) -> Union[Response, dict]: | ||
if teleport: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") | ||
return {"message": "Here's your interdimensional portal."} |
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,11 @@ | ||
from fastapi import FastAPI, Response | ||
from fastapi.responses import RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/portal") | ||
async def get_portal(teleport: bool = False) -> Response | dict: | ||
if teleport: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") | ||
return {"message": "Here's your interdimensional portal."} |
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,13 @@ | ||
from typing import Union | ||
|
||
from fastapi import FastAPI, Response | ||
from fastapi.responses import RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/portal", response_model=None) | ||
async def get_portal(teleport: bool = False) -> Union[Response, dict]: | ||
if teleport: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") | ||
return {"message": "Here's your interdimensional portal."} |
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,11 @@ | ||
from fastapi import FastAPI, Response | ||
from fastapi.responses import RedirectResponse | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/portal", response_model=None) | ||
async def get_portal(teleport: bool = False) -> Response | dict: | ||
if teleport: | ||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ") | ||
return {"message": "Here's your interdimensional portal."} |
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
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
120 changes: 120 additions & 0 deletions
120
tests/test_tutorial/test_response_model/test_tutorial003_01.py
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,120 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
from docs_src.response_model.tutorial003_01 import app | ||
|
||
client = TestClient(app) | ||
|
||
openapi_schema = { | ||
"openapi": "3.0.2", | ||
"info": {"title": "FastAPI", "version": "0.1.0"}, | ||
"paths": { | ||
"/user/": { | ||
"post": { | ||
"summary": "Create User", | ||
"operationId": "create_user_user__post", | ||
"requestBody": { | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/UserIn"} | ||
} | ||
}, | ||
"required": True, | ||
}, | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/BaseUser"} | ||
} | ||
}, | ||
}, | ||
"422": { | ||
"description": "Validation Error", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HTTPValidationError" | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
}, | ||
"components": { | ||
"schemas": { | ||
"BaseUser": { | ||
"title": "BaseUser", | ||
"required": ["username", "email"], | ||
"type": "object", | ||
"properties": { | ||
"username": {"title": "Username", "type": "string"}, | ||
"email": {"title": "Email", "type": "string", "format": "email"}, | ||
"full_name": {"title": "Full Name", "type": "string"}, | ||
}, | ||
}, | ||
"HTTPValidationError": { | ||
"title": "HTTPValidationError", | ||
"type": "object", | ||
"properties": { | ||
"detail": { | ||
"title": "Detail", | ||
"type": "array", | ||
"items": {"$ref": "#/components/schemas/ValidationError"}, | ||
} | ||
}, | ||
}, | ||
"UserIn": { | ||
"title": "UserIn", | ||
"required": ["username", "email", "password"], | ||
"type": "object", | ||
"properties": { | ||
"username": {"title": "Username", "type": "string"}, | ||
"email": {"title": "Email", "type": "string", "format": "email"}, | ||
"full_name": {"title": "Full Name", "type": "string"}, | ||
"password": {"title": "Password", "type": "string"}, | ||
}, | ||
}, | ||
"ValidationError": { | ||
"title": "ValidationError", | ||
"required": ["loc", "msg", "type"], | ||
"type": "object", | ||
"properties": { | ||
"loc": { | ||
"title": "Location", | ||
"type": "array", | ||
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]}, | ||
}, | ||
"msg": {"title": "Message", "type": "string"}, | ||
"type": {"title": "Error Type", "type": "string"}, | ||
}, | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
|
||
def test_openapi_schema(): | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == openapi_schema | ||
|
||
|
||
def test_post_user(): | ||
response = client.post( | ||
"/user/", | ||
json={ | ||
"username": "foo", | ||
"password": "fighter", | ||
"email": "[email protected]", | ||
"full_name": "Grave Dohl", | ||
}, | ||
) | ||
assert response.status_code == 200, response.text | ||
assert response.json() == { | ||
"username": "foo", | ||
"email": "[email protected]", | ||
"full_name": "Grave Dohl", | ||
} |
Oops, something went wrong.