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