@@ -1,5 +1,4 @@
11{-# LANGUAGE CPP #-}
2−{-# LANGUAGE ConstraintKinds #-}
32{-# LANGUAGE DefaultSignatures #-}
43{-# LANGUAGE FlexibleContexts #-}
54{-# LANGUAGE FlexibleInstances #-}
@@ -32,7 +31,7 @@ import Data.Data.Lens (template)
3231
3332import Control.Monad
3433import Control.Monad.Writer
35−import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
34+import Data.Aeson
3635import Data.Char
3736import Data.Data (Data)
3837import Data.Foldable (traverse_)
@@ -44,11 +43,9 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4443import Data.Int
4544import Data.IntSet (IntSet)
4645import Data.IntMap (IntMap)
47−import Data.List.NonEmpty.Compat (NonEmpty)
4846import Data.Map (Map)
4947import Data.Proxy
5048import Data.Scientific (Scientific)
51−import Data.Fixed (Fixed, HasResolution, Pico)
5249import Data.Set (Set)
5350import qualified Data.Text as T
5451import qualified Data.Text.Lazy as TL
@@ -57,8 +54,6 @@ import qualified Data.Vector as V
5754import qualified Data.Vector.Primitive as VP
5855import qualified Data.Vector.Storable as VS
5956import qualified Data.Vector.Unboxed as VU
60−import Data.Version (Version)
61−import Numeric.Natural.Compat (Natural)
6257import Data.Word
6358import GHC.Generics
6459import qualified Data.UUID.Types as UUID
@@ -69,7 +64,10 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
6964import Data.Swagger.Lens hiding (name, schema)
7065import qualified Data.Swagger.Lens as Swagger
7166import Data.Swagger.SchemaOptions
72−import Data.Swagger.Internal.TypeShape
67+
68+#ifdef __DOCTEST__
69+import Data.Swagger.Lens (name, schema)
70+#endif
7371
7472#if __GLASGOW_HASKELL__ < 800
7573#else
@@ -102,8 +100,6 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
102100-- {-\# LANGUAGE OverloadedLists \#-} -- allows to write 'Map' and 'HashMap' as lists
103101--
104102-- import Control.Lens
105−-- import Data.Proxy
106−-- import Data.Swagger
107103--
108104-- data Coord = Coord { x :: Double, y :: Double }
109105--
@@ -111,7 +107,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
111107-- declareNamedSchema _ = do
112108-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
113109-- return $ NamedSchema (Just \"Coord\") $ mempty
114−-- & type_ ?~ SwaggerObject
110+-- & type_ .~ SwaggerObject
115111-- & properties .~
116112-- [ (\"x\", doubleSchema)
117113-- , (\"y\", doubleSchema)
@@ -142,17 +138,12 @@ class ToSchema a where
142138 -- together with all used definitions.
143139 -- Note that the schema itself is included in definitions
144140 -- only if it is recursive (and thus needs its definition in scope).
145− declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
146− default declareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
147− Proxy a -> Declare (Definitions Schema) NamedSchema
141+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
142+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
148143 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149144
150−instance ToSchema TimeOfDay where
151− declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152− & example ?~ toJSON (TimeOfDay 12 33 15)
153−
154145-- | Convert a type into a schema and declare all used schema definitions.
155−declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
146+declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
156147declareSchema = fmap _namedSchemaSchema . declareNamedSchema
157148
158149-- | Convert a type into an optionally named schema.
@@ -166,7 +157,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
166157-- Just "Day"
167158-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
168159-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
169−toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
160+toNamedSchema :: ToSchema a => proxy a -> NamedSchema
170161toNamedSchema = undeclare . declareNamedSchema
171162
172163-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -176,7 +167,7 @@ toNamedSchema = undeclare . declareNamedSchema
176167--
177168-- >>> schemaName (Proxy :: Proxy UTCTime)
178169-- Just "UTCTime"
179−schemaName :: ToSchema a => Proxy a -> Maybe T.Text
170+schemaName :: ToSchema a => proxy a -> Maybe T.Text
180171schemaName = _namedSchemaName . toNamedSchema
181172
182173-- | Convert a type into a schema.
@@ -186,7 +177,7 @@ schemaName = _namedSchemaName . toNamedSchema
186177--
187178-- >>> encode $ toSchema (Proxy :: Proxy [Day])
188179-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
189−toSchema :: ToSchema a => Proxy a -> Schema
180+toSchema :: ToSchema a => proxy a -> Schema
190181toSchema = _namedSchemaSchema . toNamedSchema
191182
192183-- | Convert a type into a referenced schema if possible.
@@ -197,7 +188,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
197188--
198189-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
199190-- "{\"$ref\":\"#/definitions/Day\"}"
200−toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
191+toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
201192toSchemaRef = undeclare . declareSchemaRef
202193
203194-- | Convert a type into a referenced schema if possible
@@ -207,7 +198,7 @@ toSchemaRef = undeclare . declareSchemaRef
207198-- Schema definitions are typically declared for every referenced schema.
208199-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
209200-- will be declared (regardless of whether it is recusive or not).
210−declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
201+declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
211202declareSchemaRef proxy = do
212203 case toNamedSchema proxy of
213204 NamedSchema (Just name) schema -> do
@@ -269,7 +260,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
269260--
270261-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
271262-- when inlining recursive schemas.
272−toInlinedSchema :: ToSchema a => Proxy a -> Schema
263+toInlinedSchema :: ToSchema a => proxy a -> Schema
273264toInlinedSchema proxy = inlineAllSchemas defs schema
274265 where
275266 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -298,20 +289,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
298289-- | Default schema for binary data (any sequence of octets).
299290binarySchema :: Schema
300291binarySchema = mempty
301− & type_ ?~ SwaggerString
292+ & type_ .~ SwaggerString
302293 & format ?~ "binary"
303294
304295-- | Default schema for binary data (base64 encoded).
305296byteSchema :: Schema
306297byteSchema = mempty
307− & type_ ?~ SwaggerString
298+ & type_ .~ SwaggerString
308299 & format ?~ "byte"
309300
310301-- | Default schema for password string.
311302-- @"password"@ format is used to hint UIs the input needs to be obscured.
312303passwordSchema :: Schema
313304passwordSchema = mempty
314− & type_ ?~ SwaggerString
305+ & type_ .~ SwaggerString
315306 & format ?~ "password"
316307
317308-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -337,12 +328,12 @@ sketchSchema = sketch . toJSON
337328 sketch js@(Bool _) = go js
338329 sketch js = go js & example ?~ js
339330
340− go Null = mempty & type_ ?~ SwaggerNull
341− go (Bool _) = mempty & type_ ?~ SwaggerBoolean
342− go (String _) = mempty & type_ ?~ SwaggerString
343− go (Number _) = mempty & type_ ?~ SwaggerNumber
331+ go Null = mempty & type_ .~ SwaggerNull
332+ go (Bool _) = mempty & type_ .~ SwaggerBoolean
333+ go (String _) = mempty & type_ .~ SwaggerString
334+ go (Number _) = mempty & type_ .~ SwaggerNumber
344335 go (Array xs) = mempty
345− & type_ ?~ SwaggerArray
336+ & type_ .~ SwaggerArray
346337 & items ?~ case ischema of
347338 Just s -> SwaggerItemsObject (Inline s)
348339 _ -> SwaggerItemsArray (map Inline ys)
@@ -354,7 +345,7 @@ sketchSchema = sketch . toJSON
354345 (z:_) | allSame -> Just z
355346 _ -> Nothing
356347 go (Object o) = mempty
357− & type_ ?~ SwaggerObject
348+ & type_ .~ SwaggerObject
358349 & required .~ HashMap.keys o
359350 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
360351
@@ -377,24 +368,24 @@ sketchSchema = sketch . toJSON
377368sketchStrictSchema :: ToJSON a => a -> Schema
378369sketchStrictSchema = go . toJSON
379370 where
380− go Null = mempty & type_ ?~ SwaggerNull
371+ go Null = mempty & type_ .~ SwaggerNull
381372 go js@(Bool _) = mempty
382− & type_ ?~ SwaggerBoolean
373+ & type_ .~ SwaggerBoolean
383374 & enum_ ?~ [js]
384375 go js@(String s) = mempty
385− & type_ ?~ SwaggerString
376+ & type_ .~ SwaggerString
386377 & maxLength ?~ fromIntegral (T.length s)
387378 & minLength ?~ fromIntegral (T.length s)
388379 & pattern ?~ s
389380 & enum_ ?~ [js]
390381 go js@(Number n) = mempty
391− & type_ ?~ SwaggerNumber
382+ & type_ .~ SwaggerNumber
392383 & maximum_ ?~ n
393384 & minimum_ ?~ n
394385 & multipleOf ?~ n
395386 & enum_ ?~ [js]
396387 go js@(Array xs) = mempty
397− & type_ ?~ SwaggerArray
388+ & type_ .~ SwaggerArray
398389 & maxItems ?~ fromIntegral sz
399390 & minItems ?~ fromIntegral sz
400391 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -404,7 +395,7 @@ sketchStrictSchema = go . toJSON
404395 sz = length xs
405396 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
406397 go js@(Object o) = mempty
407− & type_ ?~ SwaggerObject
398+ & type_ .~ SwaggerObject
408399 & required .~ names
409400 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
410401 & maxProperties ?~ fromIntegral (length names)
@@ -414,19 +405,18 @@ sketchStrictSchema = go . toJSON
414405 names = HashMap.keys o
415406
416407class GToSchema (f :: * -> *) where
417− gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
408+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
418409
419410instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
420411 declareNamedSchema _ = do
421412 ref <- declareSchemaRef (Proxy :: Proxy a)
422413 return $ unnamed $ mempty
423− & type_ ?~ SwaggerArray
414+ & type_ .~ SwaggerArray
424415 & items ?~ SwaggerItemsObject ref
425416
426417instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
427418instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
428419instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429−instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
430420instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
431421instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
432422instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -446,8 +436,6 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
446436instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
447437instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
448438
449−instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450−
451439instance ToSchema a => ToSchema (Maybe a) where
452440 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
453441
@@ -470,7 +458,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
470458
471459timeSchema :: T.Text -> Schema
472460timeSchema fmt = mempty
473− & type_ ?~ SwaggerString
461+ & type_ .~ SwaggerString
474462 & format ?~ fmt
475463
476464-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -491,7 +479,7 @@ instance ToSchema ZonedTime where
491479 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
492480
493481instance ToSchema NominalDiffTime where
494− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
482+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
495483
496484-- |
497485-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -503,8 +491,6 @@ instance ToSchema UTCTime where
503491instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
504492instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
505493
506−instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507−
508494#if __GLASGOW_HASKELL__ < 800
509495#else
510496type family ToSchemaByteStringError bs where
@@ -532,8 +518,8 @@ instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
532518 declareObjectMapSchema = do
533519 schema <- declareSchemaRef (Proxy :: Proxy v)
534520 return $ unnamed $ mempty
535− & type_ ?~ SwaggerObject
536− & additionalProperties ?~ AdditionalPropertiesSchema schema
521+ & type_ .~ SwaggerObject
522+ & additionalProperties ?~ schema
537523
538524instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
539525 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
@@ -544,7 +530,7 @@ instance ToSchema a => ToSchema (Map String a) where
544530 declareNamedSchema _ = do
545531 schema <- declareSchemaRef (Proxy :: Proxy a)
546532 return $ unnamed $ mempty
547− & type_ ?~ SwaggerObject
533+ & type_ .~ SwaggerObject
548534 & additionalProperties ?~ schema
549535
550536instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -556,12 +542,6 @@ instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ =
556542
557543#endif
558544
559−instance OVERLAPPING_ ToSchema Object where
560− declareNamedSchema _ = pure $ NamedSchema (Just "Object") $ mempty
561− & type_ ?~ SwaggerObject
562− & description ?~ "Arbitrary JSON object."
563− & additionalProperties ?~ AdditionalPropertiesAllowed True
564−
565545instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
566546instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
567547instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -575,13 +555,6 @@ instance ToSchema a => ToSchema (Set a) where
575555
576556instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
577557
578−-- | @since 2.2.1
579−instance ToSchema a => ToSchema (NonEmpty a) where
580− declareNamedSchema _ = do
581− schema <- declareSchema (Proxy :: Proxy [a])
582− return $ unnamed $ schema
583− & minItems .~ Just 1
584−
585558instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
586559instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
587560
@@ -591,141 +564,61 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
591564instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
592565instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
593566
594−instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595−
596567-- | Default schema for @'Bounded'@, @'Integral'@ types.
597568--
598569-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
599570-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
600−toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
571+toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
601572toSchemaBoundedIntegral _ = mempty
602− & type_ ?~ SwaggerInteger
573+ & type_ .~ SwaggerInteger
603574 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
604575 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
605576
606577-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
607−genericToNamedSchemaBoundedIntegral :: forall a d f.
578+genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
608579 ( Bounded a, Integral a
609580 , Generic a, Rep a ~ D1 d f, Datatype d)
610− => SchemaOptions -> Proxy a -> NamedSchema
581+ => SchemaOptions -> proxy a -> NamedSchema
611582genericToNamedSchemaBoundedIntegral opts proxy
612− = genericNameSchema opts proxy (toSchemaBoundedIntegral proxy)
613−
614−-- | Declare a named schema for a @newtype@ wrapper.
615−genericDeclareNamedSchemaNewtype :: forall a d c s i inner.
616− (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner))))
617− => SchemaOptions -- ^ How to derive the name.
618− -> (Proxy inner -> Declare (Definitions Schema) Schema) -- ^ How to create a schema for the wrapped type.
619− -> Proxy a
620− -> Declare (Definitions Schema) NamedSchema
621−genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$> f (Proxy :: Proxy inner)
622−
623−-- | Declare 'Schema' for a mapping with 'Bounded' 'Enum' keys.
624−-- This makes a much more useful schema when there aren't many options for key values.
625−--
626−-- >>> data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
627−-- >>> instance ToJSON ButtonState
628−-- >>> instance ToSchema ButtonState
629−-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
630−-- >>> type ImageUrl = T.Text
631−-- >>> encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
632−-- "{\"properties\":{\"Neutral\":{\"type\":\"string\"},\"Focus\":{\"type\":\"string\"},\"Active\":{\"type\":\"string\"},\"Hover\":{\"type\":\"string\"},\"Disabled\":{\"type\":\"string\"}},\"type\":\"object\"}"
633−--
634−-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
635−-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
636−declareSchemaBoundedEnumKeyMapping :: forall map key value.
637− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
638− => Proxy (map key value) -> Declare (Definitions Schema) Schema
639−declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
640− ToJSONKeyText keyToText _ -> objectSchema keyToText
641− ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
642− where
643− objectSchema keyToText = do
644− valueRef <- declareSchemaRef (Proxy :: Proxy value)
645− let allKeys = [minBound..maxBound :: key]
646− mkPair k = (keyToText k, valueRef)
647− return $ mempty
648− & type_ ?~ SwaggerObject
649− & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
650−
651−-- | A 'Schema' for a mapping with 'Bounded' 'Enum' keys.
652−-- This makes a much more useful schema when there aren't many options for key values.
653−--
654−-- >>> data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
655−-- >>> instance ToJSON ButtonState
656−-- >>> instance ToSchema ButtonState
657−-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
658−-- >>> type ImageUrl = T.Text
659−-- >>> encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
660−-- "{\"properties\":{\"Neutral\":{\"type\":\"string\"},\"Focus\":{\"type\":\"string\"},\"Active\":{\"type\":\"string\"},\"Hover\":{\"type\":\"string\"},\"Disabled\":{\"type\":\"string\"}},\"type\":\"object\"}"
661−--
662−-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
663−-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
664−toSchemaBoundedEnumKeyMapping :: forall map key value.
665− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
666− => Proxy (map key value) -> Schema
667−toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
583+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
668584
669585-- | A configurable generic @'Schema'@ creator.
670−genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
671− SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
672−genericDeclareSchema = genericDeclareSchemaUnrestricted
586+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
587+genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
673588
674589-- | A configurable generic @'NamedSchema'@ creator.
675590-- This function applied to @'defaultSchemaOptions'@
676591-- is used as the default for @'declareNamedSchema'@
677592-- when the type is an instance of @'Generic'@.
678−genericDeclareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
679− SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
680−genericDeclareNamedSchema = genericDeclareNamedSchemaUnrestricted
681−
682−-- | A configurable generic @'Schema'@ creator.
683−--
684−-- Unlike 'genericDeclareSchema' also works for mixed sum types.
685−-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
686−genericDeclareSchemaUnrestricted :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
687−genericDeclareSchemaUnrestricted opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchemaUnrestricted opts proxy
688−
689−-- | A configurable generic @'NamedSchema'@ creator.
690−--
691−-- Unlike 'genericDeclareNamedSchema' also works for mixed sum types.
692−-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
693−genericDeclareNamedSchemaUnrestricted :: forall a. (Generic a, GToSchema (Rep a)) =>
694− SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
695−genericDeclareNamedSchemaUnrestricted opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
696−
697−-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
698−genericNameSchema :: forall a d f.
699− (Generic a, Rep a ~ D1 d f, Datatype d)
700− => SchemaOptions -> Proxy a -> Schema -> NamedSchema
701−genericNameSchema opts _ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d))
593+genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
594+genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
702595
703−gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704−gdatatypeSchemaName opts _ = case orig of
596+gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
597+gdatatypeSchemaName opts _ = case name of
705598 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
706599 _ -> Nothing
707600 where
708− orig = datatypeName (Proxy3 :: Proxy3 d f a)
709− name = datatypeNameModifier opts orig
601+ name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
710602
711603-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
712−paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) =>
713− SchemaOptions -> Proxy a -> NamedSchema
714−paramSchemaToNamedSchema opts proxy = genericNameSchema opts proxy (paramSchemaToSchema proxy)
604+paramSchemaToNamedSchema :: forall a d f proxy.
605+ (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
606+ => SchemaOptions -> proxy a -> NamedSchema
607+paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
715608
716609-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
717−paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718−paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
610+paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
611+paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
719612
720613nullarySchema :: Schema
721614nullarySchema = mempty
722− & type_ ?~ SwaggerArray
615+ & type_ .~ SwaggerArray
723616 & items ?~ SwaggerItemsArray []
724617
725−gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
618+gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
726619gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
727620
728−gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
621+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
729622gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
730623
731624instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -745,7 +638,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
745638 gdeclareNamedSchema = gdeclareNamedSumSchema
746639
747640-- | Single field constructor.
748−instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where
641+instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
749642 gdeclareNamedSchema opts _ s
750643 | unwrapUnaryRecords opts = fieldSchema
751644 | otherwise =
@@ -759,7 +652,7 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
759652 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
760653 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
761654
762−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
655+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
763656gdeclareSchemaRef opts proxy = do
764657 case gtoNamedSchema opts proxy of
765658 NamedSchema (Just name) schema -> do
@@ -790,12 +683,10 @@ withFieldSchema opts _ isRequiredField schema = do
790683 return $
791684 if T.null fname
792685 then schema
793− & type_ ?~ SwaggerArray
686+ & type_ .~ SwaggerArray
794687 & items %~ appendItem ref
795− & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796− & minItems %~ Just . maybe 1 (+1) -- increment minItems
797688 else schema
798− & type_ ?~ SwaggerObject
689+ & type_ .~ SwaggerObject
799690 & properties . at fname ?~ ref
800691 & if isRequiredField
801692 then required %~ (++ [fname])
@@ -817,13 +708,10 @@ instance OVERLAPPING_ ToSchema c => GToSchema (K1 i (Maybe c)) where
817708instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
818709 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
819710
820−instance ( GSumToSchema f
821− , GSumToSchema g
822− ) => GToSchema (f :+: g)
823− where
711+instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
824712 gdeclareNamedSchema = gdeclareNamedSumSchema
825713
826−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
714+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
827715gdeclareNamedSumSchema opts proxy s
828716 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
829717 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -832,29 +720,29 @@ gdeclareNamedSumSchema opts proxy s
832720 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
833721
834722 toStringTag schema = mempty
835− & type_ ?~ SwaggerString
723+ & type_ .~ SwaggerString
836724 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
837725
838726type AllNullary = All
839727
840−class GSumToSchema (f :: * -> *) where
841− gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
728+class GSumToSchema f where
729+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
842730
843731instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
844732 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
845733
846−gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
847− Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
734+gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
735+ Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
848736gsumConToSchemaWith ref opts _ schema = schema
849− & type_ ?~ SwaggerObject
737+ & type_ .~ SwaggerObject
850738 & properties . at tag ?~ ref
851739 & maxProperties ?~ 1
852740 & minProperties ?~ 1
853741 where
854742 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
855743
856−gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
857− SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
744+gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
745+ SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
858746gsumConToSchema opts proxy schema = do
859747 ref <- gdeclareSchemaRef opts proxy
860748 return $ gsumConToSchemaWith ref opts proxy schema
@@ -876,7 +764,3 @@ data Proxy2 a b = Proxy2
876764
877765data Proxy3 a b c = Proxy3
878766
879−-- $setup
880−-- >>> import Data.Swagger
881−-- >>> import Data.Aeson (encode)
882−-- >>> import Data.Aeson.Types (toJSONKeyText)