Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite/extend FastAPI - APIRouter route #4898

Closed
9 tasks done
efirvida opened this issue May 12, 2022 · 3 comments
Closed
9 tasks done

Overwrite/extend FastAPI - APIRouter route #4898

efirvida opened this issue May 12, 2022 · 3 comments
Labels
question Question or problem question-migrate

Comments

@efirvida
Copy link

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

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")

Description

I copy here my thread in StackOverflow

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.

I have also found an interesting thing in the generated API /docs that maybe can be a bug. The sample code generates this doc, which seems to be OK to me:

enter image description here

But... the endpoint returns the wrong response:
At this point, I spect to have a "hello world 2!" response from new_index not a "hello world!" from index

Following a suggestion in the StackOverflow comment, I found this solution which I'm not happy with

class Router(APIRouter):
    def add_api_route(...) -> None:
        ...

        route_index = self._get_route_index_by_path(route.path)
        if route_index >= 0:
            self.routes[route_index] = route
        else:
            self.routes.append(route)

    def _get_route_index_by_path(self, path: str) -> int:
        for index, route in enumerate(self.routes):
            if route.path == path:
                return index
        return -1


app = FastAPI()
router = Router()
....

I mean I'm not happy because I can´t find a good way to replace the route, since the __eq__ to check if the route is in the list is on starlette.routing.Route so this was a simplest solution that I Found

Operating System

Linux

Operating System Details

Fedora 35

FastAPI Version

0.77.1

Python Version

Python 3.10.4

Additional Context

No response

@efirvida efirvida added the question Question or problem label May 12, 2022
@raphaelauv
Copy link
Contributor

raphaelauv commented May 13, 2022

there is a wip about routes -> #4794

@efirvida
Copy link
Author

@raphaelauv but I don´t direct a solution for my case in that PR. In the documentation of the PR I read something about supporting the same endpoint with different dependencies, that is OK, and I think is very important but in my case, I want to overwrite the endpoint.

@rbrown021
Copy link

I am doing something similar where I am making some plugins with their own routes and models. But I want whoever uses them to be able to extend the models and routes and update the routes. This would be super helpful!

Repository owner locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8489 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants