@@ -1,5 +1,4 @@
11{-# LANGUAGE CPP #-}
2−{-# LANGUAGE ConstraintKinds #-}
32{-# LANGUAGE DefaultSignatures #-}
43{-# LANGUAGE FlexibleContexts #-}
54{-# LANGUAGE FlexibleInstances #-}
@@ -14,13 +13,6 @@
1413{-# LANGUAGE TypeFamilies #-}
1514{-# LANGUAGE TypeOperators #-}
1615{-# LANGUAGE TypeSynonymInstances #-}
17−{-# 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
2416#include "overlapping-compat.h"
2517module Data.Swagger.Internal.Schema where
2618
@@ -30,25 +22,27 @@ import Prelude.Compat
3022import Control.Lens
3123import Data.Data.Lens (template)
3224
25+import Control.Applicative
3326import Control.Monad
3427import Control.Monad.Writer
35−import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
28+import Data.Aeson
29+import qualified Data.Aeson.Types as Aeson
3630import Data.Char
3731import Data.Data (Data)
3832import Data.Foldable (traverse_)
33+import Data.Function (on)
3934import Data.HashMap.Strict (HashMap)
4035import qualified Data.HashMap.Strict as HashMap
4136import "unordered-containers" Data.HashSet (HashSet)
4237import qualified "unordered-containers" Data.HashSet as HashSet
38+import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4339import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4440import Data.Int
4541import Data.IntSet (IntSet)
4642import Data.IntMap (IntMap)
47−import Data.List.NonEmpty.Compat (NonEmpty)
4843import Data.Map (Map)
4944import Data.Proxy
5045import Data.Scientific (Scientific)
51−import Data.Fixed (Fixed, HasResolution, Pico)
5246import Data.Set (Set)
5347import qualified Data.Text as T
5448import qualified Data.Text.Lazy as TL
@@ -57,11 +51,8 @@ import qualified Data.Vector as V
5751import qualified Data.Vector.Primitive as VP
5852import qualified Data.Vector.Storable as VS
5953import qualified Data.Vector.Unboxed as VU
60−import Data.Version (Version)
61−import Numeric.Natural.Compat (Natural)
6254import Data.Word
6355import GHC.Generics
64−import qualified Data.UUID.Types as UUID
6556
6657import Data.Swagger.Declare
6758import Data.Swagger.Internal
@@ -69,13 +60,9 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
6960import Data.Swagger.Lens hiding (name, schema)
7061import qualified Data.Swagger.Lens as Swagger
7162import Data.Swagger.SchemaOptions
72−import Data.Swagger.Internal.TypeShape
7363
74−#if __GLASGOW_HASKELL__ < 800
75−#else
76−import qualified Data.ByteString as BS
77−import qualified Data.ByteString.Lazy as BSL
78−import GHC.TypeLits (TypeError, ErrorMessage(..))
64+#ifdef __DOCTEST__
65+import Data.Swagger.Lens (name, schema)
7966#endif
8067
8168unnamed :: Schema -> NamedSchema
@@ -102,16 +89,14 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
10289-- {-\# LANGUAGE OverloadedLists \#-} -- allows to write 'Map' and 'HashMap' as lists
10390--
10491-- import Control.Lens
105−-- import Data.Proxy
106−-- import Data.Swagger
10792--
10893-- data Coord = Coord { x :: Double, y :: Double }
10994--
11095-- instance ToSchema Coord where
111−-- declareNamedSchema _ = do
96+-- declareNamedSchema = do
11297-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
113−-- return $ NamedSchema (Just \"Coord\") $ mempty
114−-- & type_ ?~ SwaggerObject
98+-- return $ NamedSchema (Just \"Coord\")) $ mempty
99+-- & type_ .~ SwaggerObject
115100-- & properties .~
116101-- [ (\"x\", doubleSchema)
117102-- , (\"y\", doubleSchema)
@@ -142,17 +127,12 @@ class ToSchema a where
142127 -- together with all used definitions.
143128 -- Note that the schema itself is included in definitions
144129 -- 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
130+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
131+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
148132 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149133
150−instance ToSchema TimeOfDay where
151− declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152− & example ?~ toJSON (TimeOfDay 12 33 15)
153−
154134-- | Convert a type into a schema and declare all used schema definitions.
155−declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
135+declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
156136declareSchema = fmap _namedSchemaSchema . declareNamedSchema
157137
158138-- | Convert a type into an optionally named schema.
@@ -166,7 +146,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
166146-- Just "Day"
167147-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
168148-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
169−toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
149+toNamedSchema :: ToSchema a => proxy a -> NamedSchema
170150toNamedSchema = undeclare . declareNamedSchema
171151
172152-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -176,7 +156,7 @@ toNamedSchema = undeclare . declareNamedSchema
176156--
177157-- >>> schemaName (Proxy :: Proxy UTCTime)
178158-- Just "UTCTime"
179−schemaName :: ToSchema a => Proxy a -> Maybe T.Text
159+schemaName :: ToSchema a => proxy a -> Maybe T.Text
180160schemaName = _namedSchemaName . toNamedSchema
181161
182162-- | Convert a type into a schema.
@@ -186,7 +166,7 @@ schemaName = _namedSchemaName . toNamedSchema
186166--
187167-- >>> encode $ toSchema (Proxy :: Proxy [Day])
188168-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
189−toSchema :: ToSchema a => Proxy a -> Schema
169+toSchema :: ToSchema a => proxy a -> Schema
190170toSchema = _namedSchemaSchema . toNamedSchema
191171
192172-- | Convert a type into a referenced schema if possible.
@@ -197,7 +177,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
197177--
198178-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
199179-- "{\"$ref\":\"#/definitions/Day\"}"
200−toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
180+toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
201181toSchemaRef = undeclare . declareSchemaRef
202182
203183-- | Convert a type into a referenced schema if possible
@@ -207,7 +187,7 @@ toSchemaRef = undeclare . declareSchemaRef
207187-- Schema definitions are typically declared for every referenced schema.
208188-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
209189-- will be declared (regardless of whether it is recusive or not).
210−declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
190+declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
211191declareSchemaRef proxy = do
212192 case toNamedSchema proxy of
213193 NamedSchema (Just name) schema -> do
@@ -269,7 +249,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
269249--
270250-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
271251-- when inlining recursive schemas.
272−toInlinedSchema :: ToSchema a => Proxy a -> Schema
252+toInlinedSchema :: ToSchema a => proxy a -> Schema
273253toInlinedSchema proxy = inlineAllSchemas defs schema
274254 where
275255 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -298,20 +278,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
298278-- | Default schema for binary data (any sequence of octets).
299279binarySchema :: Schema
300280binarySchema = mempty
301− & type_ ?~ SwaggerString
281+ & type_ .~ SwaggerString
302282 & format ?~ "binary"
303283
304284-- | Default schema for binary data (base64 encoded).
305285byteSchema :: Schema
306286byteSchema = mempty
307− & type_ ?~ SwaggerString
287+ & type_ .~ SwaggerString
308288 & format ?~ "byte"
309289
310290-- | Default schema for password string.
311291-- @"password"@ format is used to hint UIs the input needs to be obscured.
312292passwordSchema :: Schema
313293passwordSchema = mempty
314− & type_ ?~ SwaggerString
294+ & type_ .~ SwaggerString
315295 & format ?~ "password"
316296
317297-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -337,12 +317,12 @@ sketchSchema = sketch . toJSON
337317 sketch js@(Bool _) = go js
338318 sketch js = go js & example ?~ js
339319
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
320+ go Null = mempty & type_ .~ SwaggerNull
321+ go js@(Bool _) = mempty & type_ .~ SwaggerBoolean
322+ go js@(String s) = mempty & type_ .~ SwaggerString
323+ go js@(Number n) = mempty & type_ .~ SwaggerNumber
324+ go js@(Array xs) = mempty
325+ & type_ .~ SwaggerArray
346326 & items ?~ case ischema of
347327 Just s -> SwaggerItemsObject (Inline s)
348328 _ -> SwaggerItemsArray (map Inline ys)
@@ -351,10 +331,10 @@ sketchSchema = sketch . toJSON
351331 allSame = and ((zipWith (==)) ys (tail ys))
352332
353333 ischema = case ys of
354− (z:_) | allSame -> Just z
355− _ -> Nothing
356− go (Object o) = mempty
357− & type_ ?~ SwaggerObject
334+ (z:zs) | allSame -> Just z
335+ _ -> Nothing
336+ go js@(Object o) = mempty
337+ & type_ .~ SwaggerObject
358338 & required .~ HashMap.keys o
359339 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
360340
@@ -377,24 +357,24 @@ sketchSchema = sketch . toJSON
377357sketchStrictSchema :: ToJSON a => a -> Schema
378358sketchStrictSchema = go . toJSON
379359 where
380− go Null = mempty & type_ ?~ SwaggerNull
360+ go Null = mempty & type_ .~ SwaggerNull
381361 go js@(Bool _) = mempty
382− & type_ ?~ SwaggerBoolean
362+ & type_ .~ SwaggerBoolean
383363 & enum_ ?~ [js]
384364 go js@(String s) = mempty
385− & type_ ?~ SwaggerString
365+ & type_ .~ SwaggerString
386366 & maxLength ?~ fromIntegral (T.length s)
387367 & minLength ?~ fromIntegral (T.length s)
388368 & pattern ?~ s
389369 & enum_ ?~ [js]
390370 go js@(Number n) = mempty
391− & type_ ?~ SwaggerNumber
371+ & type_ .~ SwaggerNumber
392372 & maximum_ ?~ n
393373 & minimum_ ?~ n
394374 & multipleOf ?~ n
395375 & enum_ ?~ [js]
396376 go js@(Array xs) = mempty
397− & type_ ?~ SwaggerArray
377+ & type_ .~ SwaggerArray
398378 & maxItems ?~ fromIntegral sz
399379 & minItems ?~ fromIntegral sz
400380 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -404,7 +384,7 @@ sketchStrictSchema = go . toJSON
404384 sz = length xs
405385 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
406386 go js@(Object o) = mempty
407− & type_ ?~ SwaggerObject
387+ & type_ .~ SwaggerObject
408388 & required .~ names
409389 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
410390 & maxProperties ?~ fromIntegral (length names)
@@ -414,19 +394,18 @@ sketchStrictSchema = go . toJSON
414394 names = HashMap.keys o
415395
416396class GToSchema (f :: * -> *) where
417− gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
397+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
418398
419399instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
420400 declareNamedSchema _ = do
421401 ref <- declareSchemaRef (Proxy :: Proxy a)
422402 return $ unnamed $ mempty
423− & type_ ?~ SwaggerArray
403+ & type_ .~ SwaggerArray
424404 & items ?~ SwaggerItemsObject ref
425405
426406instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
427407instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
428408instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429−instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
430409instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
431410instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
432411instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -446,8 +425,6 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
446425instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
447426instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
448427
449−instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450−
451428instance ToSchema a => ToSchema (Maybe a) where
452429 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
453430
@@ -456,11 +433,6 @@ instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
456433instance ToSchema () where
457434 declareNamedSchema _ = pure (NamedSchema Nothing nullarySchema)
458435
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−
464436instance (ToSchema a, ToSchema b) => ToSchema (a, b)
465437instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
466438instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
@@ -470,7 +442,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
470442
471443timeSchema :: T.Text -> Schema
472444timeSchema fmt = mempty
473− & type_ ?~ SwaggerString
445+ & type_ .~ SwaggerString
474446 & format ?~ fmt
475447
476448-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -491,7 +463,7 @@ instance ToSchema ZonedTime where
491463 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
492464
493465instance ToSchema NominalDiffTime where
494− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
466+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
495467
496468-- |
497469-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -503,20 +475,6 @@ instance ToSchema UTCTime where
503475instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
504476instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
505477
506−instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507−
508−#if __GLASGOW_HASKELL__ < 800
509−#else
510−type family ToSchemaByteStringError bs where
511− ToSchemaByteStringError bs = TypeError
512− ( Text "Impossible to have an instance " :<>: ShowType (ToSchema bs) :<>: Text "."
513− :$$: Text "Please, use a newtype wrapper around " :<>: ShowType bs :<>: Text " instead."
514− :$$: Text "Consider using byteSchema or binarySchema templates." )
515−
516−instance ToSchemaByteStringError BS.ByteString => ToSchema BS.ByteString where declareNamedSchema = error "impossible"
517−instance ToSchemaByteStringError BSL.ByteString => ToSchema BSL.ByteString where declareNamedSchema = error "impossible"
518−#endif
519−
520478instance ToSchema IntSet where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set Int))
521479
522480-- | NOTE: This schema does not account for the uniqueness of keys.
@@ -524,27 +482,12 @@ instance ToSchema a => ToSchema (IntMap a) where
524482 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
525483
526484#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
542485
543486instance ToSchema a => ToSchema (Map String a) where
544487 declareNamedSchema _ = do
545488 schema <- declareSchemaRef (Proxy :: Proxy a)
546489 return $ unnamed $ mempty
547− & type_ ?~ SwaggerObject
490+ & type_ .~ SwaggerObject
548491 & additionalProperties ?~ schema
549492
550493instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -554,13 +497,23 @@ instance ToSchema a => ToSchema (HashMap String a) where declareNamedSchema _ =
554497instance ToSchema a => ToSchema (HashMap T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
555498instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
556499
557−#endif
500+#else
558501
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
502+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
503+ declareNamedSchema _ = case toJSONKey :: ToJSONKeyFunction v of
504+ ToJSONKeyText _ _ -> declareObjectMapSchema
505+ ToJSONKeyValue _ _ -> declareNamedSchema (Proxy :: Proxy [(k, v)])
506+ where
507+ declareObjectMapSchema = do
508+ schema <- declareSchemaRef (Proxy :: Proxy v)
509+ return $ unnamed $ mempty
510+ & type_ .~ SwaggerObject
511+ & additionalProperties ?~ schema
512+
513+instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
514+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
515+
516+#endif
564517
565518instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
566519instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -575,13 +528,6 @@ instance ToSchema a => ToSchema (Set a) where
575528
576529instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
577530
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−
585531instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
586532instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
587533
@@ -591,141 +537,61 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
591537instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
592538instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
593539
594−instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595−
596540-- | Default schema for @'Bounded'@, @'Integral'@ types.
597541--
598542-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
599543-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
600−toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
544+toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
601545toSchemaBoundedIntegral _ = mempty
602− & type_ ?~ SwaggerInteger
546+ & type_ .~ SwaggerInteger
603547 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
604548 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
605549
606550-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
607−genericToNamedSchemaBoundedIntegral :: forall a d f.
551+genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
608552 ( Bounded a, Integral a
609553 , Generic a, Rep a ~ D1 d f, Datatype d)
610− => SchemaOptions -> Proxy a -> NamedSchema
554+ => SchemaOptions -> proxy a -> NamedSchema
611555genericToNamedSchemaBoundedIntegral 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
556+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
668557
669558-- | 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
559+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
560+genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
673561
674562-- | A configurable generic @'NamedSchema'@ creator.
675563-- This function applied to @'defaultSchemaOptions'@
676564-- is used as the default for @'declareNamedSchema'@
677565-- 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))
566+genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
567+genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
702568
703−gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704−gdatatypeSchemaName opts _ = case orig of
569+gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
570+gdatatypeSchemaName opts _ = case name of
705571 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
706572 _ -> Nothing
707573 where
708− orig = datatypeName (Proxy3 :: Proxy3 d f a)
709− name = datatypeNameModifier opts orig
574+ name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
710575
711576-- | 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)
577+paramSchemaToNamedSchema :: forall a d f proxy.
578+ (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
579+ => SchemaOptions -> proxy a -> NamedSchema
580+paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
715581
716582-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
717−paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718−paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
583+paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
584+paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
719585
720586nullarySchema :: Schema
721587nullarySchema = mempty
722− & type_ ?~ SwaggerArray
588+ & type_ .~ SwaggerArray
723589 & items ?~ SwaggerItemsArray []
724590
725−gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
591+gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
726592gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
727593
728−gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
594+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
729595gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
730596
731597instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -745,7 +611,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
745611 gdeclareNamedSchema = gdeclareNamedSumSchema
746612
747613-- | Single field constructor.
748−instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where
614+instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
749615 gdeclareNamedSchema opts _ s
750616 | unwrapUnaryRecords opts = fieldSchema
751617 | otherwise =
@@ -759,7 +625,7 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
759625 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
760626 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
761627
762−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
628+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
763629gdeclareSchemaRef opts proxy = do
764630 case gtoNamedSchema opts proxy of
765631 NamedSchema (Just name) schema -> do
@@ -778,7 +644,7 @@ gdeclareSchemaRef opts proxy = do
778644 return $ Ref (Reference name)
779645 _ -> Inline <$> gdeclareSchema opts proxy
780646
781−appendItem :: Referenced Schema -> Maybe (SwaggerItems 'SwaggerKindSchema) -> Maybe (SwaggerItems 'SwaggerKindSchema)
647+appendItem :: Referenced Schema -> Maybe (SwaggerItems SwaggerKindSchema) -> Maybe (SwaggerItems SwaggerKindSchema)
782648appendItem x Nothing = Just (SwaggerItemsArray [x])
783649appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (xs ++ [x]))
784650appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
@@ -790,12 +656,10 @@ withFieldSchema opts _ isRequiredField schema = do
790656 return $
791657 if T.null fname
792658 then schema
793− & type_ ?~ SwaggerArray
659+ & type_ .~ SwaggerArray
794660 & items %~ appendItem ref
795− & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796− & minItems %~ Just . maybe 1 (+1) -- increment minItems
797661 else schema
798− & type_ ?~ SwaggerObject
662+ & type_ .~ SwaggerObject
799663 & properties . at fname ?~ ref
800664 & if isRequiredField
801665 then required %~ (++ [fname])
@@ -817,13 +681,10 @@ instance OVERLAPPING_ ToSchema c => GToSchema (K1 i (Maybe c)) where
817681instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
818682 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
819683
820−instance ( GSumToSchema f
821− , GSumToSchema g
822− ) => GToSchema (f :+: g)
823− where
684+instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
824685 gdeclareNamedSchema = gdeclareNamedSumSchema
825686
826−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
687+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
827688gdeclareNamedSumSchema opts proxy s
828689 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
829690 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -832,29 +693,29 @@ gdeclareNamedSumSchema opts proxy s
832693 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
833694
834695 toStringTag schema = mempty
835− & type_ ?~ SwaggerString
696+ & type_ .~ SwaggerString
836697 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
837698
838699type AllNullary = All
839700
840−class GSumToSchema (f :: * -> *) where
841− gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
701+class GSumToSchema f where
702+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
842703
843704instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
844705 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
845706
846−gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
847− Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
707+gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
708+ Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
848709gsumConToSchemaWith ref opts _ schema = schema
849− & type_ ?~ SwaggerObject
710+ & type_ .~ SwaggerObject
850711 & properties . at tag ?~ ref
851712 & maxProperties ?~ 1
852713 & minProperties ?~ 1
853714 where
854715 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
855716
856−gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
857− SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
717+gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
718+ SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
858719gsumConToSchema opts proxy schema = do
859720 ref <- gdeclareSchemaRef opts proxy
860721 return $ gsumConToSchemaWith ref opts proxy schema
@@ -876,7 +737,3 @@ data Proxy2 a b = Proxy2
876737
877738data Proxy3 a b c = Proxy3
878739
879−-- $setup
880−-- >>> import Data.Swagger
881−-- >>> import Data.Aeson (encode)
882−-- >>> import Data.Aeson.Types (toJSONKeyText)