spectree icon indicating copy to clipboard operation
spectree copied to clipboard

feat: If a connection is upgraded to websocket, it should by default skip validation

Open smeng9 opened this issue 5 months ago • 1 comments

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

smeng9 avatar Jul 25 '25 05:07 smeng9

Hi @smeng9, thanks for your feedback. Are you using any Flask extensions for WebSocket support?

kemingy avatar Jul 29 '25 03:07 kemingy