-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
Additional Responses #97
Conversation
Codecov Report
@@ Coverage Diff @@
## master #97 +/- ##
========================================
- Coverage 100% 99.2% -0.8%
========================================
Files 109 110 +1
Lines 2740 2877 +137
========================================
+ Hits 2740 2854 +114
- Misses 0 23 +23
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #97 +/- ##
======================================
Coverage 100% 100%
======================================
Files 109 146 +37
Lines 2740 3590 +850
======================================
+ Hits 2740 3590 +850
Continue to review full report at Codecov.
|
Thanks! I'll review it soon. |
Thanks @barsi, I see you put quite some effort into this, thanks for that! 👏 🍰 I took it further and updated/refactored it all to make it simpler for developers to use while extending the capabilities. For example, it's possible to add more info to an existing I also included docs, tests for the code in the docs, etc. With this you can now add a from fastapi import FastAPI
from pydantic import BaseModel
from starlette.responses import JSONResponse
class Item(BaseModel):
id: str
value: str
class Message(BaseModel):
message: str
app = FastAPI()
@app.get("/items/{item_id}", response_model=Item, responses={404: {"model": Message}})
async def read_item(item_id: str):
if item_id == "foo":
return {"id": "foo", "value": "there goes my hero"}
else:
return JSONResponse(status_code=404, content={"message": "Item not found"}) The new docs are here: https://fastapi.tiangolo.com/tutorial/additional-responses/ |
@tiangolo Thanks for the brilliant & carefully crafted framework 🥇 , glad to be a contributor 👍 |
Thanks! 😊 🌮 🍰 |
Signed-off-by lmignon
Additional Responses Support
Summary
allows additional responses to be defined beside the default one
Example
Result
you can also define additional responses on
app.include_router
so it can save you from repeating the definition of common response types (e.g.HTTP 500
)Limitations
This pull request only implements the injection of
additional responses definitions into OpenAPI schema
to be appeared in swagger or other OpenAPI 3 compatible
clients (e.g. postman), here are some of the limitations:
headers
&examples
).