[python] fix deserialize on basic str fails (#18800)

* [python] fix #18774 Deserialize on basic str fails

* [python] update sample

* [python] update test

* [python] remove type

* [python] fix test

* [python] add top level type test

* Update deserialize content_type parameter and quote

* [python] restore echo_api test

* [python] add allow empty json in Response
This commit is contained in:
ふぁ
2024-06-06 17:15:50 +09:00
committed by GitHub
parent d1254ccfda
commit 6ae8a8f4c7
10 changed files with 197 additions and 64 deletions

View File

@@ -315,10 +315,7 @@ class ApiClient:
match = re.search(r"charset=([a-zA-Z\-\d]+)[\s;]?", content_type)
encoding = match.group(1) if match else "utf-8"
response_text = response_data.data.decode(encoding)
if response_type in ["bytearray", "str"]:
return_data = self.__deserialize_primitive(response_text, response_type)
else:
return_data = self.deserialize(response_text, response_type)
return_data = self.deserialize(response_text, response_type, content_type)
finally:
if not 200 <= response_data.status <= 299:
raise ApiException.from_response(
@@ -386,21 +383,35 @@ class ApiClient:
for key, val in obj_dict.items()
}
def deserialize(self, response_text, response_type):
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
"""Deserializes response into an object.
:param response: RESTResponse object to be deserialized.
:param response_type: class literal for
deserialized object, or string of class name.
:param content_type: content type of response.
:return: deserialized object.
"""
# fetch data from response object
try:
data = json.loads(response_text)
except ValueError:
if content_type is None:
try:
data = json.loads(response_text)
except ValueError:
data = response_text
elif content_type.startswith("application/json"):
if response_text == "":
data = ""
else:
data = json.loads(response_text)
elif content_type.startswith("text/plain"):
data = response_text
else:
raise ApiException(
status=0,
reason="Unsupported content type: {0}".format(content_type)
)
return self.__deserialize(data, response_type)