diff --git a/src/ansys/openapi/common/_api_client.py b/src/ansys/openapi/common/_api_client.py index dccdd373..6d733d12 100644 --- a/src/ansys/openapi/common/_api_client.py +++ b/src/ansys/openapi/common/_api_client.py @@ -319,15 +319,12 @@ def deserialize( if response_type == "file": return self.__deserialize_file(response) - try: - data = response.json() - except ValueError: - content_type = response.headers.get( - "Content-Type", "application/octet-stream" - ) - if content_type not in ["application/octet-stream"]: - data = response.text - else: + if response_type == "str": + data: SerializedType = response.text + else: + try: + data = response.json() + except ValueError: data = response.content return self.__deserialize(data, response_type) diff --git a/tests/test_api_client.py b/tests/test_api_client.py index c4202f46..1c0d3b9f 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -378,9 +378,9 @@ def test_json_parsed_as_json(self, mocker): response = self.create_response(data) deserialize_mock = mocker.patch.object(ApiClient, "_ApiClient__deserialize") deserialize_mock.return_value = True - _ = self._client.deserialize(response, dict) + _ = self._client.deserialize(response, "dict") deserialize_mock.assert_called() - deserialize_mock.assert_called_once_with(data, dict) + deserialize_mock.assert_called_once_with(data, "dict") def test_text_parsed_as_text(self, mocker): data = "This is some data this is definitely not json, it should be rendered as a string" @@ -391,6 +391,15 @@ def test_text_parsed_as_text(self, mocker): deserialize_mock.assert_called() deserialize_mock.assert_called_once_with(data, "str") + def test_deserialize_json_as_string_returns_string(self, mocker): + data = json.dumps({"foo": "bar", "baz": [1, 2, 3]}) + response = self.create_response(text=data, content_type="text/plain") + deserialize_mock = mocker.patch.object(ApiClient, "_ApiClient__deserialize") + deserialize_mock.return_value = True + _ = self._client.deserialize(response, "str") + deserialize_mock.assert_called() + deserialize_mock.assert_called_once_with(data, "str") + @pytest.mark.parametrize("provide_content_type", (True, False)) def test_application_data_is_not_parsed(self, mocker, provide_content_type): # The false case tests the default handling of non-json data