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

Can't derive ToSchema for types with polymorphic fields #144

Open
LukaHorvat opened this issue Mar 8, 2018 · 3 comments
Open

Can't derive ToSchema for types with polymorphic fields #144

LukaHorvat opened this issue Mar 8, 2018 · 3 comments

Comments

@LukaHorvat
Copy link

data Test a = Test { test :: a } deriving (Generic, ToSchema)

Results in this error

    * Overlapping instances for Data.Swagger.Internal.Schema.GToSchema
                                  (K1 GHC.Generics.R a)
        arising from the 'deriving' clause of a data type declaration
      Matching instances:
        two instances involving out-of-scope types
          instance [overlappable] ToSchema c =>
                                  Data.Swagger.Internal.Schema.GToSchema (K1 i c)
            -- Defined in `Data.Swagger.Internal.Schema'
          instance [overlapping] ToSchema c =>
                                 Data.Swagger.Internal.Schema.GToSchema (K1 i (Maybe c))
            -- Defined in `Data.Swagger.Internal.Schema'
      (The choice depends on the instantiation of `a'
       To pick the first instance above, use IncoherentInstances
       when compiling the other instance declarations)
    * When deriving the instance for (ToSchema (Test a))
@fizruk
Copy link
Member

fizruk commented Mar 9, 2018

For some reason it does not work with DeriveAnyClass, yes.
But it does work like this:

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 a with Maybe Int):

>>> encode $ toSchema (Proxy @(Test (Maybe Int)))
"{\"required\":[\"test\"],\"properties\":{\"test\":{\"maximum\":9223372036854775807,\"minimum\":-9223372036854775808,\"type\":\"integer\"}},\"type\":\"object\"}"

@LukaHorvat
Copy link
Author

Very interesting. I wonder why it works at all. The error I posted does seem reasonable.

Thanks for the workaround!

@LukaHorvat
Copy link
Author

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 Maybe value. Here's how:

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 instance ToSchema a => ToSchema (Test a) is ever enough since the choice between picking the optional and the required instance will always depend on the instantiation of a, yet GHC seems to choose the more general instance (the one that results in required fields in this case) and then infers the ToSchema a requirement from that. What the above instance does is it adds a GToSchema requirement at just the right place where the optional/required choice needs to be made so when the instance is derived and that "subinstance" is required, the search stops instead of propagating further. Only when we instantiate the variable with something concrete is this constraint resolved and by that point the compiler has enough information to pick the correct instance.

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