Skip to content

Commit 561d436

Browse files
committed
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`
1 parent 89825c4 commit 561d436

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/Data/OpenApi/Internal/Schema.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ import qualified Data.ByteString as BS
7979
import qualified Data.ByteString.Lazy.Char8 as BSL
8080
import GHC.TypeLits (TypeError, ErrorMessage(..))
8181

82+
import qualified Debug.Trace as Trace
83+
8284
unnamed :: Schema -> NamedSchema
8385
unnamed schema = NamedSchema Nothing schema
8486

@@ -623,7 +625,14 @@ instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSc
623625
instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
624626

625627
instance ToSchema a => ToSchema (Maybe a) where
626-
declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
628+
declareNamedSchema _ = do
629+
NamedSchema mName aSchema <- declareNamedSchema (Proxy :: Proxy a)
630+
631+
let aSchemaWithNull = mempty
632+
{ _schemaOneOf = Just [Inline aSchema, Inline mempty { _schemaType = Just OpenApiNull }]
633+
}
634+
635+
return $ NamedSchema mName aSchemaWithNull
627636

628637
instance (ToSchema a, ToSchema b) => ToSchema (Either a b) where
629638
-- To match Aeson instance

0 commit comments

Comments
 (0)