haskell / haskell/aeson

generic combinators backward compatibility issue

Open
#961 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
1.3k
Forks
336
Avg merge
3d 7h
Merged PRs (30d)
4

Description

IMO is not ideal and makes me question my use of generic combinators in Aeson.
At minimum, this should be documented as a gotcha.

Tested with aeson 1.5.6.0

Start like this

```Haskell
data FooBar =
Foo
| Bar
deriving Generic
```

Things work as expected

```Haskell
import qualified Data.Aeson as A
>>> A.genericToJSON A.defaultOptions Foo
String "Foo"
```

New functionality is requested, new constructor is added, and `FooBar` becomes

```Haskell
data FooBar =
Foo
| Bar
| Baz Int
deriving Generic
```

```Haskell
>>> A.genericToJSON A.defaultOptions Foo
Object (fromList [("tag",String "Foo")])
```

That IMO is concerning, that breaks compatibility. Adding a new constructor has changed how my old constructors are ToJSON and FromJSON-ed.

There is more (and is possibly related). One would expect `allNullaryToStringTag` to force the previous behavior (ideally I would not want to do that but let me try)

```Haskell
A.genericToJSON (A.defaultOptions {A.allNullaryToStringTag=True}) Foo
Object (fromList [("tag",String "Foo")])
```

Nope, what works is:

```Haskell
A.genericToJSON (A.defaultOptions {A.sumEncoding = A.UntaggedValue}) Foo
String "Foo"
```
Except this will drop tag in the `Baz` constructor too.
Maybe this has been fixed in newer versions? I could not find a ticket related to this.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.