'How to link path from OpenAPI json file to a python function in FastAPI

How to link path from OpenAPI JSON file to a python function in FastAPI.

Something I came to know about from StackOverflow was that we can use an external OpenAPI JSON file with fastapi using the following approach

app = FastAPI()

def custom_openapi():
    with open("api_spec.json", "r") as openapi:
        return json.load(openapi)


app.openapi = custom_openapi

The API spec has a single endpoint with parameters and their types.

How can I map the endpoint to a python function and get access to the parameters?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source