-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
bug: route without a trailing slash gets routed to route with a trailing slash, when the route is the top level of blueprint (maybe also app) #2080
Comments
Probably needs a bugfix release since routes like |
Hi @argaen But I don't understand your second comment, how would a route like |
And are you sure this is just Blueprints? Wouldn't this also affect app routes? app = Sanic("test")
@app.get("", strict_slashes=True)
def _(req):
return text("a")
@app.get("/", strict_slashes=True) # <- does this cause exception?
def _(req):
return text("b") Or is it something to do with how blueprints prepend |
Like this example from sanic import Sanic, Blueprint
from sanic.response import text
app = Sanic("test")
bp = Blueprint("test", url_prefix="test")
@bp.get("", strict_slashes=True)
def _(req):
return text("a")
app.blueprint(bp)
app.run() Old:
New:
The problem is the line illustrated in the fix turning "" into "/" when its an empty string |
Im not too sure on the logic between the difference between the host root path with the slash or not, like localhost:8000 vs localhost:8000/ I don't know if in networking terms there is a difference, so can't answer on whether it should affect app or not with no url_prefix |
Ok, yep I absolutely see how this is an issue on a blueprint when the route is Putting that aside, it seems like we need a condition like:
I will have to do some more testing to get to a correct solution for this. |
Ok sounds good, will leave it to sanic core devs to decide about how to handle it ... Probably a poor example on my end but basically with that I meant if |
Feel free to close & replace my PR if its inadequate, I just made it in the case that it would be a quick fix |
Thinking about it a bit more:
@ahopkins is the guru for everything regarding the new router, I'll leave it to him. |
I am looking at this now. Looks like @ashleysommer has the right idea. Basically, this should work as they should be functionally equivalent: app = Sanic(__name__)
bp = Blueprint("test", url_prefix="/bar")
@bp.get("", strict_slashes=True)
def bar(req):
return text("bar")
@bp.get("/", strict_slashes=True)
def Bar(req):
return text("Bar")
@app.get("/foo", strict_slashes=True)
def foo(request):
return text("foo")
@app.get("/foo/", strict_slashes=True)
def Foo(request):
return text("Foo") |
Running some tests, but I think it is as simple as this: if not uri.startswith("/") and (uri or hasattr(self, "router")):
uri = "/" + uri If there is a If uri is falsey (in this case it should only be Now, why am I using |
Version: 21.3.1
Description:
setting two urls in a blueprint with strict slashes for "" and "/" will raise an exception "route already registered" in sanic 21.3.1
In sanic version before 21.3 this would be acceptable
Code to reproduce:
potential fix: #2079
The text was updated successfully, but these errors were encountered: