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

A Maybe field will be invalid against a null #142

Closed
parsonsmatt opened this issue Mar 7, 2018 · 3 comments
Closed

A Maybe field will be invalid against a null #142

parsonsmatt opened this issue Mar 7, 2018 · 3 comments

Comments

@parsonsmatt
Copy link
Contributor

I'm getting validation errors against Maybe fields:

>>> :set -XDeriveGeneric
>>> data X = X { foo :: Maybe Int } deriving (Show, Generic)
>>> instance ToSchema X
>>> instance ToJSON X
>>> validateToJSON (X Nothing)
["expected JSON value of type SwaggerInteger"]
>>> encode (X Nothing)
"{\"foo\":null}"

Omitting "foo" from the required fields does not fix this issue.

What's the right way to get maybe fields to validate correctly?

@parsonsmatt parsonsmatt changed the title Maybe fields footgun A Maybe field will be invalid against a null Mar 7, 2018
@fizruk
Copy link
Member

fizruk commented Mar 7, 2018

I personally use omitNothingFields for all my JSON instances, so I don't encounter this problem.

We could alter validateToJSON so that null is considered equivalent to a missing field.
Although I'm not 100% sure that's the right thing to do (I think it might break some code in theory).

A better solution would probably be to add some nullable support (e.g. see #128).

@parsonsmatt
Copy link
Contributor Author

omitNothingFields is not helping this for me :(

>>> data X = X { foo :: Maybe Int } deriving (Show, Eq, Generic)
>>> instance ToJSON X where toJSON = genericToJSON defaultOptions { omitNothingFields = True }
>>> instance ToSchema X
>>> validateToJSON (X Nothing)
["property \"foo\" is required, but not found in \"{}\""]

@fizruk
Copy link
Member

fizruk commented Mar 7, 2018

Ah, I see omitNothingFields actually has nothing to do with your problem.

Your problem is because of a weird behaviour feature (bug) of Generic-based ToSchema instance which only happens for single field data types with Maybe! It just so happens that I've never tried that case.

This is how everything works if you just add another field (even without omitNothingFields):

>>> data X = X { foo :: Maybe Int, bar :: Maybe String } deriving (Show, Eq, Generic)
>>> instance ToJSON X
>>> instance ToSchema X
>>> validateToJSON (X Nothing Nothing)
[]

What's causing this

The problem is specifically due to this line:

      recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s

Which calls something with S1 s f and then GHC just picks this instance:

instance OVERLAPPABLE_ (Selector s, GToSchema f) => GToSchema (S1 s f) where ...

over this one:

instance OVERLAPPING_ (Selector s, ToSchema c) => GToSchema (S1 s (K1 i (Maybe c))) where ...

even when there's a Maybe. That's because GHC has to pick an instance before it knows whether it can specialise to Maybe or not.

I think I'm going to send a PR fixing this right now.

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