From 837a7b6c6b2d7456aaba7842b5ce9bf363778259 Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Mon, 29 Jul 2024 13:41:14 +0100 Subject: [PATCH] Add null to `Maybe a` schemas + 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` --- src/Data/OpenApi/Internal/Schema.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index da56acf0..f8c23360 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -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