Skip to content

use annotated description as Field description#9670

Closed
vincentsarago wants to merge 1 commit intofastapi:masterfrom
vincentsarago:AnnotatedDescription
Closed

use annotated description as Field description#9670
vincentsarago wants to merge 1 commit intofastapi:masterfrom
vincentsarago:AnnotatedDescription

Conversation

@vincentsarago
Copy link

ref: #9278

History

Since FastAPI 0.95 we cannot have multiple endpoint routes with optional Path parameter (because we cannot mix the Query/Path for a variable: 375513f#r105161665), while it seems this breaking change is assumed, I personally still need this feature.

To solve this, I have to remove the usage of Query/Path class and use simple Annotated form, but this means that I loose the nice OpenAPI kwargs provided by FieldInfo.

Proposed change

With this PR I'm proposing that we use the first metadata annotation as Field's description

from typing import Annotated
from fastapi import FastAPI

app = FastAPI()

@app.get("/test")
async def variable(foo: Annotated[str,  "get a description"] = "bar"):
   return {"foo": foo}

in ☝️ "get a description" will be forwarded to the OpenAPI schema as field's description 👇

Screenshot 2023-06-13 at 6 17 58 PM

I originally designed a more complex solution over #9289, but I think what I'm proposing with this PR is much more simple (thus acceptable ? 😅).


response = client.get("/test1/baz")
assert response.status_code == 200
assert response.json() == {"foo": "baz"}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is kinda not related to the PR's goal but I felt it was important to have this test to make sure this feature (supporting optional path) is supported!

@tiangolo
Copy link
Member

📝 Docs preview for commit 60d163d at: https://648898509e9ccd2141608062--fastapi.netlify.app

@vincentsarago
Copy link
Author

Is there anything I need to do here? 🙏

@tiangolo tiangolo added feature New feature or request investigate labels Oct 2, 2023
@alejsdev alejsdev added p4 and removed investigate labels Jan 15, 2024
@github-actions github-actions bot added the conflicts Automatically generated when a PR has a merge conflict label Sep 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2025

This pull request has a merge conflict that needs to be resolved.

@tiangolo
Copy link
Member

This could cause conflicts with a bunch of other behaviors. I'll need to handle this later in a different way. For now, I'll close this one. ☕

@tiangolo tiangolo closed this Feb 15, 2026
@tiangolo
Copy link
Member

This could cause conflicts with a bunch of other behaviors. I'll need to handle this later in a different way. For now, I'll close this one. ☕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict feature New feature or request p4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants