brendanhay / gogol

A comprehensive Google Services SDK for Haskell.
Other
280 stars 105 forks source link

Fix ToJSON & FromJSON generators for `Maybe [a]` fields #185

Closed korayal closed 1 year ago

korayal commented 1 year ago

When a field has type Maybe [a] where a is not a Text, the ToJSON or FromJSON instances fail trying to parse the result.

destinationUriFileCounts is a good example.

When trying to read the result of a BigQuery extraction job, the request fails trying to parse the response with below error:

Error in $.statistics.extract.destinationUriFileCounts[0]: parsing Int64 failed, expected Number, but encountered String

With this update, below diff is applied to this snippet

--- a/tmp/left.hs
+++ b/tmp/right.hs
@@ -23,7 +23,9 @@ instance Core.FromJSON JobStatistics4 where
       "JobStatistics4"
       ( \o ->
           JobStatistics4
-            Core.<$> (o Core..:? "destinationUriFileCounts")
+            Core.<$> ( o Core..:? "destinationUriFileCounts"
+                         Core.<&> Core.fmap (Core.fmap Core.fromAsText)
+                     )
             Core.<*> ( o Core..:? "inputBytes"
                          Core.<&> Core.fmap Core.fromAsText
                      )
@@ -34,6 +36,7 @@ instance Core.ToJSON JobStatistics4 where
     Core.object
       ( Core.catMaybes
           [ ("destinationUriFileCounts" Core..=)
+              Core.. Core.fmap Core.AsText
               Core.<$> destinationUriFileCounts,
             ("inputBytes" Core..=) Core.. Core.AsText
               Core.<$> inputBytes