Using blueprint.copy leads to warns about duplicate routes
dhensen opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When I copy a blueprint, I get warned about duplicate routes and uniqueness of them being enforced in v23.3. This seems out of place since I'm using a documented feature according to: https://sanic.dev/en/guide/best-practices/blueprints.html#copying.
DeprecationWarning: [DEPRECATION v23.3] Duplicate route names detected: copying_blueprint_warns_about_duplicate_route.Version1.something. In the future, Sanic will enforce uniqueness in route naming.
warn(version_info + message, DeprecationWarning)
I think the copy method should ensure uniqueness of copied routes or some instructions must be added to the docs to tell the dev how to solve this issue otherwise.
I'm up for doing a PR to fix this if I you point me in right direction.
Code snippet
from sanic import Sanic, Blueprint
app = Sanic(name='copying_blueprint_warns_about_duplicate_route')
v1 = Blueprint("Version1", version=1)
@v1.route("/something")
def something(request):
pass
v2 = v1.copy("Version2", version=2)
app.blueprint(v1)
app.blueprint(v2)
if __name__ == '__main__':
app.run(debug=False, auto_reload=False, fast=True)
Expected Behavior
I expect out of the box functionality such as copying a blueprint (as advertised in the docs) to work without giving me a deprecation notice that is a bit alarming.
But what does enforce uniquess mean? Will the Blueprint.copy method solve the issue or will my routing break? No quite clear what will happen.
Sanic Version
v22.12
Sounds like a duplicate of this: #2554
Would love to get a fix in for this in next release. Which means practically we would need the PR done by early next week. I think @ChihweiLHBird was looking at this. I'd suggest coordinate with him to see if he's done anything on it.
@ahopkins I think this is about how the sanic router detect and (will) remove duplicated routes (enforce uniqueness). For the issue #2554, after some minimal modifications, sanic can let the blueprint accept overwritten routes, but it still shows the notification of the deprecation for this case. Basically, I think we should consider the version_prefix
of the blueprint as a part of the route when enforcing route uniqueness.
still shows the notification of the deprecation for this case
v23.3 deprecation of duplicate route names? Deprecation removals is the last thing I usually tackle before release. On my list for the week.
Basically, I think we should consider the
version_prefix
of the blueprint as a part of the route when enforcing route uniqueness.
There are two uniqueness requirements: path and name. The name is what I was talking about above and what I think you are talking about as well as that is the only one that has not been strictly enforced until now.
However, for route path uniqueness, that should already include the version_prefix
as it is merged into the actual path when handed off to the router.
bp1 = Blueprint("bp1", version=1)
bp2 = Blueprint("bp2", version_prefix="/something/", version=1)
@bp1.get("/")
@bp2.get("/")
async def handler(request: Request):
return json({"foo": "bar"})
This is legit and should not fail.