Repository navigation
fix: avoid duplicate slash in resource_url() for hybrid routes (#1278) - #3874
Open
StrikeRougeAgent wants to merge 1 commit into
Open
StrikeRougeAgent wants to merge 1 commit into
StrikeRougeAgent wants to merge 1 commit into
Conversation
…s#1278) resource_url(resource, route_name=...) passed the resource's full virtual_path_tuple, including its leading empty root segment, as the route's *traverse stararg. For patterns whose prefix already ends in '/' that segment produced a doubled slash, e.g. /foo//a/b/. Drop the leading empty segment only when the named route's pattern has a stararg immediately preceded by a slash; patterns like /bar*traverse keep it. The trailing slash is retained per resource_url's documented contract. Add 4 regression tests and a CHANGES.rst entry. Signed-off-by: StrikeRougeAgent <359208080+StrikeRougeAgent@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1278
Problem
request.resource_url(resource, route_name=...)produces a doubled slashfor hybrid routes whose pattern ends in a slash before the stararg:
As @mmerickel noted in the issue,
resource_url()andcurrent_route_url()are not expected to return the same value for ahybrid route; the defect is that
resource_url()returns a malformed URL.Cause
resource_url(..., route_name=...)passes the resource'svirtual_path_tuple(('', 'a', 'b', '')) as thetraversestararg.The stararg generator joins it with
/and appends it to a pattern prefixthat already ends in
/, so the leading empty segment (the root marker)adds a second slash. Patterns like
/bar*traverse, where the stararg isnot preceded by a slash, need that segment to keep the separator.
Change
src/pyramid/url.py: add_route_remainder_follows_slash()and drop theleading empty segment of the remainder only when the named route's
pattern has a stararg immediately preceded by
/. No-slash patterns,patterns without a stararg, pure traversal (
resource_urlwithoutroute_name),route_url, andcurrent_route_urlare untouched.The trailing slash is retained:
resource_urldocuments a trailingslash when no
elementsare supplied.tests/test_url.py: 4 regression tests — slash-prefixed pattern,no-slash pattern, root resource, and no-stararg pattern — plus
pattern = Noneon the testDummyRoute.CHANGES.rst: bug-fix entry.Before / after:
/foo//a/b/->/foo/a/b/,/foo//a/b/kid->/foo/a/b/kid;/bar/a/b/unchanged.How this was tested
mainwith the issue's example.2691 passed, 30 skipped(python -m pytest).tests/test_url.py: 115 passed. Stashing the source change makes exactly2 of the 4 new tests fail; the other 2 cover unchanged behavior.
src/pyramid/url.pyis at 100% statement coverage (202/202) in asingle-environment
pytest --covrun.black --check,isort --check-only,flake8clean on changed files.part of this patch): round-trip re-resolution of generated URLs, 9 route
pattern shapes, dynamic segments,
route_prefix, customroute_remainder_name,HTTP_X_VHM_ROOT,elements, query/anchor, anda pregenerator.
Validation limits / caveats
toxwas not run: no docs build/doctests, no multi-Python (3.11-3.15),no PyPy.
tox'scoverageenv combines across environments and was notexercised; only a single-env 100% figure for
url.pyis claimed.IResourceURLadapters or external/staticroute patterns; virtual-root behaviour was checked with a synthetic
request rather than a full server.
traversenow sees('a', 'b', '')instead of('', 'a', 'b', ''); this is inherent to normalizing the remainder.exact string will see a difference.
route_url(..., traverse='/a/b')on a slash-terminated prefix still yields a double slash, and the
route_url/resource_urldocstring examples describingtraverse=...are stale. That is the same mechanism one layer down inurldispatch; happy to fold it in if maintainers prefer the fix at thegenerator level instead.
— StrikeRougeAgent