Getting "Error in $: expected record (:*:), encountered Array" back in 400 response

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting "Error in $: expected record (:*:), encountered Array" back in 400 response

James Wilson
I posted this question on SO:

http://stackoverflow.com/questions/39343471/haskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody

I wonder if perhaps anybody has any insight into why this might be the case? I feel like I am not doing anything complicated, so I'm hoping it's something obvious that I'm missing, but I can't see what!

The code can be seen in its entirety here (Routes.hs for the route definition, Types.hs for the Id type):

https://github.com/jsdw/talklicker/blob/master/server/app/


--
Reply | Threaded
Open this post in threaded view
|

Re: Getting "Error in $: expected record (:*:), encountered Array" back in 400 response

Alp Mestanogullari

I think people on SO are pointing you in the right direction. it would seem that the Generic instances for aeson derived for newtypes expect that the newtype is written "record-style":

newtype Id = Id { idString :: String }

One quick way to find out is to change your Id definition to that and see if that changes anything =)


Le 6 sept. 2016 18:09, "James Wilson" <[hidden email]> a écrit :
I posted this question on SO:

http://stackoverflow.com/questions/39343471/haskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody

I wonder if perhaps anybody has any insight into why this might be the case? I feel like I am not doing anything complicated, so I'm hoping it's something obvious that I'm missing, but I can't see what!

The code can be seen in its entirety here (Routes.hs for the route definition, Types.hs for the Id type):

--

--
Reply | Threaded
Open this post in threaded view
|

Re: Getting "Error in $: expected record (:*:), encountered Array" back in 400 response

James Wilson
I feel like the issue is somewhere in that area, but I wrote a custom JSON parser for my newtype like so:

instance ToJSON Id where
    toJSON
(Id s) = String $ Text.pack s
instance
FromJSON Id where
    parseJSON
(String s) = return $ Id $ Text.unpack s
    parseJSON _
= empty

And it has not helped. In addition, when my app reads the JSON (string IDs) from a file on start it has no issues, whereas if I remove the explicit parsers and use a record style newtype, it complains.

I think the issue is a little higher than this - the error message implies that servant wants a record type instead of an array at the top level. Also, whether I pass ["hello"] or [1,2,3] or [{"hello":2}], I get the exact same error.

On Tuesday, 6 September 2016 17:28:47 UTC+1, Alp Mestanogullari wrote:

I think people on SO are pointing you in the right direction. it would seem that the Generic instances for aeson derived for newtypes expect that the newtype is written "record-style":

newtype Id = Id { idString :: String }

One quick way to find out is to change your Id definition to that and see if that changes anything =)


Le 6 sept. 2016 18:09, "James Wilson" <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="D7zzA75BAgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">jam...@...> a écrit :
I posted this question on SO:

<a href="http://stackoverflow.com/questions/39343471/haskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F39343471%2Fhaskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHXfe64G2h3pbTm8VR6gpSR-KLcfA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F39343471%2Fhaskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHXfe64G2h3pbTm8VR6gpSR-KLcfA&#39;;return true;">http://stackoverflow.com/questions/39343471/haskell-servant-400-bad-request-when-using-array-of-custom-data-type-in-reqbody

I wonder if perhaps anybody has any insight into why this might be the case? I feel like I am not doing anything complicated, so I'm hoping it's something obvious that I'm missing, but I can't see what!

The code can be seen in its entirety here (Routes.hs for the route definition, Types.hs for the Id type):

https://<a href="http://github.com/jsdw/talklicker/blob/master/server/app/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgithub.com%2Fjsdw%2Ftalklicker%2Fblob%2Fmaster%2Fserver%2Fapp%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBVshtHwbv482gy5ccVtZu-n9Zpw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fgithub.com%2Fjsdw%2Ftalklicker%2Fblob%2Fmaster%2Fserver%2Fapp%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGBVshtHwbv482gy5ccVtZu-n9Zpw&#39;;return true;">github.com/jsdw/talklicker/blob/master/server/app/


--

--