@@ -64,13 +64,14 @@ import GHC.Generics
64import qualified Data.UUID.Types as UUID
64import qualified Data.UUID.Types as UUID
65import Type.Reflection (Typeable, typeRep)
65import Type.Reflection (Typeable, typeRep)
67+import Data.OpenApi.Aeson.Compat (keyToText, objectKeys, toInsOrdHashMap)
67−import Data.OpenApi.Declare
68+import Data.OpenApi.Declare
68−import Data.OpenApi.Internal
69+import Data.OpenApi.Internal
69−import Data.OpenApi.Internal.ParamSchema (ToParamSchema(..))
70+import Data.OpenApi.Internal.ParamSchema (ToParamSchema (..))
71+import Data.OpenApi.Internal.TypeShape
70−import Data.OpenApi.Lens hiding (name, schema)
72+import Data.OpenApi.Lens hiding (name, schema)
71−import qualified Data.OpenApi.Lens as Swagger
73+import qualified Data.OpenApi.Lens as Swagger
72−import Data.OpenApi.SchemaOptions
74+import Data.OpenApi.SchemaOptions
73−import Data.OpenApi.Internal.TypeShape
75import qualified Data.ByteString as BS
76import qualified Data.ByteString as BS
76import qualified Data.ByteString.Lazy.Char8 as BSL
77import qualified Data.ByteString.Lazy.Char8 as BSL
@@ -135,13 +136,13 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
136-- instance ToSchema Coord
137-- instance ToSchema Coord
138−class ToSchema a where
139+class Typeable a => ToSchema a where
139 -- | Convert a type into an optionally named schema
140 -- | Convert a type into an optionally named schema
140 -- together with all used definitions.
141 -- together with all used definitions.
141 -- Note that the schema itself is included in definitions
142 -- Note that the schema itself is included in definitions
142 -- only if it is recursive (and thus needs its definition in scope).
143 -- only if it is recursive (and thus needs its definition in scope).
143 declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
144 declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
144− default declareNamedSchema :: (Generic a, GToSchema (Rep a), Typeable a) =>
145+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) =>
145 Proxy a -> Declare (Definitions Schema) NamedSchema
146 Proxy a -> Declare (Definitions Schema) NamedSchema
146 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
147 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
@@ -405,8 +406,8 @@ sketchSchema = sketch . toJSON
405 _ -> Nothing
406 _ -> Nothing
406 go (Object o) = mempty
407 go (Object o) = mempty
407 & type_ ?~ OpenApiObject
408 & type_ ?~ OpenApiObject
408− & required .~ sort (HashMap.keys o)
409+ & required .~ sort (objectKeys o)
409− & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
410+ & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
411-- | Make a restrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
412-- | Make a restrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
412-- Produced schema uses as much constraints as possible.
413-- Produced schema uses as much constraints as possible.
@@ -570,12 +571,12 @@ sketchStrictSchema = go . toJSON
570 go js@(Object o) = mempty
571 go js@(Object o) = mempty
571 & type_ ?~ OpenApiObject
572 & type_ ?~ OpenApiObject
572 & required .~ sort names
573 & required .~ sort names
573− & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
574+ & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
574 & maxProperties ?~ fromIntegral (length names)
575 & maxProperties ?~ fromIntegral (length names)
575 & minProperties ?~ fromIntegral (length names)
576 & minProperties ?~ fromIntegral (length names)
576 & enum_ ?~ [js]
577 & enum_ ?~ [js]
578− names = HashMap.keys o
579+ names = objectKeys o
580class GToSchema (f :: * -> *) where
581class GToSchema (f :: * -> *) where
581 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
582 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
@@ -610,12 +611,12 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
610instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
611instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
611instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
612instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
613−instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
614+instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
615instance ToSchema a => ToSchema (Maybe a) where
616instance ToSchema a => ToSchema (Maybe a) where
616 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
617 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
618−instance (ToSchema a, ToSchema b, Typeable a, Typeable b) => ToSchema (Either a b) where
619+instance (ToSchema a, ToSchema b) => ToSchema (Either a b) where
619 -- To match Aeson instance
620 -- To match Aeson instance
620 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions { sumEncoding = ObjectWithSingleField }
621 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions { sumEncoding = ObjectWithSingleField }
@@ -627,12 +628,18 @@ instance ToSchema UUID.UUID where
627 declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
628 declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
628 & example ?~ toJSON (UUID.toText UUID.nil)
629 & example ?~ toJSON (UUID.toText UUID.nil)
630−instance (ToSchema a, Typeable a, ToSchema b, Typeable b) => ToSchema (a, b)
631+instance (ToSchema a, ToSchema b) => ToSchema (a, b) where
632+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
631−instance (ToSchema a, Typeable a, ToSchema b, Typeable b, ToSchema c, Typeable c) => ToSchema (a, b, c)
633+instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c) where
632−instance (ToSchema a, Typeable a, ToSchema b, Typeable b, ToSchema c, Typeable c, ToSchema d, Typeable d) => ToSchema (a, b, c, d)
634+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
635+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d) where
636+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
633−instance (ToSchema a, Typeable a, ToSchema b, Typeable b, ToSchema c, Typeable c, ToSchema d, Typeable d, ToSchema e, Typeable e) => ToSchema (a, b, c, d, e)
637+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e) => ToSchema (a, b, c, d, e) where
638+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
634−instance (ToSchema a, Typeable a, ToSchema b, Typeable b, ToSchema c, Typeable c, ToSchema d, Typeable d, ToSchema e, Typeable e, ToSchema f, Typeable f) => ToSchema (a, b, c, d, e, f)
639+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f) => ToSchema (a, b, c, d, e, f) where
640+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
635−instance (ToSchema a, Typeable a, ToSchema b, Typeable b, ToSchema c, Typeable c, ToSchema d, Typeable d, ToSchema e, Typeable e, ToSchema f, Typeable f, ToSchema g, Typeable g) => ToSchema (a, b, c, d, e, f, g)
641+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f, ToSchema g) => ToSchema (a, b, c, d, e, f, g) where
642+ declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
637timeSchema :: T.Text -> Schema
644timeSchema :: T.Text -> Schema
638timeSchema fmt = mempty
645timeSchema fmt = mempty
@@ -651,7 +658,7 @@ instance ToSchema LocalTime where
651 declareNamedSchema _ = pure $ named "LocalTime" $ timeSchema "yyyy-mm-ddThh:MM:ss"
658 declareNamedSchema _ = pure $ named "LocalTime" $ timeSchema "yyyy-mm-ddThh:MM:ss"
652 & example ?~ toJSON (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0))
659 & example ?~ toJSON (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0))
654−-- | Format @"date"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
661+-- | Format @"date-time"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
655instance ToSchema ZonedTime where
662instance ToSchema ZonedTime where
656 declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
663 declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
657 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
664 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
@@ -683,10 +690,10 @@ instance ToSchemaByteStringError BSL.ByteString => ToSchema BSL.ByteString where
683instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
690instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
685-- | NOTE: This schema does not account for the uniqueness of keys.
692-- | NOTE: This schema does not account for the uniqueness of keys.
686−instance (ToSchema a, Typeable a) => ToSchema (IntMap a) where
693+instance (ToSchema a) => ToSchema (IntMap a) where
687 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
694 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
689−instance (ToJSONKey k, ToSchema k, ToSchema v, Typeable k, Typeable v) => ToSchema (Map k v) where
696+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
690 declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction k of
697 declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction k of
691 ToJSONKeyText _ _ -> declareObjectMapSchema
698 ToJSONKeyText _ _ -> declareObjectMapSchema
692 ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
699 ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
@@ -697,7 +704,7 @@ instance (ToJSONKey k, ToSchema k, ToSchema v, Typeable k, Typeable v) => ToSche
697 & type_ ?~ OpenApiObject
704 & type_ ?~ OpenApiObject
698 & additionalProperties ?~ AdditionalPropertiesSchema schema
705 & additionalProperties ?~ AdditionalPropertiesSchema schema
700−instance (ToJSONKey k, ToSchema k, ToSchema v, Typeable k, Typeable v) => ToSchema (HashMap k v) where
707+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
701 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
708 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
703instance {-# OVERLAPPING #-} ToSchema Object where
710instance {-# OVERLAPPING #-} ToSchema Object where
@@ -801,16 +808,16 @@ genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$>
801-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
808-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
802-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
809-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
803declareSchemaBoundedEnumKeyMapping :: forall map key value.
810declareSchemaBoundedEnumKeyMapping :: forall map key value.
804− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value, Typeable key, Typeable value)
811+ (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
805 => Proxy (map key value) -> Declare (Definitions Schema) Schema
812 => Proxy (map key value) -> Declare (Definitions Schema) Schema
806declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
813declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
807− ToJSONKeyText keyToText _ -> objectSchema keyToText
814+ ToJSONKeyText getKey _ -> objectSchema getKey
808 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
815 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
810− objectSchema keyToText = do
817+ objectSchema getKey = do
811 valueRef <- declareSchemaRef (Proxy :: Proxy value)
818 valueRef <- declareSchemaRef (Proxy :: Proxy value)
812 let allKeys = [minBound..maxBound :: key]
819 let allKeys = [minBound..maxBound :: key]
813− mkPair k = (keyToText k, valueRef)
820+ mkPair k = (keyToText $ getKey k, valueRef)
814 return $ mempty
821 return $ mempty
815 & type_ ?~ OpenApiObject
822 & type_ ?~ OpenApiObject
816 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
823 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
@@ -848,7 +855,7 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
848-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
855-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
849-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
856-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
850toSchemaBoundedEnumKeyMapping :: forall map key value.
857toSchemaBoundedEnumKeyMapping :: forall map key value.
851− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value, Typeable key, Typeable value)
858+ (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
852 => Proxy (map key value) -> Schema
859 => Proxy (map key value) -> Schema
853toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
860toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
@@ -861,11 +868,23 @@ genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSche
861-- This function applied to @'defaultSchemaOptions'@
868-- This function applied to @'defaultSchemaOptions'@
862-- is used as the default for @'declareNamedSchema'@
869-- is used as the default for @'declareNamedSchema'@
863-- when the type is an instance of @'Generic'@.
870-- when the type is an instance of @'Generic'@.
872+-- Default implementation will use the name from 'Typeable' instance, including concrete
873+-- instantioations of type variables.
877+-- >>> _namedSchemaName $ undeclare $ genericDeclareNamedSchema defaultSchemaOptions (Proxy :: Proxy (Either Int Bool))
878+-- Just "Either_Int_Bool"
864genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a), Typeable a) =>
879genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a), Typeable a) =>
865 SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
880 SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
866genericDeclareNamedSchema opts _ =
881genericDeclareNamedSchema opts _ =
867− rename (Just tName) <$> gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
882+ rename (Just $ T.pack name) <$> gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
868− where tName = T.replace " " "_" $ T.pack $ show $ typeRep @a
883+ where
886+ orig = fmap unspace $ show $ typeRep @a
887+ name = datatypeNameModifier opts orig
871-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
890-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
@@ -926,10 +945,13 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
926 case schema ^. items of
945 case schema ^. items of
927 Just (OpenApiItemsArray [_]) -> fieldSchema
946 Just (OpenApiItemsArray [_]) -> fieldSchema
929− declare defs
948+ -- We have to run recordSchema instead of just using its defs,
949+ -- since those can be recursive and will lead to infinite loop,
950+ -- see https://github.com/biocad/openapi3/pull/37
951+ NamedSchema _ schema' <- recordSchema
930− return (unnamed schema)
952+ return (unnamed schema')
932− (defs, NamedSchema _ schema) = runDeclare recordSchema mempty
954+ (_, NamedSchema _ schema) = runDeclare recordSchema mempty
933 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
955 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
934 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
956 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
@@ -1092,49 +1114,6 @@ data Proxy2 a b = Proxy2
1093data Proxy3 a b c = Proxy3
1115data Proxy3 a b c = Proxy3
1095−-- | This class allows to generate schemas for polymorphic types (of kind @Type -> Type@).
1099−-- >>> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
1101−-- instance (ToSchema a, Typeable a) => ToSchema (Foo a) where
1102−-- declareNamedSchema _ = declareNamedSchema @(BySchema1 Foo a) Proxy
1105−-- >>> toNamedSchema @(Foo Int) Proxy ^. name
1107−-- >>> toNamedSchema @(Foo Bool) Proxy ^. name
1109−-- >>> toNamedSchema @(Foo (Foo T.Text)) Proxy ^. name
1110−-- Just "Foo_(Foo_Text)"
1111−class ToSchema1 (f :: * -> *) where
1112− declareNamedSchema1 :: (Generic (f a), GToSchema (Rep (f a)), ToSchema a) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema
1114− -- It would be cleaner to have GToSchema constraint only on default signature and not in the class method
1115− -- above, however sadly GHC does not like it.
1116− default declareNamedSchema1 :: forall a. (ToSchema a, Generic (f a), GToSchema (Rep (f a)), Typeable (f a)) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema
1117− declareNamedSchema1 _ _ = genericDeclareNamedSchema @(f a) defaultSchemaOptions Proxy
1119−{- | For GHC 8.6+ it's more convenient to use @DerivingVia@ to derive instances of 'ToSchema'
1120−using 'ToSchema1' instance, like this:
1122−#if __GLASGOW_HASKELL__ >= 806
1123−>>> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
1124−>>> deriving via BySchema1 Foo a instance (ToSchema a, Typeable a) => ToSchema (Foo a)
1126−> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
1127−> deriving via BySchema1 Foo a instance (ToSchema a, Typeable a) => ToSchema (Foo a)
1130−newtype BySchema1 f a = BySchema1 (f a)
1132−instance (ToSchema1 f, Generic (f a), GToSchema (Rep (f a)), Typeable (f a), ToSchema a) => ToSchema (BySchema1 f a) where
1133− declareNamedSchema _ = do
1134− sch <- declareNamedSchema1 @f @a Proxy Proxy
1135− let tName = T.replace " " "_" $ T.pack $ show $ typeRep @(f a)
1136− return $ rename (Just tName) sch
1138{- $setup
1117{- $setup
1139>>> import Data.OpenApi
1118>>> import Data.OpenApi
1140>>> import Data.Aeson (encode)
1119>>> import Data.Aeson (encode)
@@ -1144,7 +1123,4 @@ instance (ToSchema1 f, Generic (f a), GToSchema (Rep (f a)), Typeable (f a), ToS
1144>>> :set -XDeriveAnyClass
1123>>> :set -XDeriveAnyClass
1145>>> :set -XStandaloneDeriving
1124>>> :set -XStandaloneDeriving
1146>>> :set -XTypeApplications
1125>>> :set -XTypeApplications
1147−#if __GLASGOW_HASKELL__ >= 806
1148−>>> :set -XDerivingVia