-
Notifications
You must be signed in to change notification settings - Fork 59
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
Can't derive ToSchema for types with polymorphic fields #144
Comments
For some reason it does not work with data Test a = Test { test :: a } deriving (Generic)
instance ToSchema a => ToSchema (Test a) >>> encode $ toSchema (Proxy @(Test Int))
"{\"required\":[\"test\"],\"properties\":{\"test\":{\"maximum\":9223372036854775807,\"minimum\":-9223372036854775808,\"type\":\"integer\"}},\"type\":\"object\"}" Note that the field is always required (even when you instantiate >>> encode $ toSchema (Proxy @(Test (Maybe Int)))
"{\"required\":[\"test\"],\"properties\":{\"test\":{\"maximum\":9223372036854775807,\"minimum\":-9223372036854775808,\"type\":\"integer\"}},\"type\":\"object\"}" |
Very interesting. I wonder why it works at all. The error I posted does seem reasonable. Thanks for the workaround! |
Some more info on this: For the particular case of a polymorphic type with only one field, this will always be required. This instance handles optional fields: (Selector s, ToSchema c) => GToSchema (S1 s (K1 i (Maybe c))) But this instance is found first and it bypasses the check: (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) So yeah. Maybe this needs to be changed or maybe it makes sense semantically. I didn't give it too much thought. However, if we add an extra field, then we can still derive the instance and have the polymorphic field optional if it gets instantiated with a data Test a = Test { test :: a, test2 :: Int } deriving (Generic)
instance
( GToSchema (S1
('MetaSel
('Just "test")
'GHC.Generics.NoSourceUnpackedness
'GHC.Generics.NoSourceStrictness
'GHC.Generics.DecidedLazy)
(Rec0 a)) )
=> ToSchema (Test a) What I don't really get is why writing |
Results in this error
The text was updated successfully, but these errors were encountered: