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.

How to add examples for parameters for swagger docs, from the endpoint definition in .py?

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.
  • After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Example

Here’s a self-contained, example with my use case. :

from fastapi import FastAPI

app = FastAPI()

@app.post("/{name}")
def read_root(name):
    """
    Some descriptive doc-string
    """
    return f"Hello {name}"

Description

  • Open the browser and call the endpoint /.
  • Navigate to the /docs
  • note that swagger can allow you to set examples for the query param, but there is no obvious way to do this with swagger docs generated from fast api.

Environment

osx, plus from pip freeze:

certifi==2020.12.5
click==7.1.2
fastapi==0.63.0
h11==0.12.0
pydantic==1.8
starlette==0.13.6
typing-extensions==3.7.4.3
uvicorn==0.13.4
  • Python version: 3.8

Additional context

Is there some pattern with pydoc or something with setting examples that I am mising? or with pydantic?

I would like to be able to use the swagger docs for some endpoint development, so it would be great to be able to use swagger to remember an example UUID for example, so speed up the tests, and let me skip duplicating all the endpoints to postman.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
joshjmcommented, Mar 4, 2021

ahh yep; a one two punch of couldnt find docs on making a single example, + then wanting what the PR provides. I’ll close and mark as dupe(would if i could). thanks for your help! i’ll try and make some comments on that pr

1reaction
Kludexcommented, Mar 3, 2021

I don’t remember if this is written somewhere… I don’t think it is.

from fastapi import FastAPI, Path

app = FastAPI()

@app.post("/{name}")
def read_root(name: str = Path(..., example="I can't believe is that easy!")):
    """
    Some descriptive doc-string
    """
    return f"Hello {name}"

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Describing Parameters
In OpenAPI 3.0, parameters are defined in the parameters section of an operation or path. ... For example, the /users/{id} endpoint would be...
Read more >
Documenting your API with Swagger - Flask-RESTPlus
This decorator allows you specify some details about your API. They will be used in the Swagger API declarations. You can document a...
Read more >
Swagger UI tutorial | Documenting APIs
Swagger UI provides various configuration parameters (unrelated to the OpenAPI parameters) that you can use to customize the interactive display ...
Read more >
Creating a Hello World API using Swagger UI and Python
The parameters of the Swagger UI document such as document title, version, description etc. are defined within the info field. The base URL...
Read more >
flasgger/README.md at master
Easy OpenAPI specs and Swagger UI for your Flask API - flasgger/README.md at master ... PUT, PATCH is valid against the schema defined...
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