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

Support request.url_for when only "app" scope is avaialable #2672

Merged
merged 5 commits into from
Sep 29, 2024

Conversation

Jdsleppy
Copy link
Contributor

@Jdsleppy Jdsleppy commented Aug 25, 2024

Summary

Resolve the bug (or limitation) in this related discussion where Request.url_for() cannot be called in a middleware inheriting from BaseHTTPMiddleware before the Router/call_next is invoked.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Comment on lines 187 to 180
url_pather: Starlette | Router = self.scope.get("app") or self.scope["router"]
url_path = url_pather.url_path_for(name, **path_params)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to fall back to using scope["router"] to support the case where a Router is directly fed to a TestClient, as happens in test_routing.py.

Comment on lines 1047 to 1148
if request.url == request.url_for("special"):
return PlainTextResponse("Special")
return await call_next(request)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows the main motivation for supporting request.url_for() before call_next: it allows you to handle authentication or other concerns differently depending on the URL without hardcoding paths or path prefixes.

@Kludex
Copy link
Member

Kludex commented Sep 1, 2024

Well, actually this is not only for BaseHTTPMiddleware, but for any ASGI middleware.

@Jdsleppy
Copy link
Contributor Author

Jdsleppy commented Sep 2, 2024

Well, actually this is not only for BaseHTTPMiddleware, but for any ASGI middleware.

@Kludex Let me know if you'd like the commit message changed or anything, that's no problem.

Otherwise, it's rebased and ready if you're interested in the change.

@Kludex Kludex self-assigned this Sep 23, 2024
@Kludex
Copy link
Member

Kludex commented Sep 29, 2024

As a note, we do the build_middleware_stack on the Starlette, but we don't do this "build" on the Router, and that's why calling the scope["app"] works here.

@Kludex Kludex changed the title Support request.url_for in BaseMiddleware Support request.url_for when only "app" scope is avaialable Sep 29, 2024
@Kludex Kludex enabled auto-merge (squash) September 29, 2024 09:40
@Kludex Kludex merged commit fe46d99 into encode:master Sep 29, 2024
6 checks passed
@Jdsleppy Jdsleppy deleted the discussion-2522 branch September 29, 2024 11:37
@Jdsleppy
Copy link
Contributor Author

Thank you!

nixroxursox pushed a commit to nixroxursox/starlette that referenced this pull request Sep 30, 2024
…#2672)

* Support request.url_for in BaseMiddleware

* Move test to test_requests

* Call the endpoint properly

* fix test

* order the type hint

---------

Co-authored-by: Marcelo Trylesinski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants