DeanWay / fastapi-versioning

api versioning for fastapi web applications
MIT License
644 stars 63 forks source link

VersionedFastAPI causes all OPTIONS requests to return a 405 #23

Open RA-Zagros opened 3 years ago

RA-Zagros commented 3 years ago

Describe the bug When using VersionedFastAPI all OPTIONS requests return a 405.

app = FastAPI(title='APP NAME')
APP_VERSION = "1.1.1"
app.add_middleware(
    CORSMiddleware,
    allow_origin_regex='https?://.*',
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)
app.include_router(api_router_v1_0, prefix=config.API_V1_STR)
app.include_router(api_router_v1_1, prefix=config.API_V1_STR)
app = VersionedFastAPI(app)

To Reproduce Steps to reproduce the behavior:

  1. initialize app using VersionedFastAPI
  2. make a request that requires CORS options
  3. view response details
  4. returns a 405

Expected behavior Options request to pass as expected

sammaphey commented 3 years ago

I had to add the middleware after Declaring the VersionedFastAPI. So this should hopefully fix it:

app = FastAPI(title='APP NAME')
APP_VERSION = "1.1.1"
app.include_router(api_router_v1_0, prefix=config.API_V1_STR)
app.include_router(api_router_v1_1, prefix=config.API_V1_STR)
app = VersionedFastAPI(app)
app.add_middleware(
    CORSMiddleware,
    allow_origin_regex='https?://.*',
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)