Skip to content

Commit

Permalink
adds support for is_nullable_response flag in response handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sufyankhanrao committed Jul 5, 2024
1 parent 844ff10 commit 1a09c6e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/apimatic-core/response_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize
@is_date_response = false
@is_response_array = false
@is_response_void = false
@is_nullable_response = false
end

# Sets deserializer for the response.
Expand Down Expand Up @@ -138,6 +139,14 @@ def is_response_void(is_response_void)
@is_response_void = is_response_void
self
end

# Sets the is_nullable_response property.
# @param [Boolean] is_nullable_response Flag to return early in case of empty response payload.
# @return [ResponseHandler] An updated instance of ResponseHandler.
def is_nullable_response(is_nullable_response)
@is_nullable_response = is_nullable_response
self
end
# rubocop:enable Naming/PredicateName

# Main method to handle the response with all the set properties.
Expand Down Expand Up @@ -191,7 +200,7 @@ def apply_xml_deserializer(response)
# Applies deserializer to the response.
# @param [Boolean] should_symbolize_hash Flag to symbolize the hash during response deserialization.
def apply_deserializer(response, should_symbolize_hash)
return if response.raw_body.nil? || response.raw_body.to_s.strip.empty?
return if @is_nullable_response && (response.raw_body.nil? || response.raw_body.to_s.strip.empty?)

return apply_xml_deserializer(response) if @is_xml_response
return response.raw_body if @deserializer.nil?
Expand Down
47 changes: 47 additions & 0 deletions test/test-apimatic-core/response_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,55 @@ def test_empty_response_body
response_body.to_i
end)
.handle(response_mock, MockHelper.get_global_errors)
expected_response = 0

refute_nil actual_response
assert_equal expected_response, actual_response

actual_response = @response_handler
.deserializer(ApiHelper.method(:custom_type_deserializer))
.deserialize_into(Validate.method(:from_hash))
.handle(response_mock, MockHelper.get_global_errors)

assert_nil actual_response

response_body_mock = ' '
response_mock = MockHelper.create_response status_code: 200,
raw_body: response_body_mock
actual_response = @response_handler.deserializer(ApiHelper.method(:deserialize_primitive_types))
.is_primitive_response(true)
.deserialize_into(proc do |response_body|
response_body.to_i
end)
.handle(response_mock, MockHelper.get_global_errors)

refute_nil actual_response
assert_equal expected_response, actual_response

actual_response = @response_handler
.deserializer(ApiHelper.method(:custom_type_deserializer))
.deserialize_into(Validate.method(:from_hash))
.handle(response_mock, MockHelper.get_global_errors)

assert_nil actual_response
end

def test_empty_response_body_with_nullable_response_flag
response_body_mock = ''
response_mock = MockHelper.create_response status_code: 200,
raw_body: response_body_mock
actual_response = @response_handler.deserializer(ApiHelper.method(:deserialize_primitive_types))
.is_primitive_response(true)
.is_nullable_response(true)
.deserialize_into(proc do |response_body|
response_body.to_i
end)
.handle(response_mock, MockHelper.get_global_errors)

assert_nil actual_response

actual_response = @response_handler
.is_nullable_response(true)
.deserializer(ApiHelper.method(:custom_type_deserializer))
.deserialize_into(Validate.method(:from_hash))
.handle(response_mock, MockHelper.get_global_errors)
Expand All @@ -345,6 +390,7 @@ def test_empty_response_body
response_mock = MockHelper.create_response status_code: 200,
raw_body: response_body_mock
actual_response = @response_handler.deserializer(ApiHelper.method(:deserialize_primitive_types))
.is_nullable_response(true)
.is_primitive_response(true)
.deserialize_into(proc do |response_body|
response_body.to_i
Expand All @@ -354,6 +400,7 @@ def test_empty_response_body
assert_nil actual_response

actual_response = @response_handler
.is_nullable_response(true)
.deserializer(ApiHelper.method(:custom_type_deserializer))
.deserialize_into(Validate.method(:from_hash))
.handle(response_mock, MockHelper.get_global_errors)
Expand Down

0 comments on commit 1a09c6e

Please sign in to comment.