Skip to content

Commit

Permalink
deserialize method should attempt to deserialize body even if there i…
Browse files Browse the repository at this point in the history
…s no schema (#303)
  • Loading branch information
eddiechayes authored Oct 18, 2023
1 parent 25a1b94 commit 8df96df
Showing 1 changed file with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1043,60 +1043,57 @@ class OpenApiResponse(JSONDetector):
status=response.http_response.status
)

def deserialize_body(self, response: ResponseWrapper, content_type: str) -> (any, str):
if self._content_type_is_json(content_type):
deserialized_body = self.__deserialize_json(response.http_response.data)
elif content_type == 'application/octet-stream':
deserialized_body = self.__deserialize_application_octet_stream(response.http_response)
elif content_type.startswith('multipart/form-data'):
deserialized_body = self.__deserialize_multipart_form_data(response.http_response.data)
content_type = 'multipart/form-data'
else: # If we don't know how to deserialize, use raw body string
deserialized_body = response.http_response.data.decode()
return deserialized_body, content_type

def deserialize(self, response: ResponseWrapper, configuration: Configuration, skip_deserialization = False) -> ApiResponse:
content_type = response.http_response.headers.get('content-type')
deserialized_body = unset
streamed = response.http_response.supports_chunked_reads()

deserialized_headers = unset
if self.headers is not None:
# TODO add header deserialiation here
# TODO add header deserialization here
pass

if self.content is not None:
if len(self.content) == 0:
# some specs do not define response content media type schemas
return self.response_cls(
round_trip_time=response.round_trip_time,
response=response.http_response,
body=unset,
headers=response.http_response.headers,
status=response.http_response.status
)
if self.content is not None and len(self.content) == 0:
# some specs do not define response content media type schemas
return self.response_cls(
round_trip_time=response.round_trip_time,
response=response.http_response,
body=unset,
headers=response.http_response.headers,
status=response.http_response.status
)

try:
deserialized_body, content_type = self.deserialize_body(response, content_type)
except Exception:
# Most likely content-type did not match actual body
deserialized_body = unset

if not skip_deserialization:
body_schema = self.__get_schema_for_content_type(content_type)
if body_schema is None:
raise ApiValueError(
f"Invalid content_type returned. Content_type='{content_type}' was returned "
f"when only {str(set(self.content))} are defined for status_code={str(response.http_response.status)}"
)

if self._content_type_is_json(content_type):
body_data = self.__deserialize_json(response.http_response.data)
elif content_type == 'application/octet-stream':
body_data = self.__deserialize_application_octet_stream(response.http_response)
elif content_type.startswith('multipart/form-data'):
body_data = self.__deserialize_multipart_form_data(response.http_response.data)
content_type = 'multipart/form-data'
else:
raise NotImplementedError('Deserialization of {} has not yet been implemented'.format(content_type))
if skip_deserialization:
return self.response_cls(
round_trip_time=response.round_trip_time,
response=response.http_response,
body=body_data,
headers=response.http_response.headers,
status=response.http_response.status
)

# Execute validation and throw as a side effect if validation fails
body_schema.from_openapi_data_oapg(
body_data,
_configuration=configuration
)
# Validation passed, set deserialized_body to plain old deserialized data
deserialized_body = body_data
elif streamed:

if streamed:
response.http_response.release_conn()

return self.response_cls(
Expand Down

0 comments on commit 8df96df

Please sign in to comment.