mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-03 08:46:55 +00:00
[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:
@@ -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)
|
||||
|
||||
|
||||
@@ -174,17 +174,17 @@ class TestManual(unittest.TestCase):
|
||||
n = openapi_client.Pet.from_dict({"name": "testing", "photoUrls": ["http://1", "http://2"]})
|
||||
api_instance = openapi_client.BodyApi()
|
||||
api_response = api_instance.test_echo_body_pet_response_string(n)
|
||||
self.assertEqual(api_response, '{"name": "testing", "photoUrls": ["http://1", "http://2"]}')
|
||||
self.assertEqual(api_response, "{'name': 'testing', 'photoUrls': ['http://1', 'http://2']}")
|
||||
|
||||
t = openapi_client.Tag()
|
||||
api_response = api_instance.test_echo_body_tag_response_string(t)
|
||||
self.assertEqual(api_response, "{}") # assertion to ensure {} is sent in the body
|
||||
|
||||
api_response = api_instance.test_echo_body_tag_response_string(None)
|
||||
self.assertEqual(api_response, "") # assertion to ensure emtpy string is sent in the body
|
||||
|
||||
api_response = api_instance.test_echo_body_free_form_object_response_string({})
|
||||
self.assertEqual(api_response, "{}") # assertion to ensure {} is sent in the body
|
||||
|
||||
api_response = api_instance.test_echo_body_tag_response_string(None)
|
||||
self.assertEqual(api_response, "") # assertion to ensure emtpy string is sent in the body
|
||||
|
||||
def testAuthHttpBasic(self):
|
||||
api_instance = openapi_client.AuthApi()
|
||||
|
||||
Reference in New Issue
Block a user