@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ConstraintKinds #-}
23{-# LANGUAGE DefaultSignatures #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
@@ -17,6 +18,8 @@
1718#if __GLASGOW_HASKELL__ >= 800
1819-- Few generics related redundant constraints
1920{-# OPTIONS_GHC -Wno-redundant-constraints #-}
21+-- For TypeErrors
22+{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
2023#endif
2124#include "overlapping-compat.h"
2225module Data.Swagger.Internal.Schema where
@@ -29,7 +32,7 @@ import Data.Data.Lens (template)
2932
3033import Control.Monad
3134import Control.Monad.Writer
32−import Data.Aeson
35+import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
3336import Data.Char
3437import Data.Data (Data)
3538import Data.Foldable (traverse_)
@@ -41,9 +44,11 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4144import Data.Int
4245import Data.IntSet (IntSet)
4346import Data.IntMap (IntMap)
47+import Data.List.NonEmpty.Compat (NonEmpty)
4448import Data.Map (Map)
4549import Data.Proxy
4650import Data.Scientific (Scientific)
51+import Data.Fixed (Fixed, HasResolution, Pico)
4752import Data.Set (Set)
4853import qualified Data.Text as T
4954import qualified Data.Text.Lazy as TL
@@ -52,6 +57,8 @@ import qualified Data.Vector as V
5257import qualified Data.Vector.Primitive as VP
5358import qualified Data.Vector.Storable as VS
5459import qualified Data.Vector.Unboxed as VU
60+import Data.Version (Version)
61+import Numeric.Natural.Compat (Natural)
5562import Data.Word
5663import GHC.Generics
5764import qualified Data.UUID.Types as UUID
@@ -62,10 +69,7 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
6269import Data.Swagger.Lens hiding (name, schema)
6370import qualified Data.Swagger.Lens as Swagger
6471import Data.Swagger.SchemaOptions
65−
66−#ifdef __DOCTEST__
67−import Data.Swagger.Lens (name, schema)
68−#endif
72+import Data.Swagger.Internal.TypeShape
6973
7074#if __GLASGOW_HASKELL__ < 800
7175#else
@@ -98,14 +102,16 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
98102-- {-\# LANGUAGE OverloadedLists \#-} -- allows to write 'Map' and 'HashMap' as lists
99103--
100104-- import Control.Lens
105+-- import Data.Proxy
106+-- import Data.Swagger
101107--
102108-- data Coord = Coord { x :: Double, y :: Double }
103109--
104110-- instance ToSchema Coord where
105−-- declareNamedSchema = do
111+-- declareNamedSchema _ = do
106112-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
107−-- return $ NamedSchema (Just \"Coord\")) $ mempty
108−-- & type_ .~ SwaggerObject
113+-- return $ NamedSchema (Just \"Coord\") $ mempty
114+-- & type_ ?~ SwaggerObject
109115-- & properties .~
110116-- [ (\"x\", doubleSchema)
111117-- , (\"y\", doubleSchema)
@@ -136,12 +142,17 @@ class ToSchema a where
136142 -- together with all used definitions.
137143 -- Note that the schema itself is included in definitions
138144 -- only if it is recursive (and thus needs its definition in scope).
139− declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
140− default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
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
141148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
142149
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
153+
143154-- | Convert a type into a schema and declare all used schema definitions.
144−declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
155+declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
145156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
146157
147158-- | Convert a type into an optionally named schema.
@@ -155,7 +166,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
155166-- Just "Day"
156167-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
157168-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
158−toNamedSchema :: ToSchema a => proxy a -> NamedSchema
169+toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
159170toNamedSchema = undeclare . declareNamedSchema
160171
161172-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -165,7 +176,7 @@ toNamedSchema = undeclare . declareNamedSchema
165176--
166177-- >>> schemaName (Proxy :: Proxy UTCTime)
167178-- Just "UTCTime"
168−schemaName :: ToSchema a => proxy a -> Maybe T.Text
179+schemaName :: ToSchema a => Proxy a -> Maybe T.Text
169180schemaName = _namedSchemaName . toNamedSchema
170181
171182-- | Convert a type into a schema.
@@ -175,7 +186,7 @@ schemaName = _namedSchemaName . toNamedSchema
175186--
176187-- >>> encode $ toSchema (Proxy :: Proxy [Day])
177188-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
178−toSchema :: ToSchema a => proxy a -> Schema
189+toSchema :: ToSchema a => Proxy a -> Schema
179190toSchema = _namedSchemaSchema . toNamedSchema
180191
181192-- | Convert a type into a referenced schema if possible.
@@ -186,7 +197,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
186197--
187198-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
188199-- "{\"$ref\":\"#/definitions/Day\"}"
189−toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
200+toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
190201toSchemaRef = undeclare . declareSchemaRef
191202
192203-- | Convert a type into a referenced schema if possible
@@ -196,7 +207,7 @@ toSchemaRef = undeclare . declareSchemaRef
196207-- Schema definitions are typically declared for every referenced schema.
197208-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
198209-- will be declared (regardless of whether it is recusive or not).
199−declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
210+declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
200211declareSchemaRef proxy = do
201212 case toNamedSchema proxy of
202213 NamedSchema (Just name) schema -> do
@@ -258,7 +269,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
258269--
259270-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
260271-- when inlining recursive schemas.
261−toInlinedSchema :: ToSchema a => proxy a -> Schema
272+toInlinedSchema :: ToSchema a => Proxy a -> Schema
262273toInlinedSchema proxy = inlineAllSchemas defs schema
263274 where
264275 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -287,20 +298,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
287298-- | Default schema for binary data (any sequence of octets).
288299binarySchema :: Schema
289300binarySchema = mempty
290− & type_ .~ SwaggerString
301+ & type_ ?~ SwaggerString
291302 & format ?~ "binary"
292303
293304-- | Default schema for binary data (base64 encoded).
294305byteSchema :: Schema
295306byteSchema = mempty
296− & type_ .~ SwaggerString
307+ & type_ ?~ SwaggerString
297308 & format ?~ "byte"
298309
299310-- | Default schema for password string.
300311-- @"password"@ format is used to hint UIs the input needs to be obscured.
301312passwordSchema :: Schema
302313passwordSchema = mempty
303− & type_ .~ SwaggerString
314+ & type_ ?~ SwaggerString
304315 & format ?~ "password"
305316
306317-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -326,12 +337,12 @@ sketchSchema = sketch . toJSON
326337 sketch js@(Bool _) = go js
327338 sketch js = go js & example ?~ js
328339
329− go Null = mempty & type_ .~ SwaggerNull
330− go (Bool _) = mempty & type_ .~ SwaggerBoolean
331− go (String _) = mempty & type_ .~ SwaggerString
332− go (Number _) = mempty & type_ .~ SwaggerNumber
340+ go Null = mempty & type_ ?~ SwaggerNull
341+ go (Bool _) = mempty & type_ ?~ SwaggerBoolean
342+ go (String _) = mempty & type_ ?~ SwaggerString
343+ go (Number _) = mempty & type_ ?~ SwaggerNumber
333344 go (Array xs) = mempty
334− & type_ .~ SwaggerArray
345+ & type_ ?~ SwaggerArray
335346 & items ?~ case ischema of
336347 Just s -> SwaggerItemsObject (Inline s)
337348 _ -> SwaggerItemsArray (map Inline ys)
@@ -343,7 +354,7 @@ sketchSchema = sketch . toJSON
343354 (z:_) | allSame -> Just z
344355 _ -> Nothing
345356 go (Object o) = mempty
346− & type_ .~ SwaggerObject
357+ & type_ ?~ SwaggerObject
347358 & required .~ HashMap.keys o
348359 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
349360
@@ -366,24 +377,24 @@ sketchSchema = sketch . toJSON
366377sketchStrictSchema :: ToJSON a => a -> Schema
367378sketchStrictSchema = go . toJSON
368379 where
369− go Null = mempty & type_ .~ SwaggerNull
380+ go Null = mempty & type_ ?~ SwaggerNull
370381 go js@(Bool _) = mempty
371− & type_ .~ SwaggerBoolean
382+ & type_ ?~ SwaggerBoolean
372383 & enum_ ?~ [js]
373384 go js@(String s) = mempty
374− & type_ .~ SwaggerString
385+ & type_ ?~ SwaggerString
375386 & maxLength ?~ fromIntegral (T.length s)
376387 & minLength ?~ fromIntegral (T.length s)
377388 & pattern ?~ s
378389 & enum_ ?~ [js]
379390 go js@(Number n) = mempty
380− & type_ .~ SwaggerNumber
391+ & type_ ?~ SwaggerNumber
381392 & maximum_ ?~ n
382393 & minimum_ ?~ n
383394 & multipleOf ?~ n
384395 & enum_ ?~ [js]
385396 go js@(Array xs) = mempty
386− & type_ .~ SwaggerArray
397+ & type_ ?~ SwaggerArray
387398 & maxItems ?~ fromIntegral sz
388399 & minItems ?~ fromIntegral sz
389400 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -393,7 +404,7 @@ sketchStrictSchema = go . toJSON
393404 sz = length xs
394405 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
395406 go js@(Object o) = mempty
396− & type_ .~ SwaggerObject
407+ & type_ ?~ SwaggerObject
397408 & required .~ names
398409 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
399410 & maxProperties ?~ fromIntegral (length names)
@@ -403,18 +414,19 @@ sketchStrictSchema = go . toJSON
403414 names = HashMap.keys o
404415
405416class GToSchema (f :: * -> *) where
406− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
417+ gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
407418
408419instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
409420 declareNamedSchema _ = do
410421 ref <- declareSchemaRef (Proxy :: Proxy a)
411422 return $ unnamed $ mempty
412− & type_ .~ SwaggerArray
423+ & type_ ?~ SwaggerArray
413424 & items ?~ SwaggerItemsObject ref
414425
415426instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
416427instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
417428instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429+instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
418430instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
419431instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
420432instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -434,6 +446,8 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
434446instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
435447instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
436448
449+instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450+
437451instance ToSchema a => ToSchema (Maybe a) where
438452 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
439453
@@ -456,7 +470,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
456470
457471timeSchema :: T.Text -> Schema
458472timeSchema fmt = mempty
459− & type_ .~ SwaggerString
473+ & type_ ?~ SwaggerString
460474 & format ?~ fmt
461475
462476-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -477,7 +491,7 @@ instance ToSchema ZonedTime where
477491 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
478492
479493instance ToSchema NominalDiffTime where
480− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
494+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
481495
482496-- |
483497-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -489,6 +503,8 @@ instance ToSchema UTCTime where
489503instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
490504instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
491505
506+instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507+
492508#if __GLASGOW_HASKELL__ < 800
493509#else
494510type family ToSchemaByteStringError bs where
@@ -516,8 +532,8 @@ instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
516532 declareObjectMapSchema = do
517533 schema <- declareSchemaRef (Proxy :: Proxy v)
518534 return $ unnamed $ mempty
519− & type_ .~ SwaggerObject
520− & additionalProperties ?~ schema
535+ & type_ ?~ SwaggerObject
536+ & additionalProperties ?~ AdditionalPropertiesSchema schema
521537
522538instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
523539 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
@@ -528,7 +544,7 @@ instance ToSchema a => ToSchema (Map String a) where
528544 declareNamedSchema _ = do
529545 schema <- declareSchemaRef (Proxy :: Proxy a)
530546 return $ unnamed $ mempty
531− & type_ .~ SwaggerObject
547+ & type_ ?~ SwaggerObject
532548 & additionalProperties ?~ schema
533549
534550instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -540,6 +556,12 @@ instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ =
540556
541557#endif
542558
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+
543565instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
544566instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
545567instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -553,6 +575,13 @@ instance ToSchema a => ToSchema (Set a) where
553575
554576instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
555577
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+
556585instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
557586instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
558587
@@ -562,61 +591,141 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
562591instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
563592instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
564593
594+instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595+
565596-- | Default schema for @'Bounded'@, @'Integral'@ types.
566597--
567598-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
568599-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
569−toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
600+toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
570601toSchemaBoundedIntegral _ = mempty
571− & type_ .~ SwaggerInteger
602+ & type_ ?~ SwaggerInteger
572603 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
573604 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
574605
575606-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
576−genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
607+genericToNamedSchemaBoundedIntegral :: forall a d f.
577608 ( Bounded a, Integral a
578609 , Generic a, Rep a ~ D1 d f, Datatype d)
579− => SchemaOptions -> proxy a -> NamedSchema
610+ => SchemaOptions -> Proxy a -> NamedSchema
580611genericToNamedSchemaBoundedIntegral opts proxy
581− = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral 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
582668
583669-- | A configurable generic @'Schema'@ creator.
584−genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
585−genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
670+genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
671+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
672+genericDeclareSchema = genericDeclareSchemaUnrestricted
586673
587674-- | A configurable generic @'NamedSchema'@ creator.
588675-- This function applied to @'defaultSchemaOptions'@
589676-- is used as the default for @'declareNamedSchema'@
590677-- when the type is an instance of @'Generic'@.
591−genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
592−genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
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))
593702
594−gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
595−gdatatypeSchemaName opts _ = case name of
703+gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704+gdatatypeSchemaName opts _ = case orig of
596705 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
597706 _ -> Nothing
598707 where
599− name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
708+ orig = datatypeName (Proxy3 :: Proxy3 d f a)
709+ name = datatypeNameModifier opts orig
600710
601711-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
602−paramSchemaToNamedSchema :: forall a d f proxy.
603− (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
604− => SchemaOptions -> proxy a -> NamedSchema
605−paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
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)
606715
607716-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
608−paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
609−paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
717+paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718+paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
610719
611720nullarySchema :: Schema
612721nullarySchema = mempty
613− & type_ .~ SwaggerArray
722+ & type_ ?~ SwaggerArray
614723 & items ?~ SwaggerItemsArray []
615724
616−gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
725+gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
617726gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
618727
619−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
728+gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
620729gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
621730
622731instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -636,7 +745,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
636745 gdeclareNamedSchema = gdeclareNamedSumSchema
637746
638747-- | Single field constructor.
639−instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
748+instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where
640749 gdeclareNamedSchema opts _ s
641750 | unwrapUnaryRecords opts = fieldSchema
642751 | otherwise =
@@ -650,7 +759,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
650759 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
651760 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
652761
653−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
762+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
654763gdeclareSchemaRef opts proxy = do
655764 case gtoNamedSchema opts proxy of
656765 NamedSchema (Just name) schema -> do
@@ -681,10 +790,12 @@ withFieldSchema opts _ isRequiredField schema = do
681790 return $
682791 if T.null fname
683792 then schema
684− & type_ .~ SwaggerArray
793+ & type_ ?~ SwaggerArray
685794 & items %~ appendItem ref
795+ & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796+ & minItems %~ Just . maybe 1 (+1) -- increment minItems
686797 else schema
687− & type_ .~ SwaggerObject
798+ & type_ ?~ SwaggerObject
688799 & properties . at fname ?~ ref
689800 & if isRequiredField
690801 then required %~ (++ [fname])
@@ -706,10 +817,13 @@ instance OVERLAPPING_ ToSchema c => GToSchema (K1 i (Maybe c)) where
706817instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
707818 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
708819
709−instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
820+instance ( GSumToSchema f
821+ , GSumToSchema g
822+ ) => GToSchema (f :+: g)
823+ where
710824 gdeclareNamedSchema = gdeclareNamedSumSchema
711825
712−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
826+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
713827gdeclareNamedSumSchema opts proxy s
714828 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
715829 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -718,29 +832,29 @@ gdeclareNamedSumSchema opts proxy s
718832 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
719833
720834 toStringTag schema = mempty
721− & type_ .~ SwaggerString
835+ & type_ ?~ SwaggerString
722836 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
723837
724838type AllNullary = All
725839
726−class GSumToSchema f where
727− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
840+class GSumToSchema (f :: * -> *) where
841+ gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
728842
729843instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
730844 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
731845
732−gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
733− Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
846+gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
847+ Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
734848gsumConToSchemaWith ref opts _ schema = schema
735− & type_ .~ SwaggerObject
849+ & type_ ?~ SwaggerObject
736850 & properties . at tag ?~ ref
737851 & maxProperties ?~ 1
738852 & minProperties ?~ 1
739853 where
740854 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
741855
742−gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
743− SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
856+gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
857+ SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
744858gsumConToSchema opts proxy schema = do
745859 ref <- gdeclareSchemaRef opts proxy
746860 return $ gsumConToSchemaWith ref opts proxy schema
@@ -762,3 +876,7 @@ data Proxy2 a b = Proxy2
762876
763877data Proxy3 a b c = Proxy3
764878
879+-- $setup
880+-- >>> import Data.Swagger
881+-- >>> import Data.Aeson (encode)
882+-- >>> import Data.Aeson.Types (toJSONKeyText)