fastapi-cli icon indicating copy to clipboard operation
fastapi-cli copied to clipboard

:technologist: Add conditional printing API docs URLs in panel

Open FlavienRx opened this issue 1 year ago • 9 comments

Description:

This PR refactors the FastAPI server start-up logic to improve the display of the application’s documentation URLs in the console. It introduces a conditional check for app.openapi_url, ensuring that only defined documentation URLs are printed. The updated logic adds more flexibility to display either docs_url, redoc_url, or both, enhancing visibility for users when serving the API.

Changes:

  • Updated import handling with get_import_string_and_app to retrieve both import_string and app for improved compatibility with uvicorn.
  • Enhanced serving_str output formatting to conditionally display API documentation URLs based on availability.
  • Cleaned up conditional print logic to handle cases where only one or both of docs_url and redoc_url are available.

This adjustment improves user experience by making API documentation links visible and accessible only when they are set.

Examples: image image image

FlavienRx avatar Nov 09 '24 22:11 FlavienRx

hi i had the same issue with custom docs urls showing wrong url in the logs, i think the latest version of the fastapi_cli code is changed from when you worked this on it, cli module now uses ImportData to get import string and other app module data i think it's better to add the fastapi app as a field to ImportData and use it on cli module instead of returning a tuple

sehraramiz avatar Dec 16 '24 06:12 sehraramiz

Hi @sehraramiz

You right, I started to refactor my code but I run out of time to finish and push my code 😄

FlavienRx avatar Dec 16 '24 08:12 FlavienRx

@sehraramiz

I did the job, let me know what do you think.

@tiangolo, what do you think about this feature ?

FlavienRx avatar Dec 16 '24 19:12 FlavienRx

A new label has to be added:

Label check failed: required 1 of 'breaking', 'security', 'feature', 'bug', 'refactor', 'upgrade', 'docs', 'lang-all', 'internal', but found 0.

Stargator avatar Apr 18 '25 00:04 Stargator

This resolves #162

Stargator avatar Apr 18 '25 00:04 Stargator

Would this work if I disable the URLs entirely? In my case I depend on an env var:

app = FastAPI(
    lifespan=lifespan,
    docs_url="/docs" if DEV_MODE else None,
    redoc_url="/redoc" if DEV_MODE else None,
    openapi_url="/openapi.json" if DEV_MODE else None
)

eden881 avatar Aug 03 '25 22:08 eden881

@eden881 Yep, I had some tests to test this case.

FlavienRx avatar Aug 05 '25 07:08 FlavienRx

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

github-actions[bot] avatar Sep 17 '25 19:09 github-actions[bot]

I rebased my branch again.

Do you have any feedback @tiangolo @Kludex @YuriiMotov @svlandeg ? Or can you label this PR ?

FlavienRx avatar Nov 05 '25 17:11 FlavienRx