@@ -1,5 +1,4 @@
11{-# LANGUAGE CPP #-}
2−{-# LANGUAGE ConstraintKinds #-}
32{-# LANGUAGE DefaultSignatures #-}
43{-# LANGUAGE FlexibleContexts #-}
54{-# LANGUAGE FlexibleInstances #-}
@@ -15,12 +14,6 @@
1514{-# LANGUAGE TypeOperators #-}
1615{-# LANGUAGE TypeSynonymInstances #-}
1716{-# LANGUAGE UndecidableInstances #-}
18−#if __GLASGOW_HASKELL__ >= 800
19−-- Few generics related redundant constraints
20−{-# OPTIONS_GHC -Wno-redundant-constraints #-}
21−-- For TypeErrors
22−{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
23−#endif
2417#include "overlapping-compat.h"
2518module Data.Swagger.Internal.Schema where
2619
@@ -30,25 +23,27 @@ import Prelude.Compat
3023import Control.Lens
3124import Data.Data.Lens (template)
3225
26+import Control.Applicative
3327import Control.Monad
3428import Control.Monad.Writer
35−import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
29+import Data.Aeson
30+import qualified Data.Aeson.Types as Aeson
3631import Data.Char
3732import Data.Data (Data)
3833import Data.Foldable (traverse_)
34+import Data.Function (on)
3935import Data.HashMap.Strict (HashMap)
4036import qualified Data.HashMap.Strict as HashMap
4137import "unordered-containers" Data.HashSet (HashSet)
4238import qualified "unordered-containers" Data.HashSet as HashSet
39+import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4340import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4441import Data.Int
4542import Data.IntSet (IntSet)
4643import Data.IntMap (IntMap)
47−import Data.List.NonEmpty.Compat (NonEmpty)
4844import Data.Map (Map)
4945import Data.Proxy
5046import Data.Scientific (Scientific)
51−import Data.Fixed (Fixed, HasResolution, Pico)
5247import Data.Set (Set)
5348import qualified Data.Text as T
5449import qualified Data.Text.Lazy as TL
@@ -57,11 +52,8 @@ import qualified Data.Vector as V
5752import qualified Data.Vector.Primitive as VP
5853import qualified Data.Vector.Storable as VS
5954import qualified Data.Vector.Unboxed as VU
60−import Data.Version (Version)
61−import Numeric.Natural.Compat (Natural)
6255import Data.Word
6356import GHC.Generics
64−import qualified Data.UUID.Types as UUID
6557
6658import Data.Swagger.Declare
6759import Data.Swagger.Internal
@@ -69,7 +61,10 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
6961import Data.Swagger.Lens hiding (name, schema)
7062import qualified Data.Swagger.Lens as Swagger
7163import Data.Swagger.SchemaOptions
72−import Data.Swagger.Internal.TypeShape
64+
65+#ifdef __DOCTEST__
66+import Data.Swagger.Lens (name, schema)
67+#endif
7368
7469#if __GLASGOW_HASKELL__ < 800
7570#else
@@ -102,16 +97,14 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
10297-- {-\# LANGUAGE OverloadedLists \#-} -- allows to write 'Map' and 'HashMap' as lists
10398--
10499-- import Control.Lens
105−-- import Data.Proxy
106−-- import Data.Swagger
107100--
108101-- data Coord = Coord { x :: Double, y :: Double }
109102--
110103-- instance ToSchema Coord where
111−-- declareNamedSchema _ = do
104+-- declareNamedSchema = do
112105-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
113−-- return $ NamedSchema (Just \"Coord\") $ mempty
114−-- & type_ ?~ SwaggerObject
106+-- return $ NamedSchema (Just \"Coord\")) $ mempty
107+-- & type_ .~ SwaggerObject
115108-- & properties .~
116109-- [ (\"x\", doubleSchema)
117110-- , (\"y\", doubleSchema)
@@ -142,17 +135,12 @@ class ToSchema a where
142135 -- together with all used definitions.
143136 -- Note that the schema itself is included in definitions
144137 -- 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
138+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
139+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
148140 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149141
150−instance ToSchema TimeOfDay where
151− declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152− & example ?~ toJSON (TimeOfDay 12 33 15)
153−
154142-- | Convert a type into a schema and declare all used schema definitions.
155−declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
143+declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
156144declareSchema = fmap _namedSchemaSchema . declareNamedSchema
157145
158146-- | Convert a type into an optionally named schema.
@@ -166,7 +154,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
166154-- Just "Day"
167155-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
168156-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
169−toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
157+toNamedSchema :: ToSchema a => proxy a -> NamedSchema
170158toNamedSchema = undeclare . declareNamedSchema
171159
172160-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -176,7 +164,7 @@ toNamedSchema = undeclare . declareNamedSchema
176164--
177165-- >>> schemaName (Proxy :: Proxy UTCTime)
178166-- Just "UTCTime"
179−schemaName :: ToSchema a => Proxy a -> Maybe T.Text
167+schemaName :: ToSchema a => proxy a -> Maybe T.Text
180168schemaName = _namedSchemaName . toNamedSchema
181169
182170-- | Convert a type into a schema.
@@ -186,7 +174,7 @@ schemaName = _namedSchemaName . toNamedSchema
186174--
187175-- >>> encode $ toSchema (Proxy :: Proxy [Day])
188176-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
189−toSchema :: ToSchema a => Proxy a -> Schema
177+toSchema :: ToSchema a => proxy a -> Schema
190178toSchema = _namedSchemaSchema . toNamedSchema
191179
192180-- | Convert a type into a referenced schema if possible.
@@ -197,7 +185,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
197185--
198186-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
199187-- "{\"$ref\":\"#/definitions/Day\"}"
200−toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
188+toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
201189toSchemaRef = undeclare . declareSchemaRef
202190
203191-- | Convert a type into a referenced schema if possible
@@ -207,7 +195,7 @@ toSchemaRef = undeclare . declareSchemaRef
207195-- Schema definitions are typically declared for every referenced schema.
208196-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
209197-- will be declared (regardless of whether it is recusive or not).
210−declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
198+declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
211199declareSchemaRef proxy = do
212200 case toNamedSchema proxy of
213201 NamedSchema (Just name) schema -> do
@@ -269,7 +257,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
269257--
270258-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
271259-- when inlining recursive schemas.
272−toInlinedSchema :: ToSchema a => Proxy a -> Schema
260+toInlinedSchema :: ToSchema a => proxy a -> Schema
273261toInlinedSchema proxy = inlineAllSchemas defs schema
274262 where
275263 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -298,20 +286,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
298286-- | Default schema for binary data (any sequence of octets).
299287binarySchema :: Schema
300288binarySchema = mempty
301− & type_ ?~ SwaggerString
289+ & type_ .~ SwaggerString
302290 & format ?~ "binary"
303291
304292-- | Default schema for binary data (base64 encoded).
305293byteSchema :: Schema
306294byteSchema = mempty
307− & type_ ?~ SwaggerString
295+ & type_ .~ SwaggerString
308296 & format ?~ "byte"
309297
310298-- | Default schema for password string.
311299-- @"password"@ format is used to hint UIs the input needs to be obscured.
312300passwordSchema :: Schema
313301passwordSchema = mempty
314− & type_ ?~ SwaggerString
302+ & type_ .~ SwaggerString
315303 & format ?~ "password"
316304
317305-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -337,12 +325,12 @@ sketchSchema = sketch . toJSON
337325 sketch js@(Bool _) = go js
338326 sketch js = go js & example ?~ js
339327
340− go Null = mempty & type_ ?~ SwaggerNull
341− go (Bool _) = mempty & type_ ?~ SwaggerBoolean
342− go (String _) = mempty & type_ ?~ SwaggerString
343− go (Number _) = mempty & type_ ?~ SwaggerNumber
344− go (Array xs) = mempty
345− & type_ ?~ SwaggerArray
328+ go Null = mempty & type_ .~ SwaggerNull
329+ go js@(Bool _) = mempty & type_ .~ SwaggerBoolean
330+ go js@(String s) = mempty & type_ .~ SwaggerString
331+ go js@(Number n) = mempty & type_ .~ SwaggerNumber
332+ go js@(Array xs) = mempty
333+ & type_ .~ SwaggerArray
346334 & items ?~ case ischema of
347335 Just s -> SwaggerItemsObject (Inline s)
348336 _ -> SwaggerItemsArray (map Inline ys)
@@ -351,10 +339,10 @@ sketchSchema = sketch . toJSON
351339 allSame = and ((zipWith (==)) ys (tail ys))
352340
353341 ischema = case ys of
354− (z:_) | allSame -> Just z
355− _ -> Nothing
356− go (Object o) = mempty
357− & type_ ?~ SwaggerObject
342+ (z:zs) | allSame -> Just z
343+ _ -> Nothing
344+ go js@(Object o) = mempty
345+ & type_ .~ SwaggerObject
358346 & required .~ HashMap.keys o
359347 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
360348
@@ -377,24 +365,24 @@ sketchSchema = sketch . toJSON
377365sketchStrictSchema :: ToJSON a => a -> Schema
378366sketchStrictSchema = go . toJSON
379367 where
380− go Null = mempty & type_ ?~ SwaggerNull
368+ go Null = mempty & type_ .~ SwaggerNull
381369 go js@(Bool _) = mempty
382− & type_ ?~ SwaggerBoolean
370+ & type_ .~ SwaggerBoolean
383371 & enum_ ?~ [js]
384372 go js@(String s) = mempty
385− & type_ ?~ SwaggerString
373+ & type_ .~ SwaggerString
386374 & maxLength ?~ fromIntegral (T.length s)
387375 & minLength ?~ fromIntegral (T.length s)
388376 & pattern ?~ s
389377 & enum_ ?~ [js]
390378 go js@(Number n) = mempty
391− & type_ ?~ SwaggerNumber
379+ & type_ .~ SwaggerNumber
392380 & maximum_ ?~ n
393381 & minimum_ ?~ n
394382 & multipleOf ?~ n
395383 & enum_ ?~ [js]
396384 go js@(Array xs) = mempty
397− & type_ ?~ SwaggerArray
385+ & type_ .~ SwaggerArray
398386 & maxItems ?~ fromIntegral sz
399387 & minItems ?~ fromIntegral sz
400388 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -404,7 +392,7 @@ sketchStrictSchema = go . toJSON
404392 sz = length xs
405393 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
406394 go js@(Object o) = mempty
407− & type_ ?~ SwaggerObject
395+ & type_ .~ SwaggerObject
408396 & required .~ names
409397 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
410398 & maxProperties ?~ fromIntegral (length names)
@@ -414,19 +402,18 @@ sketchStrictSchema = go . toJSON
414402 names = HashMap.keys o
415403
416404class GToSchema (f :: * -> *) where
417− gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
405+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
418406
419407instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
420408 declareNamedSchema _ = do
421409 ref <- declareSchemaRef (Proxy :: Proxy a)
422410 return $ unnamed $ mempty
423− & type_ ?~ SwaggerArray
411+ & type_ .~ SwaggerArray
424412 & items ?~ SwaggerItemsObject ref
425413
426414instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
427415instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
428416instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429−instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
430417instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
431418instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
432419instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -446,8 +433,6 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
446433instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
447434instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
448435
449−instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450−
451436instance ToSchema a => ToSchema (Maybe a) where
452437 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
453438
@@ -456,11 +441,6 @@ instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
456441instance ToSchema () where
457442 declareNamedSchema _ = pure (NamedSchema Nothing nullarySchema)
458443
459−-- | For 'ToJSON' instance, see <http://hackage.haskell.org/package/uuid-aeson uuid-aeson> package.
460−instance ToSchema UUID.UUID where
461− declareNamedSchema p = pure $ named "UUID" $ paramSchemaToSchema p
462− & example ?~ toJSON (UUID.toText UUID.nil)
463−
464444instance (ToSchema a, ToSchema b) => ToSchema (a, b)
465445instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
466446instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
@@ -470,7 +450,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
470450
471451timeSchema :: T.Text -> Schema
472452timeSchema fmt = mempty
473− & type_ ?~ SwaggerString
453+ & type_ .~ SwaggerString
474454 & format ?~ fmt
475455
476456-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -491,7 +471,7 @@ instance ToSchema ZonedTime where
491471 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
492472
493473instance ToSchema NominalDiffTime where
494− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
474+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
495475
496476-- |
497477-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -503,8 +483,6 @@ instance ToSchema UTCTime where
503483instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
504484instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
505485
506−instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507−
508486#if __GLASGOW_HASKELL__ < 800
509487#else
510488type family ToSchemaByteStringError bs where
@@ -524,27 +502,12 @@ instance ToSchema a => ToSchema (IntMap a) where
524502 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
525503
526504#if MIN_VERSION_aeson(1,0,0)
527−instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
528− declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction k of
529− ToJSONKeyText _ _ -> declareObjectMapSchema
530− ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
531− where
532− declareObjectMapSchema = do
533− schema <- declareSchemaRef (Proxy :: Proxy v)
534− return $ unnamed $ mempty
535− & type_ ?~ SwaggerObject
536− & additionalProperties ?~ AdditionalPropertiesSchema schema
537−
538−instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
539− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
540−
541−#else
542505
543506instance ToSchema a => ToSchema (Map String a) where
544507 declareNamedSchema _ = do
545508 schema <- declareSchemaRef (Proxy :: Proxy a)
546509 return $ unnamed $ mempty
547− & type_ ?~ SwaggerObject
510+ & type_ .~ SwaggerObject
548511 & additionalProperties ?~ schema
549512
550513instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -554,13 +517,23 @@ instance ToSchema a => ToSchema (HashMap String a) where declareNamedSchema _ =
554517instance ToSchema a => ToSchema (HashMap T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
555518instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
556519
557−#endif
520+#else
521+
522+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
523+ declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction v of
524+ ToJSONKeyText _ _ -> declareObjectMapSchema
525+ ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
526+ where
527+ declareObjectMapSchema = do
528+ schema <- declareSchemaRef (Proxy :: Proxy v)
529+ return $ unnamed $ mempty
530+ & type_ .~ SwaggerObject
531+ & additionalProperties ?~ schema
532+
533+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
534+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
558535
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
536+#endif
564537
565538instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
566539instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -575,13 +548,6 @@ instance ToSchema a => ToSchema (Set a) where
575548
576549instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
577550
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−
585551instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
586552instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
587553
@@ -591,141 +557,61 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
591557instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
592558instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
593559
594−instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595−
596560-- | Default schema for @'Bounded'@, @'Integral'@ types.
597561--
598562-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
599563-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
600−toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
564+toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
601565toSchemaBoundedIntegral _ = mempty
602− & type_ ?~ SwaggerInteger
566+ & type_ .~ SwaggerInteger
603567 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
604568 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
605569
606570-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
607−genericToNamedSchemaBoundedIntegral :: forall a d f.
571+genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
608572 ( Bounded a, Integral a
609573 , Generic a, Rep a ~ D1 d f, Datatype d)
610− => SchemaOptions -> Proxy a -> NamedSchema
574+ => SchemaOptions -> proxy a -> NamedSchema
611575genericToNamedSchemaBoundedIntegral 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
576+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
668577
669578-- | 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
579+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
580+genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
673581
674582-- | A configurable generic @'NamedSchema'@ creator.
675583-- This function applied to @'defaultSchemaOptions'@
676584-- is used as the default for @'declareNamedSchema'@
677585-- 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))
586+genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
587+genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
702588
703−gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704−gdatatypeSchemaName opts _ = case orig of
589+gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
590+gdatatypeSchemaName opts _ = case name of
705591 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
706592 _ -> Nothing
707593 where
708− orig = datatypeName (Proxy3 :: Proxy3 d f a)
709− name = datatypeNameModifier opts orig
594+ name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
710595
711596-- | 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)
597+paramSchemaToNamedSchema :: forall a d f proxy.
598+ (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
599+ => SchemaOptions -> proxy a -> NamedSchema
600+paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
715601
716602-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
717−paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718−paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
603+paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
604+paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
719605
720606nullarySchema :: Schema
721607nullarySchema = mempty
722− & type_ ?~ SwaggerArray
608+ & type_ .~ SwaggerArray
723609 & items ?~ SwaggerItemsArray []
724610
725−gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
611+gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
726612gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
727613
728−gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
614+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
729615gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
730616
731617instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -745,7 +631,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
745631 gdeclareNamedSchema = gdeclareNamedSumSchema
746632
747633-- | Single field constructor.
748−instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where
634+instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
749635 gdeclareNamedSchema opts _ s
750636 | unwrapUnaryRecords opts = fieldSchema
751637 | otherwise =
@@ -759,7 +645,7 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
759645 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
760646 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
761647
762−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
648+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
763649gdeclareSchemaRef opts proxy = do
764650 case gtoNamedSchema opts proxy of
765651 NamedSchema (Just name) schema -> do
@@ -778,7 +664,7 @@ gdeclareSchemaRef opts proxy = do
778664 return $ Ref (Reference name)
779665 _ -> Inline <$> gdeclareSchema opts proxy
780666
781−appendItem :: Referenced Schema -> Maybe (SwaggerItems 'SwaggerKindSchema) -> Maybe (SwaggerItems 'SwaggerKindSchema)
667+appendItem :: Referenced Schema -> Maybe (SwaggerItems SwaggerKindSchema) -> Maybe (SwaggerItems SwaggerKindSchema)
782668appendItem x Nothing = Just (SwaggerItemsArray [x])
783669appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (xs ++ [x]))
784670appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
@@ -790,12 +676,10 @@ withFieldSchema opts _ isRequiredField schema = do
790676 return $
791677 if T.null fname
792678 then schema
793− & type_ ?~ SwaggerArray
679+ & type_ .~ SwaggerArray
794680 & items %~ appendItem ref
795− & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796− & minItems %~ Just . maybe 1 (+1) -- increment minItems
797681 else schema
798− & type_ ?~ SwaggerObject
682+ & type_ .~ SwaggerObject
799683 & properties . at fname ?~ ref
800684 & if isRequiredField
801685 then required %~ (++ [fname])
@@ -817,13 +701,10 @@ instance OVERLAPPING_ ToSchema c => GToSchema (K1 i (Maybe c)) where
817701instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
818702 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
819703
820−instance ( GSumToSchema f
821− , GSumToSchema g
822− ) => GToSchema (f :+: g)
823− where
704+instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
824705 gdeclareNamedSchema = gdeclareNamedSumSchema
825706
826−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
707+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
827708gdeclareNamedSumSchema opts proxy s
828709 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
829710 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -832,29 +713,29 @@ gdeclareNamedSumSchema opts proxy s
832713 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
833714
834715 toStringTag schema = mempty
835− & type_ ?~ SwaggerString
716+ & type_ .~ SwaggerString
836717 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
837718
838719type AllNullary = All
839720
840−class GSumToSchema (f :: * -> *) where
841− gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
721+class GSumToSchema f where
722+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
842723
843724instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
844725 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
845726
846−gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
847− Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
727+gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
728+ Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
848729gsumConToSchemaWith ref opts _ schema = schema
849− & type_ ?~ SwaggerObject
730+ & type_ .~ SwaggerObject
850731 & properties . at tag ?~ ref
851732 & maxProperties ?~ 1
852733 & minProperties ?~ 1
853734 where
854735 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
855736
856−gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
857− SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
737+gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
738+ SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
858739gsumConToSchema opts proxy schema = do
859740 ref <- gdeclareSchemaRef opts proxy
860741 return $ gsumConToSchemaWith ref opts proxy schema
@@ -876,7 +757,3 @@ data Proxy2 a b = Proxy2
876757
877758data Proxy3 a b c = Proxy3
878759
879−-- $setup
880−-- >>> import Data.Swagger
881−-- >>> import Data.Aeson (encode)
882−-- >>> import Data.Aeson.Types (toJSONKeyText)