hashie/hashie

Hash with Files is manipulated when using deep_locate

magynhard opened this issue · 1 comments

When I'm using deep_locate on a Hash that includes a file, it is manipulated.

I figured out this problem, when checking if a file is included in a hash, before sending it via POST to a REST service. The file isn't sent anymore, when calling deep_locate before.

If you comment the line with deep_locate, everything is fine.

def hash_include_file?(hash)
  lambda_condition = lambda { |key, value, object| value.is_a? File }
  Hashie::Extensions::DeepLocate.deep_locate(lambda_condition, hash) != []
end

file = File.new('example_file.txt','rb')
params = { file: file }

RestClient.post "www.example.com", params, {content_type: 'multipart/form-data'}

hash_include_file? params # this line makes the difference!

RestClient.post "www.example.com", params, {content_type: 'multipart/form-data'}

The requests differ in this way (see the Content-Length):

RestClient.post "http://www.example.com", 14904 byte(s) length, 
"Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", 
"Content-Length"=>"14904", 
"Content-Type"=>"multipart/form-data; boundary=----RubyFormBoundary9IgAHjSw4d2O3mJg"
, 
"User-Agent"=>"rest-client/2.0.2 (mingw32 x86_64) ruby/2.4.4p296"
# => 200 OK |  0 bytes
RestClient.post "http://www.example.com", "------RubyFormBoundaryK6UWn0DJxwKAgVsG\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example_file.txt\"\r\nContent-Type: text/plain\r\n\r\n\r\n------RubyFormBoundaryK6UWn
0DJxwKAgVsG--\r\n", 
"Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", 
"Content-Length"=>"186", 
"Content-Type"=>"multipart/form-data; boundary=----RubyFormBoundaryK6UWn0DJxwKAgVsG", 
"User-Agent"=>"rest-client/2.0.2 (mingw32 x8
6_64) ruby/2.4.4p296"
# => 200 OK |  0 bytes

I created a example project, including a rspec file, to show the problem.
https://github.com/entwanderer/rest-client-hashie

Since after almost a year still no reaction has taken place, I have cleaned up the example project. The above example should be sufficient if the bug is fixed at some point.