aeson icon indicating copy to clipboard operation
aeson copied to clipboard

omitNothingFields not used in Generic Decode

Open arianvp opened this issue 8 years ago • 4 comments

omitNothingFields option is used for generic encoding, but it is not used for generic decoding. Is this a bug?

arianvp avatar Jul 25 '17 15:07 arianvp

Yes that sounds like a bug. Are you able to provide a reproduction?

bergmark avatar Jul 26 '17 13:07 bergmark

> import Data.Aeson
> data F = F { a :: Maybe Int, b :: Maybe Int } deriving (Show, GHC.Generics.Generic)
> instance FromJSON F where parseJSON = genericParseJSON defaultOptions
> -- omitNothingFields = False by default
> decode "{}" :: Maybe F
Just (F {a = Nothing, b = Nothing})

I guess we want that to fail?

Lysxia avatar Jul 30 '17 08:07 Lysxia

Whoops big typo in my deleted comment! I'm not sure if we should consider this a bug as

> eitherDecode "{\"a\":null,\"b\":null}" :: Either String F
Right (F {a = Nothing, b = Nothing})

Does it hurt that parsing is more lenient?

bergmark avatar Aug 02 '17 18:08 bergmark

Coming back to this, I think it's a good thing that parsing is more lenient. I'd be open to add this as a part of Options so you can opt-in if you want it.

bergmark avatar Jun 14 '20 04:06 bergmark

With https://github.com/haskell/aeson/pull/1039 this will fail.

I will first add a test to master to see how things work now, and then decide what to do in #1039.

phadej avatar Jun 12 '23 23:06 phadej

#1039 will add allowOmittedFields option (True by default) to control whether omitted fields are ok when parsing.

phadej avatar Jun 14 '23 10:06 phadej