Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blueprint Middleware runs before App Middleware #2647

Open
1 task done
simon-lund opened this issue Jan 4, 2023 · 0 comments
Open
1 task done

Blueprint Middleware runs before App Middleware #2647

simon-lund opened this issue Jan 4, 2023 · 0 comments
Labels

Comments

@simon-lund
Copy link

simon-lund commented Jan 4, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

With the code below, the bp middleware runs before the app/global middleware. Can you explain why?

I expected the middleware to be registered on app level and the bp middleware on bp level
such that the app middleware is always executed first and the bp middleware as soon as the bp is "entered" on a request.

I know that this is theoretically solved by adding priority, but this behavior is not mentioned in the docs and feels like a bug because it contradicts intuition of data flow and locality.

Related Issue: #2636

Code snippet

from sanic import Sanic, Blueprint, text

bp = Blueprint('test')


@bp.get('/')
def hello_world(request):
    return text("Hello World")


@bp.on_request
def bp_middleware(request):
    print("Blueprint Middleware")


app = Sanic(__name__)


@app.on_request
def app_middleware(request):
    print("App Middleware")


app.blueprint(bp)

if __name__ == '__main__':
    app.run("localhost", port=8000, debug=True, dev=True, auto_reload=True)

Expected Behavior

Expected the app middleware to wrap the bp middleware

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Ubuntu

Sanic Version

22.9.1

Additional context

No response

@simon-lund simon-lund added the bug label Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant