GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix!: URL-encode path parameters in generated endpoints by Robbie-Palmer · Pull Request #1349 · openapi-generators/openapi-python-client

Conversation

@Robbie-Palmer

@Robbie-Palmer Robbie-Palmer commented Oct 31, 2025

Copy link
Copy Markdown
Contributor

Summary

Path parameters containing reserved characters were being inserted directly into URLs without encoding, causing malformed requests and potential security issues.

Problem

When path parameters contain special characters like:

  • Slashes (/)
  • Question marks (?)
  • Ampersands (&)
  • Hash/fragments (#)
  • Spaces

These were not being URL-encoded, leading to:

  • Malformed URLs that break routing
  • Security vulnerabilities where parameter values could inject path segments
  • Non-compliance with RFC 3986

Solution

  • Import urllib.parse.quote in the endpoint template
  • Wrap all path parameters with quote(str(...), safe="") to ensure proper percent-encoding
  • Add comprehensive tests for various special characters in path parameters

Changes

  • Updated openapi_python_client/templates/endpoint_module.py.jinja to add URL encoding
  • Added end_to_end_tests/functional_tests/generated_code_execution/test_path_parameters.py with tests
  • Regenerated all golden records to reflect the encoding changes

Test plan

  • Added test for normal alphanumeric path parameters (baseline)
  • Added tests for path parameters with reserved characters (/, ?, &, #)
  • Added tests for path parameters with spaces
  • Regenerated golden records with pdm regen
  • Verified generated code properly encodes all special characters

Example

Before:

"url": f"/items/{item_id}/details/{detail_id}"
# With item_id="item/with/slashes" becomes:
# "/items/item/with/slashes/details/..." (broken!)

After:

"url": "/items/{item_id}/details/{detail_id}".format(
    item_id=quote(str(item_id), safe=""),
    detail_id=quote(str(detail_id), safe=""),
)
# With item_id="item/with/slashes" becomes:
# "/items/item%2Fwith%2Fslashes/details/..." (correct!)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@dbanty dbanty added 🐞bug Something isn't working 🥚breaking This change breaks compatibility labels Nov 3, 2025
@dbanty dbanty changed the title fix: Properly URL-encode path parameters in generated endpoints fix!: URL-encode path parameters in generated endpoints Nov 4, 2025
@Robbie-Palmer @dbanty
fix: Properly URL-encode path parameters in generated endpoints
a357949
Path parameters containing reserved characters (/, ?, &, #, spaces, etc.)
were being inserted directly into URLs without encoding, causing malformed
requests and potential security issues.

Changes:
- Import urllib.parse.quote in endpoint template
- Wrap all path parameters with quote(str(...), safe="") to ensure proper encoding
- Add comprehensive tests for various special characters in path parameters

This ensures that path parameters are properly percent-encoded according to
RFC 3986, preventing URL parsing errors and security vulnerabilities.

Fixes cases where path parameters contain:
- Slashes (/) -> %2F
- Question marks (?) -> %3F
- Ampersands (&) -> %26
- Hash/fragments (#) -> %23
- Spaces -> %20
- And other reserved characters
@dbanty
dbanty force-pushed the fix/url-encoding branch from 7847cd0 to a357949 Compare November 4, 2025 01:02
dbanty
dbanty approved these changes Nov 4, 2025
View reviewed changes

@dbanty dbanty left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Seems like the right call, though I will mark as a breaking change since someone was probably relying on the previous unsafe behavior 😓.

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Robbie-Palmer reacted with laugh emoji
All reactions
  • 😄 1 reaction
@dbanty
dbanty enabled auto-merge November 4, 2025 01:04
@dbanty
dbanty added this pull request to the merge queue Nov 4, 2025
Merged via the queue into openapi-generators:main with commit 3464f80 Nov 4, 2025
@knope-bot knope-bot Bot mentioned this pull request Nov 4, 2025
Release 0.28.0 #1353
Closed
@knope-bot knope-bot Bot mentioned this pull request Dec 2, 2025
Release 0.28.0 #1368
Merged
github-merge-queue Bot pushed a commit that referenced this pull request Dec 3, 2025
@knope-bot
Release 0.28.0 (#1368)
49fa8fc
> [!IMPORTANT]
> Merging this pull request will create this release

## Breaking Changes

- URL-encode path parameters in generated endpoints (#1349)

## Fixes

### Fix bad code generation

#1360 by @EricAtORS

This fixes:
- missing parenthesis in to_multipart
 #1338 #1318
- missing imports in the lazy eval in to_multipart:
#931 and #1051

### Fix optional bodies

If a body is not required (the default), it will now:

1. Have `Unset` as part of its type annotation.
2. Default to a value of `UNSET`
3. Not be included in the request if it is `UNSET`

Thanks @orelmaliach for the report! Fixes #1354

Co-authored-by: knope-bot[bot] <152252888+knope-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@dbanty dbanty dbanty approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

🥚breaking This change breaks compatibility 🐞bug Something isn't working

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Robbie-Palmer @dbanty
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.