@@ -1,17 +1,15 @@
1−{-# LANGUAGE CPP #-}
21{-# LANGUAGE ConstraintKinds #-}
3−{-# LANGUAGE DataKinds #-}
42{-# LANGUAGE DefaultSignatures #-}
53{-# LANGUAGE FlexibleContexts #-}
64{-# LANGUAGE FlexibleInstances #-}
75{-# LANGUAGE KindSignatures #-}
6+{-# LANGUAGE DataKinds #-}
87{-# LANGUAGE OverloadedLists #-}
98{-# LANGUAGE OverloadedStrings #-}
109{-# LANGUAGE PackageImports #-}
1110{-# LANGUAGE PolyKinds #-}
1211{-# LANGUAGE ScopedTypeVariables #-}
1312{-# LANGUAGE TupleSections #-}
14−{-# LANGUAGE TypeApplications #-}
1513{-# LANGUAGE TypeFamilies #-}
1614{-# LANGUAGE TypeOperators #-}
1715{-# LANGUAGE TypeSynonymInstances #-}
@@ -42,7 +40,6 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4240import Data.Int
4341import Data.IntSet (IntSet)
4442import Data.IntMap (IntMap)
45−import Data.List (sort)
4643import Data.List.NonEmpty.Compat (NonEmpty)
4744import Data.Map (Map)
4845import Data.Maybe (fromMaybe)
@@ -62,16 +59,14 @@ import Numeric.Natural.Compat (Natural)
6259import Data.Word
6360import GHC.Generics
6461import qualified Data.UUID.Types as UUID
65−import Type.Reflection (Typeable, typeRep)
6662
67−import Data.OpenApi.Aeson.Compat (keyToText, objectKeys, toInsOrdHashMap)
68−import Data.OpenApi.Declare
69−import Data.OpenApi.Internal
70−import Data.OpenApi.Internal.ParamSchema (ToParamSchema (..))
71−import Data.OpenApi.Internal.TypeShape
72−import Data.OpenApi.Lens hiding (name, schema)
73−import qualified Data.OpenApi.Lens as Swagger
74−import Data.OpenApi.SchemaOptions
63+import Data.OpenApi.Declare
64+import Data.OpenApi.Internal
65+import Data.OpenApi.Internal.ParamSchema (ToParamSchema(..))
66+import Data.OpenApi.Lens hiding (name, schema)
67+import qualified Data.OpenApi.Lens as Swagger
68+import Data.OpenApi.SchemaOptions
69+import Data.OpenApi.Internal.TypeShape
7570
7671import qualified Data.ByteString as BS
7772import qualified Data.ByteString.Lazy.Char8 as BSL
@@ -136,7 +131,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
136131--
137132-- instance ToSchema Coord
138133-- @
139−class Typeable a => ToSchema a where
134+class ToSchema a where
140135 -- | Convert a type into an optionally named schema
141136 -- together with all used definitions.
142137 -- Note that the schema itself is included in definitions
@@ -158,19 +153,13 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
158153--
159154-- >>> toNamedSchema (Proxy :: Proxy String) ^. name
160155-- Nothing
161−-- >>> BSL.putStrLn $ encodePretty (toNamedSchema (Proxy :: Proxy String) ^. schema)
162−-- {
163−-- "type": "string"
164−-- }
156+-- >>> BSL.putStrLn $ encode (toNamedSchema (Proxy :: Proxy String) ^. schema)
157+-- {"type":"string"}
165158--
166159-- >>> toNamedSchema (Proxy :: Proxy Day) ^. name
167160-- Just "Day"
168−-- >>> BSL.putStrLn $ encodePretty (toNamedSchema (Proxy :: Proxy Day) ^. schema)
169−-- {
170−-- "example": "2016-07-22",
171−-- "format": "date",
172−-- "type": "string"
173−-- }
161+-- >>> BSL.putStrLn $ encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
162+-- {"example":"2016-07-22","format":"date","type":"string"}
174163toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
175164toNamedSchema = undeclare . declareNamedSchema
176165
@@ -186,35 +175,22 @@ schemaName = _namedSchemaName . toNamedSchema
186175
187176-- | Convert a type into a schema.
188177--
189−-- >>> BSL.putStrLn $ encodePretty $ toSchema (Proxy :: Proxy Int8)
190−-- {
191−-- "maximum": 127,
192−-- "minimum": -128,
193−-- "type": "integer"
194−-- }
178+-- >>> BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy Int8)
179+-- {"maximum":127,"minimum":-128,"type":"integer"}
195180--
196−-- >>> BSL.putStrLn $ encodePretty $ toSchema (Proxy :: Proxy [Day])
197−-- {
198−-- "items": {
199−-- "$ref": "#/components/schemas/Day"
200−-- },
201−-- "type": "array"
202−-- }
181+-- >>> BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy [Day])
182+-- {"items":{"$ref":"#/components/schemas/Day"},"type":"array"}
203183toSchema :: ToSchema a => Proxy a -> Schema
204184toSchema = _namedSchemaSchema . toNamedSchema
205185
206186-- | Convert a type into a referenced schema if possible.
207187-- Only named schemas can be referenced, nameless schemas are inlined.
208188--
209−-- >>> BSL.putStrLn $ encodePretty $ toSchemaRef (Proxy :: Proxy Integer)
210−-- {
211−-- "type": "integer"
212−-- }
189+-- >>> BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Integer)
190+-- {"type":"integer"}
213191--
214−-- >>> BSL.putStrLn $ encodePretty $ toSchemaRef (Proxy :: Proxy Day)
215−-- {
216−-- "$ref": "#/components/schemas/Day"
217−-- }
192+-- >>> BSL.putStrLn $ encode $ toSchemaRef (Proxy :: Proxy Day)
193+-- {"$ref":"#/components/schemas/Day"}
218194toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
219195toSchemaRef = undeclare . declareSchemaRef
220196
@@ -282,15 +258,8 @@ inlineAllSchemas = inlineSchemasWhen (const True)
282258
283259-- | Convert a type into a schema without references.
284260--
285−-- >>> BSL.putStrLn $ encodePretty $ toInlinedSchema (Proxy :: Proxy [Day])
286−-- {
287−-- "items": {
288−-- "example": "2016-07-22",
289−-- "format": "date",
290−-- "type": "string"
291−-- },
292−-- "type": "array"
293−-- }
261+-- >>> BSL.putStrLn $ encode $ toInlinedSchema (Proxy :: Proxy [Day])
262+-- {"items":{"example":"2016-07-22","format":"date","type":"string"},"type":"array"}
294263--
295264-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
296265-- when inlining recursive schemas.
@@ -323,64 +292,19 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
323292-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
324293-- Produced schema can be used for further refinement.
325294--
326−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema "hello"
327−-- {
328−-- "example": "hello",
329−-- "type": "string"
330−-- }
295+-- >>> BSL.putStrLn $ encode $ sketchSchema "hello"
296+-- {"example":"hello","type":"string"}
331297--
332−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema (1, 2, 3)
333−-- {
334−-- "example": [
335−-- 1,
336−-- 2,
337−-- 3
338−-- ],
339−-- "items": {
340−-- "type": "number"
341−-- },
342−-- "type": "array"
343−-- }
298+-- >>> BSL.putStrLn $ encode $ sketchSchema (1, 2, 3)
299+-- {"example":[1,2,3],"items":{"type":"number"},"type":"array"}
344300--
345−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema ("Jack", 25)
346−-- {
347−-- "example": [
348−-- "Jack",
349−-- 25
350−-- ],
351−-- "items": [
352−-- {
353−-- "type": "string"
354−-- },
355−-- {
356−-- "type": "number"
357−-- }
358−-- ],
359−-- "type": "array"
360−-- }
301+-- >>> BSL.putStrLn $ encode $ sketchSchema ("Jack", 25)
302+-- {"example":["Jack",25],"items":[{"type":"string"},{"type":"number"}],"type":"array"}
361303--
362304-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
363305-- >>> instance ToJSON Person
364−-- >>> BSL.putStrLn $ encodePretty $ sketchSchema (Person "Jack" 25)
365−-- {
366−-- "example": {
367−-- "age": 25,
368−-- "name": "Jack"
369−-- },
370−-- "properties": {
371−-- "age": {
372−-- "type": "number"
373−-- },
374−-- "name": {
375−-- "type": "string"
376−-- }
377−-- },
378−-- "required": [
379−-- "age",
380−-- "name"
381−-- ],
382−-- "type": "object"
383−-- }
306+-- >>> BSL.putStrLn $ encode $ sketchSchema (Person "Jack" 25)
307+-- {"example":{"age":25,"name":"Jack"},"required":["age","name"],"type":"object","properties":{"age":{"type":"number"},"name":{"type":"string"}}}
384308sketchSchema :: ToJSON a => a -> Schema
385309sketchSchema = sketch . toJSON
386310 where
@@ -406,139 +330,25 @@ sketchSchema = sketch . toJSON
406330 _ -> Nothing
407331 go (Object o) = mempty
408332 & type_ ?~ OpenApiObject
409− & required .~ sort (objectKeys o)
410− & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
333+ & required .~ HashMap.keys o
334+ & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
411335
412336-- | Make a restrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
413337-- Produced schema uses as much constraints as possible.
414338--
415−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema "hello"
416−-- {
417−-- "enum": [
418−-- "hello"
419−-- ],
420−-- "maxLength": 5,
421−-- "minLength": 5,
422−-- "pattern": "hello",
423−-- "type": "string"
424−-- }
339+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema "hello"
340+-- {"maxLength":5,"pattern":"hello","minLength":5,"type":"string","enum":["hello"]}
425341--
426−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema (1, 2, 3)
427−-- {
428−-- "enum": [
429−-- [
430−-- 1,
431−-- 2,
432−-- 3
433−-- ]
434−-- ],
435−-- "items": [
436−-- {
437−-- "enum": [
438−-- 1
439−-- ],
440−-- "maximum": 1,
441−-- "minimum": 1,
442−-- "multipleOf": 1,
443−-- "type": "number"
444−-- },
445−-- {
446−-- "enum": [
447−-- 2
448−-- ],
449−-- "maximum": 2,
450−-- "minimum": 2,
451−-- "multipleOf": 2,
452−-- "type": "number"
453−-- },
454−-- {
455−-- "enum": [
456−-- 3
457−-- ],
458−-- "maximum": 3,
459−-- "minimum": 3,
460−-- "multipleOf": 3,
461−-- "type": "number"
462−-- }
463−-- ],
464−-- "maxItems": 3,
465−-- "minItems": 3,
466−-- "type": "array",
467−-- "uniqueItems": true
468−-- }
342+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema (1, 2, 3)
343+-- {"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]]}
469344--
470−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema ("Jack", 25)
471−-- {
472−-- "enum": [
473−-- [
474−-- "Jack",
475−-- 25
476−-- ]
477−-- ],
478−-- "items": [
479−-- {
480−-- "enum": [
481−-- "Jack"
482−-- ],
483−-- "maxLength": 4,
484−-- "minLength": 4,
485−-- "pattern": "Jack",
486−-- "type": "string"
487−-- },
488−-- {
489−-- "enum": [
490−-- 25
491−-- ],
492−-- "maximum": 25,
493−-- "minimum": 25,
494−-- "multipleOf": 25,
495−-- "type": "number"
496−-- }
497−-- ],
498−-- "maxItems": 2,
499−-- "minItems": 2,
500−-- "type": "array",
501−-- "uniqueItems": true
502−-- }
345+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema ("Jack", 25)
346+-- {"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]]}
503347--
504348-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
505349-- >>> instance ToJSON Person
506−-- >>> BSL.putStrLn $ encodePretty $ sketchStrictSchema (Person "Jack" 25)
507−-- {
508−-- "enum": [
509−-- {
510−-- "age": 25,
511−-- "name": "Jack"
512−-- }
513−-- ],
514−-- "maxProperties": 2,
515−-- "minProperties": 2,
516−-- "properties": {
517−-- "age": {
518−-- "enum": [
519−-- 25
520−-- ],
521−-- "maximum": 25,
522−-- "minimum": 25,
523−-- "multipleOf": 25,
524−-- "type": "number"
525−-- },
526−-- "name": {
527−-- "enum": [
528−-- "Jack"
529−-- ],
530−-- "maxLength": 4,
531−-- "minLength": 4,
532−-- "pattern": "Jack",
533−-- "type": "string"
534−-- }
535−-- },
536−-- "required": [
537−-- "age",
538−-- "name"
539−-- ],
540−-- "type": "object"
541−-- }
350+-- >>> BSL.putStrLn $ encode $ sketchStrictSchema (Person "Jack" 25)
351+-- {"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"]}}}
542352sketchStrictSchema :: ToJSON a => a -> Schema
543353sketchStrictSchema = go . toJSON
544354 where
@@ -570,13 +380,13 @@ sketchStrictSchema = go . toJSON
570380 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
571381 go js@(Object o) = mempty
572382 & type_ ?~ OpenApiObject
573− & required .~ sort names
574− & properties .~ fmap (Inline . go) (toInsOrdHashMap o)
383+ & required .~ names
384+ & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
575385 & maxProperties ?~ fromIntegral (length names)
576386 & minProperties ?~ fromIntegral (length names)
577387 & enum_ ?~ [js]
578388 where
579− names = objectKeys o
389+ names = HashMap.keys o
580390
581391class GToSchema (f :: * -> *) where
582392 gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
@@ -611,7 +421,7 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
611421instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
612422instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
613423
614−instance (Typeable (Fixed a), HasResolution a) => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
424+instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
615425
616426instance ToSchema a => ToSchema (Maybe a) where
617427 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
@@ -628,18 +438,12 @@ instance ToSchema UUID.UUID where
628438 declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
629439 & example ?~ toJSON (UUID.toText UUID.nil)
630440
631−instance (ToSchema a, ToSchema b) => ToSchema (a, b) where
632− declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
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
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
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
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
642− declareNamedSchema = fmap unname . genericDeclareNamedSchema defaultSchemaOptions
441+instance (ToSchema a, ToSchema b) => ToSchema (a, b)
442+instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
443+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
444+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e) => ToSchema (a, b, c, d, e)
445+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f) => ToSchema (a, b, c, d, e, f)
446+instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f, ToSchema g) => ToSchema (a, b, c, d, e, f, g)
643447
644448timeSchema :: T.Text -> Schema
645449timeSchema fmt = mempty
@@ -658,7 +462,7 @@ instance ToSchema LocalTime where
658462 declareNamedSchema _ = pure $ named "LocalTime" $ timeSchema "yyyy-mm-ddThh:MM:ss"
659463 & example ?~ toJSON (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0))
660464
661−-- | Format @"date-time"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
465+-- | Format @"date"@ corresponds to @yyyy-mm-ddThh:MM:ss(Z|+hh:MM)@ format.
662466instance ToSchema ZonedTime where
663467 declareNamedSchema _ = pure $ named "ZonedTime" $ timeSchema "date-time"
664468 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
@@ -690,7 +494,7 @@ instance ToSchemaByteStringError BSL.ByteString => ToSchema BSL.ByteString where
690494instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
691495
692496-- | NOTE: This schema does not account for the uniqueness of keys.
693−instance (ToSchema a) => ToSchema (IntMap a) where
497+instance ToSchema a => ToSchema (IntMap a) where
694498 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
695499
696500instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
@@ -746,12 +550,8 @@ instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declar
746550
747551-- | Default schema for @'Bounded'@, @'Integral'@ types.
748552--
749−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
750−-- {
751−-- "maximum": 32767,
752−-- "minimum": -32768,
753−-- "type": "integer"
754−-- }
553+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
554+-- {"maximum":32767,"minimum":-32768,"type":"integer"}
755555toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
756556toSchemaBoundedIntegral _ = mempty
757557 & type_ ?~ OpenApiInteger
@@ -783,27 +583,8 @@ genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$>
783583-- >>> instance ToSchema ButtonState
784584-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
785585-- >>> type ImageUrl = T.Text
786−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
787−-- {
788−-- "properties": {
789−-- "Active": {
790−-- "type": "string"
791−-- },
792−-- "Disabled": {
793−-- "type": "string"
794−-- },
795−-- "Focus": {
796−-- "type": "string"
797−-- },
798−-- "Hover": {
799−-- "type": "string"
800−-- },
801−-- "Neutral": {
802−-- "type": "string"
803−-- }
804−-- },
805−-- "type": "object"
806−-- }
586+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
587+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
807588--
808589-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
809590-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -811,13 +592,13 @@ declareSchemaBoundedEnumKeyMapping :: forall map key value.
811592 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
812593 => Proxy (map key value) -> Declare (Definitions Schema) Schema
813594declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
814− ToJSONKeyText getKey _ -> objectSchema getKey
595+ ToJSONKeyText keyToText _ -> objectSchema keyToText
815596 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
816597 where
817− objectSchema getKey = do
598+ objectSchema keyToText = do
818599 valueRef <- declareSchemaRef (Proxy :: Proxy value)
819600 let allKeys = [minBound..maxBound :: key]
820− mkPair k = (keyToText $ getKey k, valueRef)
601+ mkPair k = (keyToText k, valueRef)
821602 return $ mempty
822603 & type_ ?~ OpenApiObject
823604 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
@@ -830,27 +611,8 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
830611-- >>> instance ToSchema ButtonState
831612-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
832613-- >>> type ImageUrl = T.Text
833−-- >>> BSL.putStrLn $ encodePretty $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
834−-- {
835−-- "properties": {
836−-- "Active": {
837−-- "type": "string"
838−-- },
839−-- "Disabled": {
840−-- "type": "string"
841−-- },
842−-- "Focus": {
843−-- "type": "string"
844−-- },
845−-- "Hover": {
846−-- "type": "string"
847−-- },
848−-- "Neutral": {
849−-- "type": "string"
850−-- }
851−-- },
852−-- "type": "object"
853−-- }
614+-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
615+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
854616--
855617-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
856618-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -860,7 +622,7 @@ toSchemaBoundedEnumKeyMapping :: forall map key value.
860622toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
861623
862624-- | A configurable generic @'Schema'@ creator.
863−genericDeclareSchema :: (Generic a, GToSchema (Rep a), Typeable a) =>
625+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) =>
864626 SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
865627genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
866628
@@ -868,24 +630,9 @@ genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSche
868630-- This function applied to @'defaultSchemaOptions'@
869631-- is used as the default for @'declareNamedSchema'@
870632-- when the type is an instance of @'Generic'@.
871−--
872−-- Default implementation will use the name from 'Typeable' instance, including concrete
873−-- instantioations of type variables.
874−--
875−-- For example:
876−--
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) =>
633+genericDeclareNamedSchema :: forall a. (Generic a, GToSchema (Rep a)) =>
880634 SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
881−genericDeclareNamedSchema opts _ =
882− rename (Just $ T.pack name) <$> gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
883− where
884− unspace ' ' = '_'
885− unspace x = x
886− orig = fmap unspace $ show $ typeRep @a
887− name = datatypeNameModifier opts orig
888−
635+genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
889636
890637-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
891638genericNameSchema :: forall a d f.
@@ -945,13 +692,10 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
945692 case schema ^. items of
946693 Just (OpenApiItemsArray [_]) -> fieldSchema
947694 _ -> do
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
952− return (unnamed schema')
695+ declare defs
696+ return (unnamed schema)
953697 where
954− (_, NamedSchema _ schema) = runDeclare recordSchema mempty
698+ (defs, NamedSchema _ schema) = runDeclare recordSchema mempty
955699 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
956700 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
957701
@@ -1114,13 +858,7 @@ data Proxy2 a b = Proxy2
1114858
1115859data Proxy3 a b c = Proxy3
1116860
1117−{- $setup
1118−>>> import Data.OpenApi
1119−>>> import Data.Aeson (encode)
1120−>>> import Data.Aeson.Types (toJSONKeyText)
1121−>>> import Data.OpenApi.Internal.Utils
1122−>>> :set -XScopedTypeVariables
1123−>>> :set -XDeriveAnyClass
1124−>>> :set -XStandaloneDeriving
1125−>>> :set -XTypeApplications
1126−-}
861+-- $setup
862+-- >>> import Data.OpenApi
863+-- >>> import Data.Aeson (encode)
864+-- >>> import Data.Aeson.Types (toJSONKeyText)