Skip to content

[azure-core] Fix format_url dropping trailing slash when URL template is query-string only#45366

Open
Copilot wants to merge 4 commits intomainfrom
copilot/fix-url-formatting-edge-case
Open

[azure-core] Fix format_url dropping trailing slash when URL template is query-string only#45366
Copilot wants to merge 4 commits intomainfrom
copilot/fix-url-formatting-edge-case

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

When format_url is called with a query-string-only template (e.g. ?key=value) and the base URL (resolved from path_format_arguments) has a trailing slash, the trailing slash is incorrectly stripped.

client = PipelineClientBase("{url}")
client.format_url(
    "?versionid=2026-02-25T17%3A48%3A15Z",
    url="https://storage.blob.core.windows.net/sample//a/a/"
)
# Before: https://storage.blob.core.windows.net/sample//a/a?versionid=...
# After:  https://storage.blob.core.windows.net/sample//a/a/?versionid=...

Changes

  • _base.py: Removed the unconditional .rstrip("/") from the base URL computation in format_url. This was always redundant for path stubs — _urljoin already strips the trailing slash from the base path inside its if stub_url_path: branch when actually joining path segments. For query-string-only stubs where stub_url_path is empty, _urljoin leaves the base path untouched, so the trailing slash is now correctly preserved before the ?.
  • test_pipeline.py: Added test covering the exact scenario from the issue.
  • CHANGELOG.md: Updated unreleased section.
Original prompt

This section details on the original issue you should resolve

<issue_title>[azure-core] format url edge case</issue_title>
<issue_description>when this line gets called _request.url = self._client.format_url(_request.url, **path_format_arguments) in typespec generated code

where _request.url is '?versionid=2026-02-25T17%3A54%3A29.8001208Z' and path format arguments is {"url": 'https://storage.blob.core.windows.net/sample//a/a/'} where /a/a/ is the name of the blob, we want the resulting url to be

'https://storage.blob.core.windows.net/sample//a/a/?versionid=2026-02-25T17%3A48%3A15.3980761Z'

however we are getting

'https://storage.blob.core.windows.net/sample//a/a?versionid=2026-02-25T17%3A48%3A15.3980761Z'</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…string only

Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix URL formatting for edge case in Azure Core [azure-core] Fix format_url dropping trailing slash when URL template is query-string only Feb 25, 2026
@l0lawrence
Copy link
Member

/azp run python - tables - ci

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@l0lawrence
Copy link
Member

/azp run python - tables - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@l0lawrence
Copy link
Member

/azp run python - storage - tests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@l0lawrence
Copy link
Member

ah but fixing this while fixing storage typespec, then causes tables to fail because it doesnt allow '/' in the names:
image

@l0lawrence
Copy link
Member

@copilot our tables tests are failing with this change

…only stubs

Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 25, 2026

@copilot our tables tests are failing with this change

Fixed in commit 5d8bb45. The fix is now more targeted: .rstrip("/") is only skipped when the stub URL is query-string only (no path component). For path stubs like /Tables, /Tables('mytable'), etc., the old .rstrip("/") behavior is fully preserved — so tables tests should no longer be affected.

The key logic:

# Only strip trailing slash from base when stub has a path component.
# For query-string-only stubs (e.g. "?key=val"), preserve any trailing
# slash so it appears before the query string in the final URL.
if url.split("?", 1)[0]:
    base = base.rstrip("/")
url = _urljoin(base, url)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • bing.com
    • Triggering command: /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/bin/python python -m pytest tests/test_pipeline.py -x -q --tb=short --noconftest (dns block)
  • pypi.org
    • Triggering command: /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/bin/python /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/bin/python /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/lib/python3.9/site-packages/pip/__pip-REDACTED__.py install --ignore-installed --no-user --prefix /tmp/pip-build-env-5ssyj9no/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i REDACTED -- setuptools&gt;=40.8.0 (dns block)
    • Triggering command: /home/REDACTED/work/azure-sdk-for-python/azure-sdk-for-python/.venv/bin/pip pip install isodate --quiet (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Co-authored-by: l0lawrence <100643745+l0lawrence@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 25, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • scanning-api.github.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@l0lawrence l0lawrence marked this pull request as ready for review February 25, 2026 23:04
@l0lawrence l0lawrence requested a review from lmazuel as a code owner February 25, 2026 23:04
Copilot AI review requested due to automatic review settings February 25, 2026 23:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes PipelineClientBase.format_url so a base URL’s trailing slash is preserved when the provided URL template is query-string-only (e.g. ?key=value), preventing incorrect path truncation before the ?.

Changes:

  • Removed unconditional trailing-slash stripping from base URL formatting in format_url.
  • Added a regression test covering query-string-only templates with a trailing-slash base URL.
  • Documented the fix in the unreleased changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
sdk/core/azure-core/azure/core/pipeline/transport/_base.py Stops stripping trailing slash from the formatted base URL, enabling correct join behavior for query-only templates.
sdk/core/azure-core/tests/test_pipeline.py Adds a regression test to ensure trailing slashes are preserved for query-only URL templates.
sdk/core/azure-core/CHANGELOG.md Notes the bug fix in the unreleased “Bugs Fixed” section.


def test_format_url_trailing_slash_preserved_with_query_only():
# Test that trailing slash in base URL is preserved when url_template is query-string only
# https://github.com/Azure/azure-sdk-for-python/issues/XXXXX
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

The comment references a placeholder issue link (issues/XXXXX). Please replace it with the actual issue/PR number (per the PR description this appears to be #45365) or remove the link to avoid stale placeholders in the test suite.

Suggested change
# https://github.com/Azure/azure-sdk-for-python/issues/XXXXX
# https://github.com/Azure/azure-sdk-for-python/issues/45365

Copilot uses AI. Check for mistakes.

### Bugs Fixed

- Fixed `PipelineClient.format_url` to preserve trailing slash in the base URL when the URL template is a query-string only (e.g. `?key=value`). #40426
Copy link

Copilot AI Feb 25, 2026

Choose a reason for hiding this comment

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

This changelog entry appears to reference a different issue number than the PR description (which mentions #45365). Please update the trailing reference to the correct issue/PR. Also consider tweaking the wording to be grammatically consistent with the PR title (e.g., 'query-string-only').

Suggested change
- Fixed `PipelineClient.format_url` to preserve trailing slash in the base URL when the URL template is a query-string only (e.g. `?key=value`). #40426
- Fixed `PipelineClient.format_url` to preserve trailing slash in the base URL when the URL template is query-string-only (e.g., `?key=value`). #45365

Copilot uses AI. Check for mistakes.
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.

[azure-core] format url edge case

3 participants