I want to be able to exclude all routes of an APIRouter from documentation.
There's a way to do this for individual routes, but having a way to apply this option to all subroutes is more maintainable.
instead of this:
from fastapi import FastAPI
app = FastAPI()
foo_api = APIRouter()
app.include_router(foo_api, prefix='/foo')
@foo_api.get('/', include_in_schema=False)
async def get_foo():
pass
@foo_api.post('/', include_in_schema=False)
async def add_foo():
pass
I want this:
from fastapi import FastAPI
app = FastAPI()
foo_api = APIRouter(include_in_schema=False)
app.include_router(foo_api, prefix='/foo')
@foo_api.get('/')
async def get_foo():
pass
@foo_api.post('/')
async def add_foo():
pass
A naive solution would be just defaulting to self.include_in_router if it's not specified as argument in fastapi.routing.APIRouter.add_api_route
Windows 10
fastapi==0.58.0
Python 3.8.1
Created a PR that implements your naive solution. Seems like a great feature :smile:
I was also looking for this. This would be a great addition to have and helps follow the DRY principle.
@anindyamanna There are still not comments by @tiangolo on it, so for now you can do as follows:
from typing import Callable
from fastapi import APIRouter, FastAPI
from fastapi.routing import APIRoute
app = FastAPI()
class PotatoRoute(APIRoute):
def __init__(self, path: str, endpoint: Callable, **kwargs):
kwargs["include_in_schema"] = False
super().__init__(path, endpoint, **kwargs)
router = APIRouter(route_class=PotatoRoute)
@router.get("/")
def home():
return "Hello World!"
app.include_router(router, prefix="/potato")
Most helpful comment
Created a PR that implements your naive solution. Seems like a great feature :smile: