'How to overwrite/extend FastAPI - APIRouter route

this question is related to FastAPI with APIRouter plugin system not working , just to give a little bit of context...

So... I'm trying to create a simple pluggable FastAPI application where plugins can add, or not, API endpoints. Now is working fine so every plugin adds its own API endpoints. But what if a plugin can extend other plugins, to add or modify its endpoints...

For that, I build a plugin graph to handle the plugin dependencies and now a plugin can import the router from its dependencies and add some new endpoints. But I can´t overwrite the existing endpoints with the new routes.

To make it simpler I wrote this piece of code to simulate this:

import uvicorn
from fastapi import APIRouter, FastAPI

router = APIRouter()


@router.get("/")
def index():
    return "hello world!"


@router.get("/")
def new_index():
    return "hello world 2!"


app = FastAPI()
app.include_router(router)

if __name__ == "__main__":
    uvicorn.run("server:app")

At this point, I would expect to have the hello world 2! response for the / route but instead of that I have the hello world!

UPDATE

I have also found another interesting thing in the generated api /docs that maybe can be a bug... (I update the routes functions name, to clarify). The code generates this doc... So I may think that it's Ok!. At least the documentation was built from the new_index function

enter image description here

But as I was mentioned before the response is from index



Sources

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

Source: Stack Overflow

Solution Source