From 362140060ab90463185b3b1f729b79c08226bf55 Mon Sep 17 00:00:00 2001 From: theSoenke Date: Wed, 18 Oct 2023 17:10:06 +0200 Subject: [PATCH] fix(python): pass format_options correctly (#436) --- clients/python/test/fixtures/en.json | 3 +++ clients/python/test/test_locales_api.py | 23 +++++++++++++++-- clients/python/test/test_uploads_api.py | 12 ++++++--- .../templates/python/api_client.mustache | 25 +++++++++++++++++-- 4 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 clients/python/test/fixtures/en.json diff --git a/clients/python/test/fixtures/en.json b/clients/python/test/fixtures/en.json new file mode 100644 index 00000000..21da3b26 --- /dev/null +++ b/clients/python/test/fixtures/en.json @@ -0,0 +1,3 @@ +{ + "key": "value" +} diff --git a/clients/python/test/test_locales_api.py b/clients/python/test/test_locales_api.py index fe2ddb7b..0ce1e0f2 100644 --- a/clients/python/test/test_locales_api.py +++ b/clients/python/test/test_locales_api.py @@ -54,12 +54,31 @@ def test_locale_delete(self): """ pass - def test_locale_download(self): + @patch('urllib3.PoolManager.urlopen') + def test_locale_download(self, mock_get): """Test case for locale_download Download a locale # noqa: E501 """ - pass + + body = bytes('{"key":"value"}', 'utf-8') + mock_get.return_value = Mock(ok=True) + mock_get.return_value.data = body + mock_get.return_value.status = 200 + mock_get.return_value.getencoding.return_value = 'utf-8' + mock_get.return_value.getheader.side_effect = { 'Content-Disposition': None }.get + + with phrase_api.ApiClient(self.configuration) as api_client: + api_instance = phrase_api.api.locales_api.LocalesApi(api_client) + api_response = api_instance.locale_download("project_id_example", "en", file_format="simple_json", format_options={"enable_pluralization": True, "custom_metadata_columns": { "E": "text" }}) + + self.assertEqual("https://api.phrase.com/v2/projects/project_id_example/locales/en/download?file_format=simple_json&format_options%5Benable_pluralization%5D=True&format_options%5Bcustom_metadata_columns%5D%5BE%5D=text", mock_get.call_args_list[0].args[1]) + + self.assertIsNotNone(api_response) + file = open(api_response, "r") + content = file.read() + file.close() + self.assertEqual(body.decode(), content) def test_locale_show(self): """Test case for locale_show diff --git a/clients/python/test/test_uploads_api.py b/clients/python/test/test_uploads_api.py index bf09ddac..d26f4280 100644 --- a/clients/python/test/test_uploads_api.py +++ b/clients/python/test/test_uploads_api.py @@ -42,10 +42,14 @@ def test_upload_create(self, mock_post): project_id = "project_id_example" with phrase_api.ApiClient(self.configuration) as api_client: api_instance = phrase_api.UploadsApi(api_client) - api_response = api_instance.upload_create(project_id, file="./README.md", file_format="simple_json") - - self.assertEqual("POST", mock_post.call_args_list[0].args[0]) - self.assertEqual("https://api.phrase.com/v2/projects/project_id_example/uploads", mock_post.call_args_list[0].args[1]) + api_response = api_instance.upload_create( + project_id, + file="./test/fixtures/en.json", + file_format="simple_json", + format_options={"enable_pluralization": True} + ) + + mock_post.assert_called_with("POST", "https://api.phrase.com/v2/projects/project_id_example/uploads", query_params=[], headers={'Accept': 'application/json', 'Content-Type': 'multipart/form-data', 'User-Agent': 'OpenAPI-Generator/1.14.0/python', 'Authorization': 'token YOUR_API_KEY'}, post_params=[('file_format', 'simple_json'), ('format_options', {'enable_pluralization': True}), ('file', ('en.json', b'{\n "key": "value"\n}\n', 'application/json'))], body=None, _preload_content=True, _request_timeout=None) self.assertIsNotNone(api_response) self.assertIsInstance(api_response, phrase_api.models.upload.Upload) diff --git a/openapi-generator/templates/python/api_client.mustache b/openapi-generator/templates/python/api_client.mustache index 47ede519..e07dd1e9 100644 --- a/openapi-generator/templates/python/api_client.mustache +++ b/openapi-generator/templates/python/api_client.mustache @@ -161,8 +161,8 @@ class ApiClient(object): # query parameters if query_params: query_params = self.sanitize_for_serialization(query_params) - query_params = self.parameters_to_tuples(query_params, - collection_formats) + query_params = self.parameters_to_tuples(query_params, collection_formats) + query_params = self.flatten_query_params(query_params) # post parameters if post_params or files: @@ -473,6 +473,27 @@ class ApiClient(object): new_params.append((k, v)) return new_params + def flatten_query_params(self, params): + result_params = {} + for key, value in params: + if isinstance(value, dict): + self.flatten_dict(result_params, value, key) + else: + result_params[key] = value + + return [(key, value) for (key,value) in result_params.items()] + + def flatten_dict(self, params, param, namespace): + for key, value in param.items(): + new_key = "{0}[{1}]".format(namespace, key) + if isinstance(value, dict): + params = self.flatten_dict(params, value, new_key) + else: + params[new_key] = value + + return params + + def files_parameters(self, files=None): """Builds form parameters.