diff --git a/zerver/lib/bugdown/api_code_examples.py b/zerver/lib/bugdown/api_code_examples.py index 48171304c6..6d35ffce17 100644 --- a/zerver/lib/bugdown/api_code_examples.py +++ b/zerver/lib/bugdown/api_code_examples.py @@ -153,7 +153,15 @@ def generate_curl_example(endpoint: str, method: str, openapi_entry = openapi_spec.spec()['paths'][endpoint][method.lower()] openapi_params = openapi_entry.get("parameters", []) - curl_first_line_parts = ["curl"] + curl_method_arguments(endpoint, method, + format_dict = {} + for param in openapi_params: + if param["in"] != "path": + continue + example_value = get_openapi_param_example_value_as_string(endpoint, method, param) + format_dict[param["name"]] = example_value + example_endpoint = endpoint.format_map(format_dict) + + curl_first_line_parts = ["curl"] + curl_method_arguments(example_endpoint, method, api_url) lines.append(" ".join(curl_first_line_parts)) diff --git a/zerver/tests/test_openapi.py b/zerver/tests/test_openapi.py index 9823fdc2b3..092b0f224f 100644 --- a/zerver/tests/test_openapi.py +++ b/zerver/tests/test_openapi.py @@ -675,6 +675,16 @@ class TestCurlExampleGeneration(ZulipTestCase): { "name": "param1", "in": "path", + "description": "Param in path", + "schema": { + "type": "integer" + }, + "example": 35, + "required": True + }, + { + "name": "param2", + "in": "query", "description": "An object", "schema": { "type": "object" @@ -811,17 +821,6 @@ class TestCurlExampleGeneration(ZulipTestCase): ] self.assertEqual(generated_curl_example, expected_curl_example) - @patch("zerver.lib.openapi.OpenAPISpec.spec") - def test_generate_and_render_curl_with_object_param_in_path(self, spec_mock: MagicMock) -> None: - spec_mock.return_value = self.spec_mock_using_param_in_path - generated_curl_example = self.curl_example("/endpoint/{param1}", "GET") - expected_curl_example = [ - '```curl', - 'curl -sSX GET -G http://localhost:9991/api/v1/endpoint/{param1}', - '```' - ] - self.assertEqual(generated_curl_example, expected_curl_example) - @patch("zerver.lib.openapi.OpenAPISpec.spec") def test_generate_and_render_curl_with_object_without_example(self, spec_mock: MagicMock) -> None: spec_mock.return_value = self.spec_mock_using_object_without_example @@ -834,6 +833,18 @@ class TestCurlExampleGeneration(ZulipTestCase): with self.assertRaises(ValueError): self.curl_example("/endpoint", "GET") + @patch("zerver.lib.openapi.OpenAPISpec.spec") + def test_generate_and_render_curl_with_param_in_path(self, spec_mock: MagicMock) -> None: + spec_mock.return_value = self.spec_mock_using_param_in_path + generated_curl_example = self.curl_example("/endpoint/{param1}", "GET") + expected_curl_example = [ + '```curl', + 'curl -sSX GET -G http://localhost:9991/api/v1/endpoint/35 \\', + ' --data-urlencode param2=\'{"key": "value"}\'', + '```' + ] + self.assertEqual(generated_curl_example, expected_curl_example) + def test_generate_and_render_curl_wrapper(self) -> None: generated_curl_example = render_curl_example("/get_stream_id:GET:email:key", api_url="https://zulip.example.com/api")