From 837a7b6c6b2d7456aaba7842b5ce9bf363778259 Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Mon, 29 Jul 2024 13:41:14 +0100 Subject: [PATCH 1/3] 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 From 269877cfcf9ef8fdcdb73f2d4f00eb750e993709 Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Mon, 29 Jul 2024 15:16:40 +0100 Subject: [PATCH 2/3] Use `anyOf` instead of `oneOf` to fix nested `Maybe`s + See 0c8ca8bebb1d07925aeda6e613c2ed1c9e422abd --- src/Data/OpenApi/Internal/Schema.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index f8c23360..ab1c0d9e 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -627,7 +627,7 @@ instance ToSchema a => ToSchema (Maybe a) where NamedSchema mName aSchema <- declareNamedSchema (Proxy :: Proxy a) let aSchemaWithNull = mempty - { _schemaOneOf = Just [Inline aSchema, Inline mempty { _schemaType = Just OpenApiNull }] + { _schemaAnyOf = Just [Inline aSchema, Inline mempty { _schemaType = Just OpenApiNull }] } return $ NamedSchema mName aSchemaWithNull From 5ac3f3388f9383aeabd0595ce852587d4f285555 Mon Sep 17 00:00:00 2001 From: Junaid Rasheed Date: Mon, 29 Jul 2024 15:16:56 +0100 Subject: [PATCH 3/3] return -> pure --- src/Data/OpenApi/Internal/Schema.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/OpenApi/Internal/Schema.hs b/src/Data/OpenApi/Internal/Schema.hs index ab1c0d9e..fb25f093 100644 --- a/src/Data/OpenApi/Internal/Schema.hs +++ b/src/Data/OpenApi/Internal/Schema.hs @@ -630,7 +630,7 @@ instance ToSchema a => ToSchema (Maybe a) where { _schemaAnyOf = Just [Inline aSchema, Inline mempty { _schemaType = Just OpenApiNull }] } - return $ NamedSchema mName aSchemaWithNull + pure $ NamedSchema mName aSchemaWithNull instance (ToSchema a, ToSchema b) => ToSchema (Either a b) where -- To match Aeson instance