question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query parameters from Depends do not show description in docs

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import Depends, FastAPI, Query
from pydantic import BaseModel, Field

app = FastAPI()


class CommonParams(BaseModel):
    a: int = Field(
        1,
        description="Description does not show up in docs",
    )
    b: int = Query(2, description="Using query changes nothing")


@app.get("/")
async def get_root(
    params: CommonParams = Depends(),
    q: int = Query(5, description="This description shows up"),
):
    return {
        "a": params.a,
        "b": params.b,
        "q": q,
    }

Description

Open browser, go to docs page, look at parameters for default route.

Note that description shows up for q parameter, but not for a or b. The schema for a and b in openapi.json does not have a description, but q does.

I expect the description for the fields to show up.

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.75.0

Python Version

3.8.11

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
jdratlifcommented, Mar 25, 2022

This is a workaround, and I see that it works with python dataclasses, too, but I still think it should work with pydantic models.

from dataclasses import dataclass

from fastapi import Depends, FastAPI, Query

app = FastAPI()


@dataclass
class CommonParams:
    a: int = Query(1, description="describes A")
    b: int = Query(2, description="Describes B")


@app.get("/")
async def get_root(
    params: CommonParams = Depends(),
    q: int = Query(5, description="This description shows up"),
):
    return {
        "a": params.a,
        "b": params.b,
        "q": q,
    }
3reactions
long2icecommented, Jun 8, 2022

By reading source code I found solution:

class CommonParams(BaseModel):
    a: int = Field(
        Query(...,example=1,description="Description does not show up in docs",
    )
    b: int = Query(
        Query(..., example=2, description="Using query changes nothing")
    ) # this is also ok

The code is here: https://github.com/tiangolo/fastapi/blob/master/fastapi/dependencies/utils.py#L361

Which is not so obvious, but really effective.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set description for query parameter in swagger doc using ...
Am I missing anything to get the description for QueryParams(model) in swagger ui? python · fastapi · Share.
Read more >
Use query parameters to customize responses - Microsoft Graph
Microsoft Graph provides optional query parameters that you can use to specify and control the amount of data returned in a response.
Read more >
Create & use named functions - Google Docs Editors Help
Named functions let you create custom functions that can use built-in Sheets ... These arguments are placeholders and descriptions of the type of...
Read more >
Query Parameters and String Validations - FastAPI
This will validate the data, show a clear error when the data is not valid, and document the parameter in the OpenAPI schema...
Read more >
Parameters - Cypher Manual - Neo4j
Setting parameters when running a query is dependent on the client ... For more information refer to Operations Manual → Cypher Shell -...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found