spectree
spectree copied to clipboard
feat: If a connection is upgraded to websocket, it should by default skip validation
Describe the bug
Currently the skip validation is set to False by default, and if user forget to turn this feature on for websocket, it will throw user some socket closed error when the flask app is served through gunicorn.
This is slightly confusing, and the error message is not very clear.
To Reproduce
@app.route("/position", websocket=True)
@spec.validate()
def stream() :
ws = simple_websocket.Server.accept(request.environ)
try:
while True:
ws.send(json.dumps({"x":1,"y":2}))
time.sleep(0.05)
except simple_websocket.ConnectionClosed:
pass
class WebSocketResponse(Response):
def __call__(self, *args, **kwargs):
if ws.mode == 'eventlet':
try:
from eventlet.wsgi import WSGI_LOCAL
ALREADY_HANDLED = []
except ImportError:
from eventlet.wsgi import ALREADY_HANDLED
WSGI_LOCAL = None
if hasattr(WSGI_LOCAL, 'already_handled'):
WSGI_LOCAL.already_handled = True
return ALREADY_HANDLED
elif ws.mode == 'gunicorn':
raise StopIteration()
elif ws.mode == 'werkzeug':
return super().__call__(*args, **kwargs)
else:
return []
return WebSocketResponse()
Expected behavior
by default it should auto skip the validation for websocket, or it should raise more meaningful error.
The spectree version
1.4.11
Additional context
No response
Hi @smeng9, thanks for your feedback. Are you using any Flask extensions for WebSocket support?