Fastapi: Exclude all routes from schema under an APIRouter

Created on 30 Jun 2020  路  4Comments  路  Source: tiangolo/fastapi

First check

  • [x] I added a very descriptive title to this issue.
  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] 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.
  • [x] 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.
  • [x] After submitting this, I commit to:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • Or, I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Example

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

The solution you would like

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

https://github.com/tiangolo/fastapi/blob/e5594e860f34c939d530640b1c240ed7f64003b1/fastapi/routing.py#L369

Environment

Windows 10
fastapi==0.58.0
Python 3.8.1
enhancement

Most helpful comment

Created a PR that implements your naive solution. Seems like a great feature :smile:

All 4 comments

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")
Was this page helpful?
0 / 5 - 0 ratings