From 406bc28a3d7e671f384b07a213551cd9c749d0d4 Mon Sep 17 00:00:00 2001 From: Roi Shacham <93319592+rshacham@users.noreply.github.com> Date: Sat, 13 Jan 2024 05:36:01 +0200 Subject: [PATCH] Fix parameters_to_url_query doesn't properly convert lists to string (#17592) * fix parameters_to_url_query to properly handle lists * fix query parameters bug in sample clients * add tests for url query list value * build project * tests fix * Revert "build project" This reverts commit a486a6de7528302db92c36e64dcd20d41ada51b9. --- .../main/resources/python-pydantic-v1/api_client.mustache | 2 +- .../src/main/resources/python/api_client.mustache | 2 +- .../openapi_client/api_client.py | 2 +- .../python-pydantic-v1/openapi_client/api_client.py | 2 +- .../client/echo_api/python-pydantic-v1/test/test_manual.py | 6 ++++++ .../client/echo_api/python/openapi_client/api_client.py | 2 +- samples/client/echo_api/python/tests/test_manual.py | 7 ++++++- .../petstore/python-aiohttp/petstore_api/api_client.py | 2 +- .../python-pydantic-v1-aiohttp/petstore_api/api_client.py | 2 +- .../petstore/python-pydantic-v1/petstore_api/api_client.py | 2 +- .../petstore/python-pydantic-v1/tests/test_api_client.py | 4 ++++ .../client/petstore/python/petstore_api/api_client.py | 2 +- .../client/petstore/python/tests/test_api_client.py | 4 ++++ 13 files changed, 29 insertions(+), 10 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache index fe61bd61a5ed..7c7bfcdc7d5b 100644 --- a/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python-pydantic-v1/api_client.mustache @@ -577,7 +577,7 @@ class ApiClient: if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/modules/openapi-generator/src/main/resources/python/api_client.mustache b/modules/openapi-generator/src/main/resources/python/api_client.mustache index 8c9812db7d2b..83b21860d554 100644 --- a/modules/openapi-generator/src/main/resources/python/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/python/api_client.mustache @@ -491,7 +491,7 @@ class ApiClient: if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/api_client.py b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/api_client.py index ce93eaca6dea..262f9aecfa7e 100644 --- a/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/api_client.py +++ b/samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent-true/openapi_client/api_client.py @@ -484,7 +484,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py index 459e3850849e..6383d7e39e7e 100644 --- a/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py +++ b/samples/client/echo_api/python-pydantic-v1/openapi_client/api_client.py @@ -539,7 +539,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/client/echo_api/python-pydantic-v1/test/test_manual.py b/samples/client/echo_api/python-pydantic-v1/test/test_manual.py index fedf4cfad6bf..202cf2dd1963 100644 --- a/samples/client/echo_api/python-pydantic-v1/test/test_manual.py +++ b/samples/client/echo_api/python-pydantic-v1/test/test_manual.py @@ -149,6 +149,12 @@ def test_parameters_to_url_query_boolean_value(self): client = openapi_client.ApiClient() params = client.parameters_to_url_query([("boolean", True),], {}) self.assertEqual(params, "boolean=true") + + def test_parameters_to_url_query_list_value(self): + client = openapi_client.ApiClient() + params = client.parameters_to_url_query(params=[('list', [1, 2, 3])], collection_formats={'list': 'multi'}) + self.assertEqual(params, "list=1&list=2&list=3") + def echoServerResponseParaserTest(self): s = """POST /echo/body/Pet/response_string HTTP/1.1 diff --git a/samples/client/echo_api/python/openapi_client/api_client.py b/samples/client/echo_api/python/openapi_client/api_client.py index ce93eaca6dea..262f9aecfa7e 100644 --- a/samples/client/echo_api/python/openapi_client/api_client.py +++ b/samples/client/echo_api/python/openapi_client/api_client.py @@ -484,7 +484,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/client/echo_api/python/tests/test_manual.py b/samples/client/echo_api/python/tests/test_manual.py index 17e0e1014051..525c08758544 100644 --- a/samples/client/echo_api/python/tests/test_manual.py +++ b/samples/client/echo_api/python/tests/test_manual.py @@ -224,7 +224,12 @@ def test_parameters_to_url_query_boolean_value(self): client = openapi_client.ApiClient() params = client.parameters_to_url_query([("boolean", True),], {}) self.assertEqual(params, "boolean=true") - + + def test_parameters_to_url_query_list_value(self): + client = openapi_client.ApiClient() + params = client.parameters_to_url_query(params=[('list', [1, 2, 3])], collection_formats={'list': 'multi'}) + self.assertEqual(params, "list=1&list=2&list=3") + def echoServerResponseParaserTest(self): s = """POST /echo/body/Pet/response_string HTTP/1.1 Host: localhost:3000 diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py index 5e403f5a51ae..fdb8c16ab6b0 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/api_client.py @@ -486,7 +486,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py index dbef903bdd62..9fefc1b1ae07 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1-aiohttp/petstore_api/api_client.py @@ -509,7 +509,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py index 2a74a7d70049..1b4f238f9ddd 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/petstore_api/api_client.py @@ -538,7 +538,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/openapi3/client/petstore/python-pydantic-v1/tests/test_api_client.py b/samples/openapi3/client/petstore/python-pydantic-v1/tests/test_api_client.py index d254d28010db..9205f4ed998b 100644 --- a/samples/openapi3/client/petstore/python-pydantic-v1/tests/test_api_client.py +++ b/samples/openapi3/client/petstore/python-pydantic-v1/tests/test_api_client.py @@ -260,3 +260,7 @@ def test_parameters_to_url_query(self): def test_parameters_to_url_query_boolean_value(self): result = self.api_client.parameters_to_url_query([('boolean', True)], {}) self.assertEqual(result, "boolean=true") + + def test_parameters_to_url_query_list_value(self): + params = self.api_client.parameters_to_url_query(params=[('list', [1, 2, 3])], collection_formats={'list': 'multi'}) + self.assertEqual(params, "list=1&list=2&list=3") diff --git a/samples/openapi3/client/petstore/python/petstore_api/api_client.py b/samples/openapi3/client/petstore/python/petstore_api/api_client.py index 5aca14639f2a..f9ebfbb54028 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/api_client.py +++ b/samples/openapi3/client/petstore/python/petstore_api/api_client.py @@ -483,7 +483,7 @@ def parameters_to_url_query(self, params, collection_formats): if k in collection_formats: collection_format = collection_formats[k] if collection_format == 'multi': - new_params.extend((k, value) for value in v) + new_params.extend((k, str(value)) for value in v) else: if collection_format == 'ssv': delimiter = ' ' diff --git a/samples/openapi3/client/petstore/python/tests/test_api_client.py b/samples/openapi3/client/petstore/python/tests/test_api_client.py index 972b705872df..8c5a99d3acff 100644 --- a/samples/openapi3/client/petstore/python/tests/test_api_client.py +++ b/samples/openapi3/client/petstore/python/tests/test_api_client.py @@ -245,3 +245,7 @@ def test_parameters_to_url_query_dict_values(self): def test_parameters_to_url_query_boolean_value(self): result = self.api_client.parameters_to_url_query([('boolean', True)], {}) self.assertEqual(result, "boolean=true") + + def test_parameters_to_url_query_list_value(self): + params = self.api_client.parameters_to_url_query(params=[('list', [1, 2, 3])], collection_formats={'list': 'multi'}) + self.assertEqual(params, "list=1&list=2&list=3")