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

Mismatch between ToJSON and ToSchema with nullary single-record constructor #81

Open
jkarni opened this issue Mar 27, 2023 · 0 comments

Comments

@jkarni
Copy link

jkarni commented Mar 27, 2023

ghci> data Foo = Foo deriving stock (Generic)
ghci> genericToJSON defaultOptions Foo
Array []
 ghci> encodePretty $ runDeclare (genericDeclareSchema (fromAesonOptions (defaultOptions)) (Proxy :: Proxy Foo)) mempty
"[\n    {},\n    {\n        \"enum\": [\n            \"Foo\"\n        ],\n        \"type\": \"string\"\n    }\n]"

Now, you can get Aeson to behave in the way openapi3 does by using tagSingleConstructors 👍

ghci> genericToJSON (defaultOptions { tagSingleConstructors = True}) Foo
String "Foo"

But with tagSingleConstructors you get a mismatch when the record is not nullary:

ghci> data Bar = Bar Int deriving stock (Generic)
ghci> genericToJSON (defaultOptions { tagSingleConstructors = True}) (Bar 5)
Object (fromList [("contents",Number 5.0),("tag",String "Bar")])
ghci> encodePretty $ runDeclare (genericDeclareSchema (fromAesonOptions (defaultOptions { tagSingleConstructors = True})) (Proxy :: Proxy Bar)) mempty
"[\n    {},\n    {\n        \"maximum\": 9223372036854775807,\n        \"minimum\": -9223372036854775808,\n        \"type\": \"integer\"\n    }\n]"

Which is already known, but means that even if you can modify your encoding to navigate openapi/aeson support, there isn't a single Options that works in all cases.

Am happy to help - though if you have any insights on where to start, let me know!

(See also #55)

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

1 participant