icyleaf / halite

💎HTTP Requests Client with a chainable REST API, built-in sessions and middlewares.
MIT License
170 stars 13 forks source link

Can POST JSON payloads with a sub hash with a value of type Array(String) #41

Closed fusillicode closed 6 years ago

fusillicode commented 6 years ago

Hi, I think I've encountered a bug or an undocumented support for some JSON payloads.

If I try to perform this POST request Halite.post("https://my-app.com", json: {"asd" => { "asd" => ["asd", "sad"] }}) I end up with the following compiler error:

/lib/halite/src/halite/options.cr:356, line 84:
...
  68.       private def parse_json(raw : (Hash(String, _) | NamedTuple)?) : Hash(String, Options::Type)
  69.         new_json = {} of String => Type
  70.         return new_json unless json = raw
  71. 
  72.         if json.responds_to?(:each)
  73.           json.each do |k, v|
  74.             pp k, v
  75.             new_json[k.to_s] =
  76.               case v
  77.               when Array
  78.                 pp "CICCIO"
  79.                 v.each_with_object([] of Type) do |e, obj|
  80.                   obj << e.as(Type)
  81.                 end
  82.               when Hash
  83.                 v.each_with_object({} of String => Type) do |(ik, iv), obj|
> 84.                   obj[ik.to_s] = iv.as(Type)
  85.                 end
  86.               when NamedTuple
  87.                 hash = {} of String => Type
  88.                 v.each do |nk, nv|
  89.                   hash[nk.to_s] = nv.as(Type)
  90.                 end
  91.                 hash
  92.               else
  93.                 v.as(Type)
  94.               end
  95.           end
  96.         end
  97. 
  98.         new_json
  99.       end
 100.     

can't cast Array(String) to Halite::Options::Type

Btw I'm using the latest version of the shard, i.e. 0.7.2. For any other information I remain obviously at total disposal :)

icyleaf commented 6 years ago

Thanks for feedback, I’ll check on next monday.

fusillicode commented 6 years ago

My pleasure @icyleaf & thank you for the incredibly fast feedback!

As said, for anything, I remain at total disposal :)

Have a nice weekend 🍻

icyleaf commented 6 years ago

@fusillicode May you try master branch:

Replace it, then run shards update and try again

dependencies:
  halite:
    github: icyleaf/halite
    branch: master
icyleaf commented 6 years ago

Released v0.7.3, closed for now. Reopen with any new issues please.

fusillicode commented 6 years ago

Hi @icyleaf I'm really ashamed for the delay of my reply. Unfortunately I didn't had the time to test the master branch 😞 I'll update to the v0.7.3 as soon as I can and report any issue I'll find.

Thank you really a lot for your work & your supporto to the Crystal community 🙇‍♂️

P.S: sorry once again. I would have really liked to submit a PR myself about this issue but as said I'm really out of time in this period 😞