Skip to content

Commit

Permalink
Add null to Maybe a schemas
Browse files Browse the repository at this point in the history
+ Generate a schema for `a`
+ Generate a schema with the `null` type
+ Combine both schemas with a `oneOf` to emulate `Maybe a`
+ Follows behaviour of other `Maybe` instances, e.g., `ToJSON`
  • Loading branch information
rasheedja committed Jul 29, 2024
1 parent 89825c4 commit 837a7b6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Data/OpenApi/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,14 @@ instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSc
instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema

instance ToSchema a => ToSchema (Maybe a) where
declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
declareNamedSchema _ = do
NamedSchema mName aSchema <- declareNamedSchema (Proxy :: Proxy a)

let aSchemaWithNull = mempty
{ _schemaOneOf = Just [Inline aSchema, Inline mempty { _schemaType = Just OpenApiNull }]
}

return $ NamedSchema mName aSchemaWithNull

instance (ToSchema a, ToSchema b) => ToSchema (Either a b) where
-- To match Aeson instance
Expand Down

0 comments on commit 837a7b6

Please sign in to comment.