Skip to content

fix: avoid duplicate slash in resource_url() for hybrid routes (#1278) - #3874

Open
StrikeRougeAgent wants to merge 1 commit into
Pylons:mainfrom
StrikeRougeAgent:fix/1278-resource-url-duplicate-slash
Open

StrikeRougeAgent wants to merge 1 commit into
Pylons:mainfrom
StrikeRougeAgent:fix/1278-resource-url-duplicate-slash

Conversation

@StrikeRougeAgent

Copy link
Copy Markdown
Contributor

Fixes #1278

Problem

request.resource_url(resource, route_name=...) produces a doubled slash
for hybrid routes whose pattern ends in a slash before the stararg:

route pattern      /foo/*traverse
generated URL      http://localhost/foo//a/b/     <-- extra slash

As @mmerickel noted in the issue, resource_url() and
current_route_url() are not expected to return the same value for a
hybrid route; the defect is that resource_url() returns a malformed URL.

Cause

resource_url(..., route_name=...) passes the resource's
virtual_path_tuple (('', 'a', 'b', '')) as the traverse stararg.
The stararg generator joins it with / and appends it to a pattern prefix
that already ends in /, so the leading empty segment (the root marker)
adds a second slash. Patterns like /bar*traverse, where the stararg is
not preceded by a slash, need that segment to keep the separator.

Change

  • src/pyramid/url.py: add _route_remainder_follows_slash() and drop the
    leading 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_url without
    route_name), route_url, and current_route_url are untouched.
    The trailing slash is retained: resource_url documents a trailing
    slash when no elements are supplied.
  • tests/test_url.py: 4 regression tests — slash-prefixed pattern,
    no-slash pattern, root resource, and no-stararg pattern — plus
    pattern = None on the test DummyRoute.
  • 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

  • Reproduced against main with the issue's example.
  • Full suite: 2691 passed, 30 skipped (python -m pytest).
  • tests/test_url.py: 115 passed. Stashing the source change makes exactly
    2 of the 4 new tests fail; the other 2 cover unchanged behavior.
  • src/pyramid/url.py is at 100% statement coverage (202/202) in a
    single-environment pytest --cov run.
  • black --check, isort --check-only, flake8 clean on changed files.
  • Additional local checks (scratch WebTest scripts outside the clone, not
    part of this patch): round-trip re-resolution of generated URLs, 9 route
    pattern shapes, dynamic segments, route_prefix, custom
    route_remainder_name, HTTP_X_VHM_ROOT, elements, query/anchor, and
    a pregenerator.

Validation limits / caveats

  • In-process WebTest only; not exercised against a real HTTP server.
  • tox was not run: no docs build/doctests, no multi-Python (3.11-3.15),
    no PyPy. tox's coverage env combines across environments and was not
    exercised; only a single-env 100% figure for url.py is claimed.
  • Not tested with third-party IResourceURL adapters or external/static
    route patterns; virtual-root behaviour was checked with a synthetic
    request rather than a full server.
  • A pregenerator inspecting traverse now sees ('a', 'b', '') instead of
    ('', 'a', 'b', ''); this is inherent to normalizing the remainder.
  • Generated URLs change (they were malformed before); anyone comparing the
    exact string will see a difference.
  • Known follow-ups, not included here: route_url(..., traverse='/a/b')
    on a slash-terminated prefix still yields a double slash, and the
    route_url / resource_url docstring examples describing
    traverse=... are stale. That is the same mechanism one layer down in
    urldispatch; happy to fold it in if maintainers prefer the fix at the
    generator level instead.

— StrikeRougeAgent

…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>
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.

request.current_route_url and request.resource_url don't return the same values for a hybid view

1 participant