@@ -149,3 +149,79 @@ def test_path_params_with_hash_are_encoded(self, sync_detailed, Client):
149149 # Hash should be encoded as %23
150150 expected_url = "/items/item%231/details/detail%23id"
151151 assert call_kwargs ["url" ] == expected_url
152+
153+
154+ @with_generated_client_fixture (
155+ """
156+ paths:
157+ "/test/{has_default}/{no_default}":
158+ get:
159+ operationId: getTest
160+ parameters:
161+ - name: has_default
162+ in: path
163+ required: true
164+ schema:
165+ type: string
166+ default: default_value
167+ - name: no_default
168+ in: path
169+ required: true
170+ schema:
171+ type: integer
172+ responses:
173+ "200":
174+ description: Success
175+ content:
176+ application/json:
177+ schema:
178+ type: object
179+ properties:
180+ result:
181+ type: string
182+ """
183+ )
184+ @with_generated_code_import (".api.default.get_test.sync_detailed" )
185+ @with_generated_code_import (".client.Client" )
186+ class TestPathParameterWithDefaultBeforeRequired :
187+ """A path parameter with a default may precede one without a default in the path.
188+
189+ The generated function must still be valid Python, and the default must be used
190+ when the parameter is not supplied.
191+ """
192+
193+ def test_default_is_used_when_parameter_is_omitted (self , sync_detailed , Client ):
194+ mock_httpx_client = MagicMock (spec = httpx .Client )
195+ mock_response = MagicMock (spec = httpx .Response )
196+ mock_response .status_code = 200
197+ mock_response .json .return_value = {"result" : "ok" }
198+ mock_response .content = b'{"result": "ok"}'
199+ mock_response .headers = {}
200+ mock_httpx_client .request .return_value = mock_response
201+
202+ client = Client (base_url = "https://api.example.com" )
203+ client .set_httpx_client (mock_httpx_client )
204+
205+ sync_detailed (no_default = 42 , client = client )
206+
207+ mock_httpx_client .request .assert_called_once ()
208+ call_kwargs = mock_httpx_client .request .call_args [1 ]
209+ assert call_kwargs ["url" ] == "/test/default_value/42"
210+
211+ def test_explicit_value_overrides_default (self , sync_detailed , Client ):
212+ mock_httpx_client = MagicMock (spec = httpx .Client )
213+ mock_response = MagicMock (spec = httpx .Response )
214+ mock_response .status_code = 200
215+ mock_response .json .return_value = {"result" : "ok" }
216+ mock_response .content = b'{"result": "ok"}'
217+ mock_response .headers = {}
218+ mock_httpx_client .request .return_value = mock_response
219+
220+ client = Client (base_url = "https://api.example.com" )
221+ client .set_httpx_client (mock_httpx_client )
222+
223+ sync_detailed (no_default = 42 , has_default = "custom" , client = client )
224+
225+ mock_httpx_client .request .assert_called_once ()
226+ call_kwargs = mock_httpx_client .request .call_args [1 ]
227+ assert call_kwargs ["url" ] == "/test/custom/42"
0 commit comments