Skip to content

Commit 94c2cc0

Browse files
committed
Fixed bug in handling errors in parsing request IDs
1 parent 66dec39 commit 94c2cc0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/Network/JsonRpc/Types.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,17 @@ instance A.FromJSON Request where
110110
(Request <$>
111111
x .: "method" <*>
112112
(parseParams =<< x .:? "params" .!= emptyObject) <*>
113-
(Just <$> x .: idKey <|> return Nothing)) -- (.:?) parses Null value as Nothing
113+
parseId)
114114
where parseParams (A.Object obj) = return $ Left obj
115115
parseParams (A.Array ar) = return $ Right ar
116116
parseParams _ = empty
117117
checkVersion ver = when (ver /= jsonRpcVersion) (fail $ "Wrong JSON RPC version: " ++ unpack ver)
118+
-- (.:?) parses Null value as Nothing so parseId needs
119+
-- to use both (.:?) and (.:) to handle all cases
120+
parseId = x .:? idKey >>= \optional ->
121+
case optional of
122+
Nothing -> Just <$> (x .: idKey) <|> return Nothing
123+
_ -> return optional
118124
parseJSON _ = empty
119125

120126
data Response = Response Id (Either RpcError A.Value)

0 commit comments

Comments
 (0)