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
✨ Add support for function return type annotations to declare the response_model
#1436
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17e4878
:tada: Use function return type annotation as response_model
uriyyo ccc0f40
Merge remote-tracking branch 'fork/master'
uriyyo 1d6161c
Update impl
uriyyo 8c4cf25
Fix tests
uriyyo 685f402
Add ability to set response_model to None
uriyyo 01640c4
Merge branch 'master' into master
uriyyo 3b3d039
Merge branch 'master' into master
tiangolo 6a12fc5
✅ Add lots of tests to cover corner cases
tiangolo fe9c95c
📝 Update all the docs for return types and response_model
tiangolo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
@app.delete( | ||
"/{id}", | ||
status_code=204, | ||
response_model=None, | ||
) | ||
async def delete_deployment( | ||
id: int, | ||
|
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,141 @@ | ||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
from pydantic import BaseModel | ||
|
||
|
||
class ModelOne(BaseModel): | ||
name: str | ||
|
||
|
||
class ModelTwo(BaseModel): | ||
surname: str | ||
|
||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/valid1") | ||
def valid1() -> ModelOne: | ||
return ModelOne(name="Test") | ||
|
||
|
||
@app.get("/valid2", response_model=ModelTwo) | ||
def valid2(): | ||
return ModelTwo(surname="Test") | ||
|
||
|
||
@app.get("/valid3", response_model=ModelTwo) | ||
def valid3() -> ModelOne: | ||
return ModelTwo(surname="Test") | ||
|
||
|
||
@app.get("/valid4") | ||
def valid4() -> "ModelOne": | ||
return ModelOne(name="Test") | ||
|
||
|
||
openapi_schema = { | ||
"openapi": "3.0.2", | ||
"info": {"title": "FastAPI", "version": "0.1.0"}, | ||
"paths": { | ||
"/valid1": { | ||
"get": { | ||
"summary": "Valid1", | ||
"operationId": "valid1_valid1_get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/ModelOne"} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
"/valid2": { | ||
"get": { | ||
"summary": "Valid2", | ||
"operationId": "valid2_valid2_get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/ModelTwo"} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
"/valid3": { | ||
"get": { | ||
"summary": "Valid3", | ||
"operationId": "valid3_valid3_get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/ModelTwo"} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
"/valid4": { | ||
"get": { | ||
"summary": "Valid4", | ||
"operationId": "valid4_valid4_get", | ||
"responses": { | ||
"200": { | ||
"description": "Successful Response", | ||
"content": { | ||
"application/json": { | ||
"schema": {"$ref": "#/components/schemas/ModelOne"} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
}, | ||
"components": { | ||
"schemas": { | ||
"ModelOne": { | ||
"title": "ModelOne", | ||
"required": ["name"], | ||
"type": "object", | ||
"properties": {"name": {"title": "Name", "type": "string"}}, | ||
}, | ||
"ModelTwo": { | ||
"title": "ModelTwo", | ||
"required": ["surname"], | ||
"type": "object", | ||
"properties": {"surname": {"title": "Surname", "type": "string"}}, | ||
}, | ||
} | ||
}, | ||
} | ||
|
||
client = TestClient(app) | ||
|
||
|
||
def test_openapi_schema(): | ||
response = client.get("/openapi.json") | ||
assert response.status_code == 200, response.text | ||
assert response.json() == openapi_schema | ||
|
||
|
||
def test_path_operations(): | ||
response = client.get("/valid1") | ||
assert response.status_code == 200, response.text | ||
response = client.get("/valid2") | ||
assert response.status_code == 200, response.text | ||
response = client.get("/valid3") | ||
assert response.status_code == 200, response.text | ||
response = client.get("/valid4") | ||
assert response.status_code == 200, 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.
@iudeen They actually different. On lines 22-44 there is no return type annotation but 27-29 has it.