bugdown: Show example value for path params in curl example.

This commit is contained in:
Vishnu Ks
2019-10-03 13:59:28 +00:00
committed by Tim Abbott
parent a03765bbe2
commit cb64fc8732
2 changed files with 31 additions and 12 deletions

View File

@@ -153,7 +153,15 @@ def generate_curl_example(endpoint: str, method: str,
openapi_entry = openapi_spec.spec()['paths'][endpoint][method.lower()] openapi_entry = openapi_spec.spec()['paths'][endpoint][method.lower()]
openapi_params = openapi_entry.get("parameters", []) 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) api_url)
lines.append(" ".join(curl_first_line_parts)) lines.append(" ".join(curl_first_line_parts))

View File

@@ -675,6 +675,16 @@ class TestCurlExampleGeneration(ZulipTestCase):
{ {
"name": "param1", "name": "param1",
"in": "path", "in": "path",
"description": "Param in path",
"schema": {
"type": "integer"
},
"example": 35,
"required": True
},
{
"name": "param2",
"in": "query",
"description": "An object", "description": "An object",
"schema": { "schema": {
"type": "object" "type": "object"
@@ -811,17 +821,6 @@ class TestCurlExampleGeneration(ZulipTestCase):
] ]
self.assertEqual(generated_curl_example, expected_curl_example) 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") @patch("zerver.lib.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_object_without_example(self, spec_mock: MagicMock) -> None: 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 spec_mock.return_value = self.spec_mock_using_object_without_example
@@ -834,6 +833,18 @@ class TestCurlExampleGeneration(ZulipTestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.curl_example("/endpoint", "GET") 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: def test_generate_and_render_curl_wrapper(self) -> None:
generated_curl_example = render_curl_example("/get_stream_id:GET:email:key", generated_curl_example = render_curl_example("/get_stream_id:GET:email:key",
api_url="https://zulip.example.com/api") api_url="https://zulip.example.com/api")