Yes, FastAPI/OpenAPI supports multiple response models. You can define multiple response models in the OpenAPI specification using the responses
section with different response codes. For example:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
price: float
class ErrorResponse(BaseModel):
message: str
@app.get("/items/{item_id}")
async def read_item(item_id: int):
if item_id == 0:
return {"error": "item_id must be greater than zero"}
elif item_id == 1:
return ErrorResponse(message="Item not found")
else:
return Item(name="Item " + str(item_id), price=1.23)
In the example above, we define two response models: Item
and ErrorResponse
. We use Item
as the response model for successful requests and ErrorResponse
as the response model for error requests. The responses
section in the OpenAPI specification will show the different models for each possible response code.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-02-22 11:00:00 +0000
Seen: 10 times
Last updated: Jun 27 '22
How can the approval status of a SharePoint List item be updated using Power Automate?
How can I use flexbox to achieve an even amount of empty space at either end?
How can the last item be displayed fully while only half of the right item is shown in slick slider?
What does it mean to select an item on the display by clicking it?
What is the process for removing an item from the Laravel session?
How to obtain a Null value in combo boxes?
What is the process for selecting items in a list that appear only once?
How can I send an array of objects to a controller in ASP.NET 6 (Core)?