Add option to create openapi 3.0 links for generated schemas #6410
Replies: 2 comments
-
|
Well after a little more searching and messing around with this, I managed to implement a link in FastAPI. then found out that this feature is not yet implemented in swagger-UI! swagger-api/swagger-ui#3641
the code i used to create the link: from fastapi import FastAPI
from pydantic import BaseModel, Field
class CreateSubItem(BaseModel):
id: int
name: str
class CreateItem(BaseModel):
id: int
name: str
sub_item_id: int = Field(..., name="sub_item_id")
app = FastAPI()
links = {
"GetSubItemId": {
"operationId": "create_item_item_post",
"parameters": [{"sub_item_id": "$response.body#/id"}],
"description": "The `id` value returned in the response can be used as the `sub_item_id` value in `POST /item`",
}
}
@app.post(
"/subitem/",
status_code=201,
responses={201: {"description": "Created", "links": links, "model": CreateSubItem}},
)
async def create_sub_item(subitem: CreateSubItem):
return {"id": subitem.id, "name": subitem.name}
@app.post("/item")
async def create_item(item: CreateItem):
return {"id": item.id, "name": item.name, "sub_item_id": item.sub_item_id}It will be rendered as: will close the issue for now. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Thanks for reporting back and closing the issue 👍
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
with OpenAPI 3.0 links, we can describe how various values returned by one operation can be used as input for other operations as described here.
I checked the documentation and GitHub issues but couldn't find anything related to this.
so in the code example i provided, we could get the returned
CreateSubItem.idfor the next request when we create anItemasCreateItem.sub_item_id.Wanted Solution
It would be great if we could define these kinds of links somehow in the Pydantic models or any other way.
I'm sure there is a better way to implement this, but my suggestion is to define the relationship as a string to the Pydantic model. maybe as an extra keyword argument value to the Pydantic's Field.
for example
Field(..., refrences="SomeModel.id")and FastAPI pickup that and create that refrence in schema generation.Wanted Code
Alternatives
No response
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.10.4
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions