DeanWay / fastapi-versioning

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

AttributeError: 'APIWebSocketRoute' object has no attribute 'methods' #38

Open cheesycod opened 3 years ago

cheesycod commented 3 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] Yes, I want to use this with a api with Websockets as well Describe the solution you'd like A clear and concise description of what you want to happen. Websocket support in fastapi versioner Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered. I right now have my own middleware to handle these things but this looks better Additional context Add any other context or screenshots about the feature request here.

smihaila commented 3 years ago

Hi,

I'm looking for the same feature, namely the ability to decorate a @fastapi.APIRouter.websocket("/path") route with @fastapi_versioning.version(major, minor).

Currently getting the same error as cheesycod.

Actually, even without decorating a WebSocket-based FastAPI endpoint with @version(major, minor), and having such decorators strictly at REST / non-WebSocket routes level, I'm still getting the same error (AttributeError: 'APIWebSocketRoute' object has no attribute 'methods') in the line that creates a VersionedFastAPI instance.

... and to mitigate the error, I have to re-structure the routers registration (i.e. FastAPI.include_router() calls) so that any WebSocket-based router comes AFTER creating the VersionedFastAPI instance. Like this:

app: FastAPI = FastAPI(title=APP_NAME, ...)
app.include_router(some_non_websocket_router)

app: VersionedFastAPI = VersionedFastAPI(app, ...)
app.add_middleware(...)

app.include_router(some_websocket_router)

Thank you.