Skip to content

fix: generate valid code when a path parameter with a default precedes one without - #1495

Open
saadkhantareen wants to merge 1 commit into
openapi-generators:mainfrom
saadkhantareen:fix/path-parameter-default-order
Open

saadkhantareen wants to merge 1 commit into
openapi-generators:mainfrom
saadkhantareen:fix/path-parameter-default-order

Conversation

@saadkhantareen

Copy link
Copy Markdown

Summary

Generating a client for an endpoint whose path declares a parameter with a default before a parameter without one (for example /test/{has_default}/{no_default}, where has_default has a schema default) produced invalid Python:

def _get_kwargs(
    has_default: str = "default_value",
    no_default: int,
) -> dict[str, Any]:

Python rejects this with SyntaxError: Parameter without a default cannot follow a parameter with a default, so generation failed entirely.

Changes

  • Endpoint.sort_parameters now stable-sorts path parameters after ordering them by their position in the path, moving parameters that declare a default after those that do not. Path order is preserved within each group, so endpoints that already generated valid code are unchanged, and all internal calls pass path parameters by keyword.
  • Added a unit test covering the reordering, including preservation of path order within the with-default and without-default groups.
  • Added functional tests that generate a client from the failing shape of spec, verify the default is used when the parameter is omitted, and verify an explicit value overrides it.

Tests

  • pytest tests/test_parser/test_openapi.py end_to_end_tests/functional_tests/generated_code_execution/test_path_parameters.py — 33 passed.
  • pytest tests --basetemp=tests/tmp — 322 passed, 4 skipped.
  • pytest end_to_end_tests/test_end_to_end.py end_to_end_tests/functional_tests --basetemp=tests/tmp — 136 passed; the 9 snapshot failures in test_end_to_end.py occur identically on unmodified main and are unaffected by this change.
  • Manual verification: generating a client from the spec in Generates SyntaxError if a path parameter without a default value follows another that has a default value #1281 failed before this change and succeeds after it, producing no_default: int, has_default: str = "default_value".
  • ruff check and ruff format --check pass on the changed files; mypy openapi_python_client/parser/openapi.py reports no issues.

Fixes #1281

Copilot AI balanced review requested due to automatic review settings October 17, 2026 10:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

Generates SyntaxError if a path parameter without a default value follows another that has a default value

2 participants