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

[python] fix content_type deserialize #19317

Merged
merged 5 commits into from
Aug 24, 2024
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 @@ -412,12 +412,12 @@ class ApiClient:
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
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 @@ -405,12 +405,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ def deserialize(self, response_text: str, response_type: str, content_type: Opti
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
elif re.match(r'^application/(json|[\w!#$&.+-^_]+\+json)\s*(;|$)', content_type, re.IGNORECASE):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
elif re.match(r'^text/plain\s*(;|$)', content_type, re.IGNORECASE):
data = response_text
else:
raise ApiException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,35 @@ def test_deserialize_animal(self):
self.assertEqual(deserialized.class_name, "Cat")
self.assertEqual(deserialized.declawed, True)
self.assertEqual(deserialized.to_json(), '{"className": "Cat", "color": "red", "declawed": true}')

def test_deserialize_content_type(self):

response = json.dumps({"a": "a"})

deserialized = self.deserialize(response, "Dict[str, str]", 'application/json')
self.assertTrue(isinstance(deserialized, dict))


deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json')
self.assertTrue(isinstance(deserialized, dict))


deserialized = self.deserialize(response, "Dict[str, str]", 'application/json; charset=utf-8')
self.assertTrue(isinstance(deserialized, dict))

deserialized = self.deserialize(response, "Dict[str, str]", 'application/vnd.api+json; charset=utf-8')
self.assertTrue(isinstance(deserialized, dict))

deserialized = self.deserialize(response, "str", 'text/plain')
self.assertTrue(isinstance(deserialized, str))

deserialized = self.deserialize(response, "Dict[str, str]", 'APPLICATION/JSON')
self.assertTrue(isinstance(deserialized, dict))

with self.assertRaises(petstore_api.ApiException) as cm:
deserialized = self.deserialize(response, "str", 'text/html')

with self.assertRaises(petstore_api.ApiException) as cm:
deserialized = self.deserialize(response, "Dict[str, str]", 'application/jsonnnnn')


Loading