fix: generate valid code when a path parameter with a default precedes one without by saadkhantareen · Pull Request #1495 · openapi-generators/openapi-python-client · GitHub
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:
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.
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.
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
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.
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:
Python rejects this with SyntaxError: Parameter without a default cannot follow a parameter with a default, so generation failed entirely.
Changes
Tests
Fixes #1281