@@ -42,7 +42,6 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
42import Data.Int
42import Data.Int
43import Data.IntSet (IntSet)
43import Data.IntSet (IntSet)
44import Data.IntMap (IntMap)
44import Data.IntMap (IntMap)
45−import Data.List (sort)
46import Data.List.NonEmpty.Compat (NonEmpty)
45import Data.List.NonEmpty.Compat (NonEmpty)
47import Data.Map (Map)
46import Data.Map (Map)
48import Data.Maybe (fromMaybe)
47import Data.Maybe (fromMaybe)
@@ -64,14 +63,13 @@ import GHC.Generics
64import qualified Data.UUID.Types as UUID
63import qualified Data.UUID.Types as UUID
65import Type.Reflection (Typeable, typeRep)
64import Type.Reflection (Typeable, typeRep)
67−import Data.OpenApi.Aeson.Compat (keyToText, objectKeys, toInsOrdHashMap)
68−import Data.OpenApi.Declare
66+import Data.OpenApi.Declare
69−import Data.OpenApi.Internal
67+import Data.OpenApi.Internal
70−import Data.OpenApi.Internal.ParamSchema (ToParamSchema (..))
68+import Data.OpenApi.Internal.ParamSchema (ToParamSchema(..))
71−import Data.OpenApi.Internal.TypeShape
72−import Data.OpenApi.Lens hiding (name, schema)
69+import Data.OpenApi.Lens hiding (name, schema)
73−import qualified Data.OpenApi.Lens as Swagger
70+import qualified Data.OpenApi.Lens as Swagger
74−import Data.OpenApi.SchemaOptions
71+import Data.OpenApi.SchemaOptions
72+import Data.OpenApi.Internal.TypeShape
76import qualified Data.ByteString as BS
74import qualified Data.ByteString as BS
77import qualified Data.ByteString.Lazy.Char8 as BSL
75import qualified Data.ByteString.Lazy.Char8 as BSL
@@ -136,7 +134,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
137-- instance ToSchema Coord
135-- instance ToSchema Coord
139−class Typeable a => ToSchema a where
137+class ToSchema a where
140 -- | Convert a type into an optionally named schema
138 -- | Convert a type into an optionally named schema
141 -- together with all used definitions.
139 -- together with all used definitions.
142 -- Note that the schema itself is included in definitions
140 -- Note that the schema itself is included in definitions
@@ -158,19 +156,13 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
159-- >>> toNamedSchema (Proxy :: Proxy String) ^. name
157-- >>> toNamedSchema (Proxy :: Proxy String) ^. name
160-- Nothing
158-- Nothing
161−-- >>> BSL.putStrLn $ encodePretty (toNamedSchema (Proxy :: Proxy String) ^. schema)
159+-- >>> BSL.putStrLn $ encode (toNamedSchema (Proxy :: Proxy String) ^. schema)
163−-- "type": "string"
160+-- {"type":"string"}
166-- >>> toNamedSchema (Proxy :: Proxy Day) ^. name
162-- >>> toNamedSchema (Proxy :: Proxy Day) ^. name
167-- Just "Day"
163-- Just "Day"
168−-- >>> BSL.putStrLn $ encodePretty (toNamedSchema (Proxy :: Proxy Day) ^. schema)
164+-- >>> BSL.putStrLn $ encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
170−-- "example": "2016-07-22",
165+-- {"example":"2016-07-22","format":"date","type":"string"}
174toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
166toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
175toNamedSchema = undeclare . declareNamedSchema
167toNamedSchema = undeclare . declareNamedSchema
@@ -186,35 +178,22 @@ schemaName = _namedSchemaName . toNamedSchema
187-- | Convert a type into a schema.
179-- | Convert a type into a schema.
189−-- >>> BSL.putStrLn $ encodePretty $ toSchema (Proxy :: Proxy Int8)
181+-- >>> BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy Int8)
193−-- "type": "integer"
182+-- {"maximum":127,"minimum":-128,"type":"integer"}
196−-- >>> BSL.putStrLn $ encodePretty $ toSchema (Proxy :: Proxy [Day])
184+-- >>> BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy [Day])
199−-- "$ref": "#/components/schemas/Day"
185+-- {"items":{"$ref":"#/components/schemas/Day"},"type":"array"}
203toSchema :: ToSchema a => Proxy a -> Schema
186toSchema :: ToSchema a => Proxy a -> Schema
204toSchema = _namedSchemaSchema . toNamedSchema
187toSchema = _namedSchemaSchema . toNamedSchema
206-- | Convert a type into a referenced schema if possible.
189-- | Convert a type into a referenced schema if possible.
207-- Only named schemas can be referenced, nameless schemas are inlined.
190-- Only named schemas can be referenced, nameless schemas are inlined.
209−-- >>> BSL.putStrLn $ encodePretty $ toSchemaRef (Proxy :: Proxy Integer)
192+-- >>> BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Integer)
211−-- "type": "integer"
193+-- {"type":"integer"}
214−-- >>> BSL.putStrLn $ encodePretty $ toSchemaRef (Proxy :: Proxy Day)
195+-- >>> BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Day)
216−-- "$ref": "#/components/schemas/Day"
196+-- {"$ref":"#/components/schemas/Day"}
218toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
197toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
219toSchemaRef = undeclare . declareSchemaRef
198toSchemaRef = undeclare . declareSchemaRef
@@ -282,15 +261,8 @@ inlineAllSchemas = inlineSchemasWhen (const True)
283-- | Convert a type into a schema without references.
262-- | Convert a type into a schema without references.
285−-- >>> BSL.putStrLn $ encodePretty $ toInlinedSchema (Proxy :: Proxy [Day])
264+-- >>> BSL.putStrLn $ encode $ toInlinedSchema (Proxy :: Proxy [Day])
288−-- "example": "2016-07-22",
265+-- {"items":{"example":"2016-07-22","format":"date","type":"string"},"type":"array"}
295-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
267-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
296-- when inlining recursive schemas.
268-- when inlining recursive schemas.
@@ -323,64 +295,19 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
323-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
295-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
324-- Produced schema can be used for further refinement.
296-- Produced schema can be used for further refinement.
326−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema "hello"
298+-- >>> BSL.putStrLn $ encode $ sketchSchema "hello"
328−-- "example": "hello",
299+-- {"example":"hello","type":"string"}
332−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema (1, 2, 3)
301+-- >>> BSL.putStrLn $ encode $ sketchSchema (1, 2, 3)
340−-- "type": "number"
302+-- {"example":[1,2,3],"items":{"type":"number"},"type":"array"}
345−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema ("Jack", 25)
304+-- >>> BSL.putStrLn $ encode $ sketchSchema ("Jack", 25)
346−-- {
305+-- {"example":["Jack",25],"items":[{"type":"string"},{"type":"number"}],"type":"array"}
362-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
307-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
363-- >>> instance ToJSON Person
308-- >>> instance ToJSON Person
364−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema (Person "Jack" 25)
309+-- >>> BSL.putStrLn $ encode $ sketchSchema (Person "Jack" 25)
365−-- {
310+-- {"example":{"age":25,"name":"Jack"},"required":["age","name"],"type":"object","properties":{"age":{"type":"number"},"name":{"type":"string"}}}
384sketchSchema :: ToJSON a => a -> Schema
311sketchSchema :: ToJSON a => a -> Schema
385sketchSchema = sketch . toJSON
312sketchSchema = sketch . toJSON
@@ -406,139 +333,25 @@ sketchSchema = sketch . toJSON
406 _ -> Nothing
333 _ -> Nothing
407 go (Object o) = mempty
334 go (Object o) = mempty
408 & type_ ?~ OpenApiObject
335 & type_ ?~ OpenApiObject
409− & required .~ sort (objectKeys o)
336+ & required .~ HashMap.keys o
410− & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
337+ & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
412-- | Make a restrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
339-- | Make a restrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
413-- Produced schema uses as much constraints as possible.
340-- Produced schema uses as much constraints as possible.
415−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema "hello"
342+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema "hello"
416−-- {
343+-- {"maxLength":5,"pattern":"hello","minLength":5,"type":"string","enum":["hello"]}
422−-- "pattern": "hello",
426−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema (1, 2, 3)
345+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema (1, 2, 3)
427−-- {
346+-- {"minItems":3,"uniqueItems":true,"items":[{"maximum":1,"minimum":1,"multipleOf":1,"type":"number","enum":[1]},{"maximum":2,"minimum":2,"multipleOf":2,"type":"number","enum":[2]},{"maximum":3,"minimum":3,"multipleOf":3,"type":"number","enum":[3]}],"maxItems":3,"type":"array","enum":[[1,2,3]]}
467−-- "uniqueItems": true
470−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema ("Jack", 25)
348+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema ("Jack", 25)
471−-- {
349+-- {"minItems":2,"uniqueItems":true,"items":[{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]},{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]}],"maxItems":2,"type":"array","enum":[["Jack",25]]}
485−-- "pattern": "Jack",
501−-- "uniqueItems": true
504-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
351-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
505-- >>> instance ToJSON Person
352-- >>> instance ToJSON Person
506−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema (Person "Jack" 25)
353+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema (Person "Jack" 25)
507−-- {
354+-- {"minProperties":2,"required":["age","name"],"maxProperties":2,"type":"object","enum":[{"age":25,"name":"Jack"}],"properties":{"age":{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]},"name":{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]}}}
514−-- "maxProperties": 2,
515−-- "minProperties": 2,
532−-- "pattern": "Jack",
542sketchStrictSchema :: ToJSON a => a -> Schema
355sketchStrictSchema :: ToJSON a => a -> Schema
543sketchStrictSchema = go . toJSON
356sketchStrictSchema = go . toJSON
@@ -570,13 +383,13 @@ sketchStrictSchema = go . toJSON
570 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
383 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
571 go js@(Object o) = mempty
384 go js@(Object o) = mempty
572 & type_ ?~ OpenApiObject
385 & type_ ?~ OpenApiObject
573− & required .~ sort names
386+ & required .~ names
574− & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
387+ & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
575 & maxProperties ?~ fromIntegral (length names)
388 & maxProperties ?~ fromIntegral (length names)
576 & minProperties ?~ fromIntegral (length names)
389 & minProperties ?~ fromIntegral (length names)
577 & enum_ ?~ [js]
390 & enum_ ?~ [js]
579− names = objectKeys o
392+ names = HashMap.keys o
581class GToSchema (f :: * -> *) where
394class GToSchema (f :: * -> *) where
582 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
395 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
@@ -611,7 +424,7 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
611instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
424instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
612instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
425instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
614−instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
427+instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
616instance ToSchema a => ToSchema (Maybe a) where
429instance ToSchema a => ToSchema (Maybe a) where
617 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
430 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
@@ -628,18 +441,12 @@ instance ToSchema UUID.UUID where
628 declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
441 declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
629 & example ?~ toJSON (UUID.toText UUID.nil)
442 & example ?~ toJSON (UUID.toText UUID.nil)
631−instance (ToSchema a, ToSchema b) => ToSchema (a, b) where
444+instance (ToSchema a, ToSchema b) => ToSchema (a, b)
632− declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
633−instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c) where
445+instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
634− declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
635−instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d) where
446+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema 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
447+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema 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
448+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema 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
449+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f, ToSchema g) => ToSchema (a, b, c, d, e, f, g)
642− declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
644timeSchema :: T.Text -> Schema
451timeSchema :: T.Text -> Schema
645timeSchema fmt = mempty
452timeSchema fmt = mempty
@@ -658,7 +465,7 @@ instance ToSchema LocalTime where
658 declareNamedSchema _ = pure $ named "LocalTime" $ timeSchema "yyyy-mm-ddThh:MM:ss"
465 declareNamedSchema _ = pure $ named "LocalTime" $ timeSchema "yyyy-mm-ddThh:MM:ss"
659 & example ?~ toJSON (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0))
466 & 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.
468+-- | Format @"date"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
662instance ToSchema ZonedTime where
469instance ToSchema ZonedTime where
663 declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
470 declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
664 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
471 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
@@ -690,7 +497,7 @@ instance ToSchemaByteStringError BSL.ByteString => ToSchema BSL.ByteString where
690instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
497instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
692-- | NOTE: This schema does not account for the uniqueness of keys.
499-- | NOTE: This schema does not account for the uniqueness of keys.
693−instance (ToSchema a) => ToSchema (IntMap a) where
500+instance ToSchema a => ToSchema (IntMap a) where
694 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
501 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
696instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
503instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
@@ -746,12 +553,8 @@ instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declar
747-- | Default schema for @'Bounded'@, @'Integral'@ types.
554-- | Default schema for @'Bounded'@, @'Integral'@ types.
749−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
556+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
751−-- "maximum": 32767,
557+-- {"maximum":32767,"minimum":-32768,"type":"integer"}
752−-- "minimum": -32768,
755toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
558toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
756toSchemaBoundedIntegral _ = mempty
559toSchemaBoundedIntegral _ = mempty
757 & type_ ?~ OpenApiInteger
560 & type_ ?~ OpenApiInteger
@@ -783,27 +586,8 @@ genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$>
783-- >>> instance ToSchema ButtonState
586-- >>> instance ToSchema ButtonState
784-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
587-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
785-- >>> type ImageUrl = T.Text
588-- >>> type ImageUrl = T.Text
786−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
589+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
787−-- {
590+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
808-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
592-- 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.
593-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -811,13 +595,13 @@ declareSchemaBoundedEnumKeyMapping :: forall map key value.
811 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
595 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
812 => Proxy (map key value) -> Declare (Definitions Schema) Schema
596 => Proxy (map key value) -> Declare (Definitions Schema) Schema
813declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
597declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
814− ToJSONKeyText getKey _ -> objectSchema getKey
598+ ToJSONKeyText keyToText _ -> objectSchema keyToText
815 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
599 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
817− objectSchema getKey = do
601+ objectSchema keyToText = do
818 valueRef <- declareSchemaRef (Proxy :: Proxy value)
602 valueRef <- declareSchemaRef (Proxy :: Proxy value)
819 let allKeys = [minBound..maxBound :: key]
603 let allKeys = [minBound..maxBound :: key]
820− mkPair k = (keyToText $ getKey k, valueRef)
604+ mkPair k = (keyToText k, valueRef)
821 return $ mempty
605 return $ mempty
822 & type_ ?~ OpenApiObject
606 & type_ ?~ OpenApiObject
823 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
607 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
@@ -830,27 +614,8 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
830-- >>> instance ToSchema ButtonState
614-- >>> instance ToSchema ButtonState
831-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
615-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
832-- >>> type ImageUrl = T.Text
616-- >>> type ImageUrl = T.Text
833−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
617+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
834−-- {
618+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
855-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
620-- 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.
621-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -860,7 +625,7 @@ toSchemaBoundedEnumKeyMapping :: forall map key value.
860toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
625toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
862-- | A configurable generic @'Schema'@ creator.
627-- | A configurable generic @'Schema'@ creator.
863−genericDeclareSchema :: (Generic a, GToSchema (Rep a), Typeable a) =>
628+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) =>
864 SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
629 SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
865genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
630genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
@@ -868,24 +633,9 @@ genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSche
868-- This function applied to @'defaultSchemaOptions'@
633-- This function applied to @'defaultSchemaOptions'@
869-- is used as the default for @'declareNamedSchema'@
634-- is used as the default for @'declareNamedSchema'@
870-- when the type is an instance of @'Generic'@.
635-- 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"
879−genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a), Typeable a) =>
636+genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a)) =>
880 SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
637 SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
881−genericDeclareNamedSchema opts _ =
882− rename (Just $ T.pack name) <$> gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
638+genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
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'.
640-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
891genericNameSchema :: forall a d f.
641genericNameSchema :: forall a d f.
@@ -945,13 +695,10 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
945 case schema ^. items of
695 case schema ^. items of
946 Just (OpenApiItemsArray [_]) -> fieldSchema
696 Just (OpenApiItemsArray [_]) -> fieldSchema
948− -- We have to run recordSchema instead of just using its defs,
698+ 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')
699+ return (unnamed schema)
954− (_, NamedSchema _ schema) = runDeclare recordSchema mempty
701+ (defs, NamedSchema _ schema) = runDeclare recordSchema mempty
955 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
702 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
956 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
703 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
@@ -1114,13 +861,58 @@ data Proxy2 a b = Proxy2
1115data Proxy3 a b c = Proxy3
862data Proxy3 a b c = Proxy3
864+-- | This class allows to generate schemas for polymorphic types (of kind @Type -> Type@).
868+-- >>> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
870+-- instance (ToSchema a, Typeable a) => ToSchema (Foo a) where
871+-- declareNamedSchema _ = declareNamedSchema @(BySchema1 Foo a) Proxy
874+-- >>> toNamedSchema @(Foo Int) Proxy ^. name
876+-- >>> toNamedSchema @(Foo Bool) Proxy ^. name
878+-- >>> toNamedSchema @(Foo (Foo T.Text)) Proxy ^. name
879+-- Just "Foo_(Foo_Text)"
880+class ToSchema1 (f :: * -> *) where
881+ declareNamedSchema1 :: (Generic (f a), GToSchema (Rep (f a)), ToSchema a) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema
883+ -- It would be cleaner to have GToSchema constraint only on default signature and not in the class method
884+ -- above, however sadly GHC does not like it.
885+ default declareNamedSchema1 :: forall a. (ToSchema a, Generic (f a), GToSchema (Rep (f a))) => Proxy f -> Proxy a -> Declare (Definitions Schema) NamedSchema
886+ declareNamedSchema1 _ _ = genericDeclareNamedSchema @(f a) defaultSchemaOptions Proxy
888+{- | For GHC 8.6+ it's more convenient to use @DerivingVia@ to derive instances of 'ToSchema'
889+using 'ToSchema1' instance, like this:
891+#if __GLASGOW_HASKELL__ >= 806
892+>>> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
893+>>> deriving via BySchema1 Foo a instance (ToSchema a, Typeable a) => ToSchema (Foo a)
895+> data Foo a = Foo { foo :: a, bar :: Int } deriving (Eq, Show, Generic, ToSchema1)
896+> deriving via BySchema1 Foo a instance (ToSchema a, Typeable a) => ToSchema (Foo a)
899+newtype BySchema1 f a = BySchema1 (f a)
901+instance (ToSchema1 f, Generic (f a), GToSchema (Rep (f a)), Typeable (f a), ToSchema a) => ToSchema (BySchema1 f a) where
902+ declareNamedSchema _ = do
903+ sch <- declareNamedSchema1 @f @a Proxy Proxy
904+ let tName = T.replace " " "_" $ T.pack $ show $ typeRep @(f a)
905+ return $ rename (Just tName) sch
1117{- $setup
907{- $setup
1118>>> import Data.OpenApi
908>>> import Data.OpenApi
1119>>> import Data.Aeson (encode)
909>>> import Data.Aeson (encode)
1120>>> import Data.Aeson.Types (toJSONKeyText)
910>>> import Data.Aeson.Types (toJSONKeyText)
1121−>>> import Data.OpenApi.Internal.Utils
1122>>> :set -XScopedTypeVariables
911>>> :set -XScopedTypeVariables
1123>>> :set -XDeriveAnyClass
912>>> :set -XDeriveAnyClass
1124>>> :set -XStandaloneDeriving
913>>> :set -XStandaloneDeriving
1125>>> :set -XTypeApplications
914>>> :set -XTypeApplications
915+#if __GLASGOW_HASKELL__ >= 806
916+>>> :set -XDerivingVia