Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix parameters_to_url_query returns booleans with upper letter #16947

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,10 @@ class ApiClient:
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python boolean is a subclass of int so isinstance(True, (int, float)) == True. We need to check if the type is boolean before checking if the type is a number to get the behaviour we desire.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In python boolean is a subclass of int so isinstance(True, (int, float)) == True. We need to check if the type is boolean before checking if the type is a number to get the behaviour we desire.

TIL it's still the case in Python 3, I thought this was only the case in 2.x 👍

v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,10 @@ class ApiClient:
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params:
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params:
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ def testAuthHttpBasic(self):
self.assertTrue("Authorization" in e.headers)
self.assertEqual(e.headers["Authorization"], "Basic dGVzdF91c2VybmFtZTp0ZXN0X3Bhc3N3b3Jk")

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 echoServerResponseParaserTest(self):
s = """POST /echo/body/Pet/response_string HTTP/1.1
Host: localhost:3000
Expand Down
4 changes: 2 additions & 2 deletions samples/client/echo_api/python/openapi_client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params:
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
5 changes: 5 additions & 0 deletions samples/client/echo_api/python/test/test_manual.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ def test_from_to_methods(self):
self.assertEqual(pet2.tags[0].name, "None")
self.assertEqual(pet2.category.id, 1)

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 echoServerResponseParaserTest(self):
s = """POST /echo/body/Pet/response_string HTTP/1.1
Host: localhost:3000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params:
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,6 @@ def test_parameters_to_url_query(self):
result = self.api_client.parameters_to_url_query([('value', dictionary)], {})
self.assertEqual(result, 'value=%7B%22strValues%22%3A%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%2C%20%22dictValues%22%3A%20%5B%7B%22name%22%3A%20%22value1%22%2C%20%22age%22%3A%2014%7D%2C%20%7B%22name%22%3A%20%22value2%22%2C%20%22age%22%3A%2012%7D%5D%7D')



def test_parameters_to_url_query_boolean_value(self):
result = self.api_client.parameters_to_url_query([('boolean', True)], {})
self.assertEqual(result, "boolean=true")
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ def parameters_to_url_query(self, params, collection_formats):
if collection_formats is None:
collection_formats = {}
for k, v in params.items() if isinstance(params, dict) else params:
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, bool):
v = str(v).lower()
if isinstance(v, (int, float)):
v = str(v)
if isinstance(v, dict):
v = json.dumps(v)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,6 @@ def test_parameters_to_url_query(self):
result = self.api_client.parameters_to_url_query([('value', dictionary)], {})
self.assertEqual(result, 'value=%7B%22strValues%22%3A%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%2C%20%22dictValues%22%3A%20%5B%7B%22name%22%3A%20%22value1%22%2C%20%22age%22%3A%2014%7D%2C%20%7B%22name%22%3A%20%22value2%22%2C%20%22age%22%3A%2012%7D%5D%7D')



def test_parameters_to_url_query_boolean_value(self):
result = self.api_client.parameters_to_url_query([('boolean', True)], {})
self.assertEqual(result, "boolean=true")
Loading