From 4115a165aa5b95b78cceb5b393d17200e1115bf9 Mon Sep 17 00:00:00 2001 From: rechain Date: Wed, 25 Feb 2026 23:03:49 -0500 Subject: [PATCH] asyncapi link bug --- fastapi/applications.py | 2 +- tests/test_asyncapi.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 84a951c843..2b6ff05bf2 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -1222,7 +1222,7 @@ class FastAPI(Starlette): if oauth2_redirect_url: oauth2_redirect_url = root_path + oauth2_redirect_url asyncapi_docs_url = None - if self.asyncapi_docs_url: + if self.asyncapi_url and self.asyncapi_docs_url: asyncapi_docs_url = root_path + self.asyncapi_docs_url return get_swagger_ui_html( openapi_url=openapi_url, diff --git a/tests/test_asyncapi.py b/tests/test_asyncapi.py index 5ce436348c..5dff11cb0c 100644 --- a/tests/test_asyncapi.py +++ b/tests/test_asyncapi.py @@ -402,3 +402,28 @@ def test_get_asyncapi_direct(): assert schema["asyncapi"] == "2.6.0" assert schema["info"]["title"] == "Test API" assert "/ws" in schema["channels"] + + +def test_asyncapi_url_none_no_link_in_swagger(): + """Test that Swagger UI doesn't show AsyncAPI link when asyncapi_url is None.""" + app = FastAPI( + title="Test API", + version="1.0.0", + asyncapi_url=None, # Explicitly disabled + # asyncapi_docs_url defaults to "/asyncapi-docs" + ) + + @app.websocket("/ws") + async def websocket_endpoint(websocket: WebSocket): + await websocket.accept() + await websocket.close() + + client = TestClient(app) + # Swagger UI should not show AsyncAPI link when asyncapi_url is None + response = client.get("/docs") + assert response.status_code == 200, response.text + assert "/asyncapi-docs" not in response.text + + # AsyncAPI endpoint should not exist + response = client.get("/asyncapi-docs") + assert response.status_code == 404