Skip to content

Try it out button not working using root_path with ApiGateway #5838

@EvanderDeLeon

Description

@EvanderDeLeon

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

#main.py

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

from test_api.router import *

logger = logging.getLogger(__name__)

app = FastAPI(
    title="test_API",
    root_path="/test",
)

app.include_router(router)

description = """
Test API overview
"""

def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Test API",
        version="0.0.1",
        description=description,
        routes=app.routes,
    )
    openapi_schema["info"]["x-logo"] = {
        "url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema

# assign the customized OpenAPI schema
app.openapi = custom_openapi

#router.py

import logging

from fastapi import APIRouter

logger = logging.getLogger(__name__)

router = APIRouter(
    prefix="/test",
)

@router.get(
    "/health_check",
)
def root():
    return {"status": "Success"}

Description

We are using FastAPI alongside ApiGateway on AWS. We have multiple APIs being handled by one Custom domain name so we are required to use API mappings with specific Paths.

With the configuration shown all the routes for the API works as well as the documentation or getting the openapi.json file. The only issue is when trying to execute the API from the docs page with the "Try it out" option. Is not adding the prefix so every request is ending up with a 404 error.

image

We tried working with routes as well, but this ends up duplicating the prefix and we don't want that.

After more testing we found that the issue could be related to the custom_openapi function, if we remove it, the try it out button works. But the rest of the docs does not.

Is there any kind of parameter we could use inside this definition to achieve the addition of the prefix only to the execute part without having the need to add a router since this causes the prefix to be duplicated.

The request would look like this:

curl --location --request GET 'http://127.0.0.1:9999/test/test/health_check'

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.88.0

Python Version

Python 3.8

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions