@@ -24,7 +24,7 @@ module Data.OpenApi.Internal.Schema where
2424import Prelude ()
2525import Prelude.Compat
2626
27- import Control.Lens hiding (allOf )
27+ import Control.Lens hiding (allOf , anyOf )
2828import Data.Data.Lens (template )
2929
3030import Control.Applicative ((<|>) )
@@ -43,6 +43,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4343import Data.Int
4444import Data.IntSet (IntSet )
4545import Data.IntMap (IntMap )
46+ import Data.Kind
4647import Data.List (sort )
4748import Data.List.NonEmpty.Compat (NonEmpty )
4849import Data.Map (Map )
@@ -587,7 +588,7 @@ sketchStrictSchema = go . toJSON
587588 where
588589 names = objectKeys o
589590
590- class GToSchema (f :: * -> * ) where
591+ class GToSchema (f :: Type -> Type ) where
591592 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema ) NamedSchema
592593
593594instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a ] where
@@ -623,7 +624,10 @@ instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSc
623624instance (Typeable (Fixed a ), HasResolution a ) => ToSchema (Fixed a ) where declareNamedSchema = plain . paramSchemaToSchema
624625
625626instance ToSchema a => ToSchema (Maybe a ) where
626- declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a )
627+ declareNamedSchema _ = do
628+ ref <- declareSchemaRef (Proxy @ a )
629+ -- NB: using 'oneOf' goes wrong for nested Maybe's as both subschemas match 'null'.
630+ pure $ unnamed $ mempty & anyOf ?~ [Inline $ mempty & type_ ?~ OpenApiNull , ref]
627631
628632instance (ToSchema a , ToSchema b ) => ToSchema (Either a b ) where
629633 -- To match Aeson instance
@@ -1016,10 +1020,7 @@ instance {-# OVERLAPPING #-} (Selector s, ToSchema c) => GToSchema (S1 s (K1 i (
10161020instance {-# OVERLAPPABLE #-} (Selector s , GToSchema f ) => GToSchema (S1 s f ) where
10171021 gdeclareNamedSchema opts _ = fmap unnamed . withFieldSchema opts (Proxy2 :: Proxy2 s f ) True
10181022
1019- instance {-# OVERLAPPING #-} ToSchema c => GToSchema (K1 i (Maybe c )) where
1020- gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c )
1021-
1022- instance {-# OVERLAPPABLE #-} ToSchema c => GToSchema (K1 i c ) where
1023+ instance ToSchema c => GToSchema (K1 i c ) where
10231024 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c )
10241025
10251026instance ( GSumToSchema f
@@ -1031,7 +1032,9 @@ instance ( GSumToSchema f
10311032
10321033gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema ) NamedSchema
10331034gdeclareNamedSumSchema opts proxy _
1034- | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchemas)
1035+ | allNullaryToStringTag opts && allNullary = pure $ unnamed $ mempty
1036+ & type_ ?~ OpenApiString
1037+ & enum_ ?~ map (String . fst ) sumSchemas
10351038 | otherwise = do
10361039 (schemas, _) <- runWriterT declareSumSchema
10371040 return $ unnamed $ mempty
@@ -1040,13 +1043,9 @@ gdeclareNamedSumSchema opts proxy _
10401043 declareSumSchema = gsumToSchema opts proxy
10411044 (sumSchemas, All allNullary) = undeclare (runWriterT declareSumSchema)
10421045
1043- toStringTag schemas = mempty
1044- & type_ ?~ OpenApiString
1045- & enum_ ?~ map (String . fst ) sumSchemas
1046-
10471046type AllNullary = All
10481047
1049- class GSumToSchema (f :: * -> * ) where
1048+ class GSumToSchema (f :: Type -> Type ) where
10501049 gsumToSchema :: SchemaOptions -> Proxy f -> WriterT AllNullary (Declare (Definitions Schema )) [(T. Text , Referenced Schema )]
10511050
10521051instance (GSumToSchema f , GSumToSchema g ) => GSumToSchema (f :+: g ) where
0 commit comments