Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic encoding incorrect for some cases #1016

Open
yav opened this issue Apr 2, 2023 · 1 comment
Open

Generic encoding incorrect for some cases #1016

yav opened this issue Apr 2, 2023 · 1 comment

Comments

@yav
Copy link

yav commented Apr 2, 2023

Consider a type like this:

data T = A { tag :: Int } | B

when using the default options for generic encoding and try to encode A { tag = 2 } the result is {"tag":2}, because the field tag overwrote the actual constructor tag A

@phadej
Copy link
Collaborator

phadej commented Jun 20, 2023

The default sum encoding is TaggedObject with tagFieldName and its docs say

So make sure that your record doesn't have a field with the same label as the tagFieldName. Otherwise the tag gets overwritten by the encoded value of that field!

So either we can somehow try to point out that in the docs more clearly (I have no idea how though), or change the default sum encoding to e.g. ObjectWithSingleField, which doesn't have such limitations. That however would be a very big breaking change (silently changing stuff for people, i.e. only proper encoding tests would find the change!), so I'm hesitant.

I'd advice you

  • to write instances out, like toEncoding = toEncoding myOptions (which you should do anyway, so toEncoding is derived, and not using toJSON), where myOptions = defaultOptions { ... }
  • write encoding (or better roundtrip) tests
  • Then you can change the "tag", or the sum encoding for a type with tag field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants