@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ConstraintKinds #-}
23{-# LANGUAGE DefaultSignatures #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
@@ -14,6 +15,12 @@
1415{-# LANGUAGE TypeOperators #-}
1516{-# LANGUAGE TypeSynonymInstances #-}
1617{-# 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
1724#include "overlapping-compat.h"
1825module Data.Swagger.Internal.Schema where
1926
@@ -23,27 +30,25 @@ import Prelude.Compat
2330import Control.Lens
2431import Data.Data.Lens (template)
2532
26−import Control.Applicative
2733import Control.Monad
2834import Control.Monad.Writer
29−import Data.Aeson
30−import qualified Data.Aeson.Types as Aeson
35+import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
3136import Data.Char
3237import Data.Data (Data)
3338import Data.Foldable (traverse_)
34−import Data.Function (on)
3539import Data.HashMap.Strict (HashMap)
3640import qualified Data.HashMap.Strict as HashMap
3741import "unordered-containers" Data.HashSet (HashSet)
3842import qualified "unordered-containers" Data.HashSet as HashSet
39−import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
4043import 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,8 +57,11 @@ 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
64+import qualified Data.UUID.Types as UUID
5765
5866import Data.Swagger.Declare
5967import Data.Swagger.Internal
@@ -61,10 +69,7 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
6169import Data.Swagger.Lens hiding (name, schema)
6270import qualified Data.Swagger.Lens as Swagger
6371import Data.Swagger.SchemaOptions
64−
65−#ifdef __DOCTEST__
66−import Data.Swagger.Lens (name, schema)
67−#endif
72+import Data.Swagger.Internal.TypeShape
6873
6974#if __GLASGOW_HASKELL__ < 800
7075#else
@@ -97,14 +102,16 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
97102-- {-\# LANGUAGE OverloadedLists \#-} -- allows to write 'Map' and 'HashMap' as lists
98103--
99104-- import Control.Lens
105+-- import Data.Proxy
106+-- import Data.Swagger
100107--
101108-- data Coord = Coord { x :: Double, y :: Double }
102109--
103110-- instance ToSchema Coord where
104−-- declareNamedSchema = do
111+-- declareNamedSchema _ = do
105112-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
106−-- return $ NamedSchema (Just \"Coord\")) $ mempty
107−-- & type_ .~ SwaggerObject
113+-- return $ NamedSchema (Just \"Coord\") $ mempty
114+-- & type_ ?~ SwaggerObject
108115-- & properties .~
109116-- [ (\"x\", doubleSchema)
110117-- , (\"y\", doubleSchema)
@@ -135,12 +142,17 @@ class ToSchema a where
135142 -- together with all used definitions.
136143 -- Note that the schema itself is included in definitions
137144 -- only if it is recursive (and thus needs its definition in scope).
138− declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
139− 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
140148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
141149
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
153+
142154-- | Convert a type into a schema and declare all used schema definitions.
143−declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
155+declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
144156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
145157
146158-- | Convert a type into an optionally named schema.
@@ -154,7 +166,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
154166-- Just "Day"
155167-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
156168-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
157−toNamedSchema :: ToSchema a => proxy a -> NamedSchema
169+toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
158170toNamedSchema = undeclare . declareNamedSchema
159171
160172-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -164,7 +176,7 @@ toNamedSchema = undeclare . declareNamedSchema
164176--
165177-- >>> schemaName (Proxy :: Proxy UTCTime)
166178-- Just "UTCTime"
167−schemaName :: ToSchema a => proxy a -> Maybe T.Text
179+schemaName :: ToSchema a => Proxy a -> Maybe T.Text
168180schemaName = _namedSchemaName . toNamedSchema
169181
170182-- | Convert a type into a schema.
@@ -174,7 +186,7 @@ schemaName = _namedSchemaName . toNamedSchema
174186--
175187-- >>> encode $ toSchema (Proxy :: Proxy [Day])
176188-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
177−toSchema :: ToSchema a => proxy a -> Schema
189+toSchema :: ToSchema a => Proxy a -> Schema
178190toSchema = _namedSchemaSchema . toNamedSchema
179191
180192-- | Convert a type into a referenced schema if possible.
@@ -185,7 +197,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
185197--
186198-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
187199-- "{\"$ref\":\"#/definitions/Day\"}"
188−toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
200+toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
189201toSchemaRef = undeclare . declareSchemaRef
190202
191203-- | Convert a type into a referenced schema if possible
@@ -195,7 +207,7 @@ toSchemaRef = undeclare . declareSchemaRef
195207-- Schema definitions are typically declared for every referenced schema.
196208-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
197209-- will be declared (regardless of whether it is recusive or not).
198−declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
210+declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
199211declareSchemaRef proxy = do
200212 case toNamedSchema proxy of
201213 NamedSchema (Just name) schema -> do
@@ -257,7 +269,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
257269--
258270-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
259271-- when inlining recursive schemas.
260−toInlinedSchema :: ToSchema a => proxy a -> Schema
272+toInlinedSchema :: ToSchema a => Proxy a -> Schema
261273toInlinedSchema proxy = inlineAllSchemas defs schema
262274 where
263275 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -286,20 +298,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
286298-- | Default schema for binary data (any sequence of octets).
287299binarySchema :: Schema
288300binarySchema = mempty
289− & type_ .~ SwaggerString
301+ & type_ ?~ SwaggerString
290302 & format ?~ "binary"
291303
292304-- | Default schema for binary data (base64 encoded).
293305byteSchema :: Schema
294306byteSchema = mempty
295− & type_ .~ SwaggerString
307+ & type_ ?~ SwaggerString
296308 & format ?~ "byte"
297309
298310-- | Default schema for password string.
299311-- @"password"@ format is used to hint UIs the input needs to be obscured.
300312passwordSchema :: Schema
301313passwordSchema = mempty
302− & type_ .~ SwaggerString
314+ & type_ ?~ SwaggerString
303315 & format ?~ "password"
304316
305317-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -325,12 +337,12 @@ sketchSchema = sketch . toJSON
325337 sketch js@(Bool _) = go js
326338 sketch js = go js & example ?~ js
327339
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
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
334346 & items ?~ case ischema of
335347 Just s -> SwaggerItemsObject (Inline s)
336348 _ -> SwaggerItemsArray (map Inline ys)
@@ -339,10 +351,10 @@ sketchSchema = sketch . toJSON
339351 allSame = and ((zipWith (==)) ys (tail ys))
340352
341353 ischema = case ys of
342− (z:zs) | allSame -> Just z
343− _ -> Nothing
344− go js@(Object o) = mempty
345− & type_ .~ SwaggerObject
354+ (z:_) | allSame -> Just z
355+ _ -> Nothing
356+ go (Object o) = mempty
357+ & type_ ?~ SwaggerObject
346358 & required .~ HashMap.keys o
347359 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
348360
@@ -365,24 +377,24 @@ sketchSchema = sketch . toJSON
365377sketchStrictSchema :: ToJSON a => a -> Schema
366378sketchStrictSchema = go . toJSON
367379 where
368− go Null = mempty & type_ .~ SwaggerNull
380+ go Null = mempty & type_ ?~ SwaggerNull
369381 go js@(Bool _) = mempty
370− & type_ .~ SwaggerBoolean
382+ & type_ ?~ SwaggerBoolean
371383 & enum_ ?~ [js]
372384 go js@(String s) = mempty
373− & type_ .~ SwaggerString
385+ & type_ ?~ SwaggerString
374386 & maxLength ?~ fromIntegral (T.length s)
375387 & minLength ?~ fromIntegral (T.length s)
376388 & pattern ?~ s
377389 & enum_ ?~ [js]
378390 go js@(Number n) = mempty
379− & type_ .~ SwaggerNumber
391+ & type_ ?~ SwaggerNumber
380392 & maximum_ ?~ n
381393 & minimum_ ?~ n
382394 & multipleOf ?~ n
383395 & enum_ ?~ [js]
384396 go js@(Array xs) = mempty
385− & type_ .~ SwaggerArray
397+ & type_ ?~ SwaggerArray
386398 & maxItems ?~ fromIntegral sz
387399 & minItems ?~ fromIntegral sz
388400 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -392,7 +404,7 @@ sketchStrictSchema = go . toJSON
392404 sz = length xs
393405 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
394406 go js@(Object o) = mempty
395− & type_ .~ SwaggerObject
407+ & type_ ?~ SwaggerObject
396408 & required .~ names
397409 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
398410 & maxProperties ?~ fromIntegral (length names)
@@ -402,18 +414,19 @@ sketchStrictSchema = go . toJSON
402414 names = HashMap.keys o
403415
404416class GToSchema (f :: * -> *) where
405− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
417+ gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
406418
407419instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
408420 declareNamedSchema _ = do
409421 ref <- declareSchemaRef (Proxy :: Proxy a)
410422 return $ unnamed $ mempty
411− & type_ .~ SwaggerArray
423+ & type_ ?~ SwaggerArray
412424 & items ?~ SwaggerItemsObject ref
413425
414426instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
415427instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
416428instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429+instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
417430instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
418431instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
419432instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -433,6 +446,8 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
433446instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
434447instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
435448
449+instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450+
436451instance ToSchema a => ToSchema (Maybe a) where
437452 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
438453
@@ -441,6 +456,11 @@ instance (ToSchema a, ToSchema b) => ToSchema (Either a b)
441456instance ToSchema () where
442457 declareNamedSchema _ = pure (NamedSchema Nothing nullarySchema)
443458
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+
444464instance (ToSchema a, ToSchema b) => ToSchema (a, b)
445465instance (ToSchema a, ToSchema b, ToSchema c) => ToSchema (a, b, c)
446466instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d) => ToSchema (a, b, c, d)
@@ -450,7 +470,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
450470
451471timeSchema :: T.Text -> Schema
452472timeSchema fmt = mempty
453− & type_ .~ SwaggerString
473+ & type_ ?~ SwaggerString
454474 & format ?~ fmt
455475
456476-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -471,7 +491,7 @@ instance ToSchema ZonedTime where
471491 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
472492
473493instance ToSchema NominalDiffTime where
474− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
494+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
475495
476496-- |
477497-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -483,6 +503,8 @@ instance ToSchema UTCTime where
483503instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
484504instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
485505
506+instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507+
486508#if __GLASGOW_HASKELL__ < 800
487509#else
488510type family ToSchemaByteStringError bs where
@@ -502,12 +524,27 @@ instance ToSchema a => ToSchema (IntMap a) where
502524 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [(Int, a)])
503525
504526#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
505542
506543instance ToSchema a => ToSchema (Map String a) where
507544 declareNamedSchema _ = do
508545 schema <- declareSchemaRef (Proxy :: Proxy a)
509546 return $ unnamed $ mempty
510− & type_ .~ SwaggerObject
547+ & type_ ?~ SwaggerObject
511548 & additionalProperties ?~ schema
512549
513550instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -517,24 +554,14 @@ instance ToSchema a => ToSchema (HashMap String a) where declareNamedSchema _ =
517554instance ToSchema a => ToSchema (HashMap T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
518555instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
519556
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))
535−
536557#endif
537558
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+
538565instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
539566instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
540567instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -548,6 +575,13 @@ instance ToSchema a => ToSchema (Set a) where
548575
549576instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
550577
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+
551585instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
552586instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
553587
@@ -557,61 +591,141 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
557591instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
558592instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
559593
594+instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595+
560596-- | Default schema for @'Bounded'@, @'Integral'@ types.
561597--
562598-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
563599-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
564−toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
600+toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
565601toSchemaBoundedIntegral _ = mempty
566− & type_ .~ SwaggerInteger
602+ & type_ ?~ SwaggerInteger
567603 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
568604 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
569605
570606-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
571−genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
607+genericToNamedSchemaBoundedIntegral :: forall a d f.
572608 ( Bounded a, Integral a
573609 , Generic a, Rep a ~ D1 d f, Datatype d)
574− => SchemaOptions -> proxy a -> NamedSchema
610+ => SchemaOptions -> Proxy a -> NamedSchema
575611genericToNamedSchemaBoundedIntegral opts proxy
576− = 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
577668
578669-- | A configurable generic @'Schema'@ creator.
579−genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
580−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
581673
582674-- | A configurable generic @'NamedSchema'@ creator.
583675-- This function applied to @'defaultSchemaOptions'@
584676-- is used as the default for @'declareNamedSchema'@
585677-- when the type is an instance of @'Generic'@.
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
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))
588702
589−gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
590−gdatatypeSchemaName opts _ = case name of
703+gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704+gdatatypeSchemaName opts _ = case orig of
591705 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
592706 _ -> Nothing
593707 where
594− name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
708+ orig = datatypeName (Proxy3 :: Proxy3 d f a)
709+ name = datatypeNameModifier opts orig
595710
596711-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
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)
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)
601715
602716-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
603−paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
604−paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
717+paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718+paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
605719
606720nullarySchema :: Schema
607721nullarySchema = mempty
608− & type_ .~ SwaggerArray
722+ & type_ ?~ SwaggerArray
609723 & items ?~ SwaggerItemsArray []
610724
611−gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
725+gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
612726gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
613727
614−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
728+gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
615729gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
616730
617731instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -631,7 +745,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
631745 gdeclareNamedSchema = gdeclareNamedSumSchema
632746
633747-- | Single field constructor.
634−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
635749 gdeclareNamedSchema opts _ s
636750 | unwrapUnaryRecords opts = fieldSchema
637751 | otherwise =
@@ -645,7 +759,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
645759 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
646760 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
647761
648−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
762+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
649763gdeclareSchemaRef opts proxy = do
650764 case gtoNamedSchema opts proxy of
651765 NamedSchema (Just name) schema -> do
@@ -664,7 +778,7 @@ gdeclareSchemaRef opts proxy = do
664778 return $ Ref (Reference name)
665779 _ -> Inline <$> gdeclareSchema opts proxy
666780
667−appendItem :: Referenced Schema -> Maybe (SwaggerItems SwaggerKindSchema) -> Maybe (SwaggerItems SwaggerKindSchema)
781+appendItem :: Referenced Schema -> Maybe (SwaggerItems 'SwaggerKindSchema) -> Maybe (SwaggerItems 'SwaggerKindSchema)
668782appendItem x Nothing = Just (SwaggerItemsArray [x])
669783appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (xs ++ [x]))
670784appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
@@ -676,10 +790,12 @@ withFieldSchema opts _ isRequiredField schema = do
676790 return $
677791 if T.null fname
678792 then schema
679− & type_ .~ SwaggerArray
793+ & type_ ?~ SwaggerArray
680794 & items %~ appendItem ref
795+ & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796+ & minItems %~ Just . maybe 1 (+1) -- increment minItems
681797 else schema
682− & type_ .~ SwaggerObject
798+ & type_ ?~ SwaggerObject
683799 & properties . at fname ?~ ref
684800 & if isRequiredField
685801 then required %~ (++ [fname])
@@ -701,10 +817,13 @@ instance OVERLAPPING_ ToSchema c => GToSchema (K1 i (Maybe c)) where
701817instance OVERLAPPABLE_ ToSchema c => GToSchema (K1 i c) where
702818 gdeclareNamedSchema _ _ _ = declareNamedSchema (Proxy :: Proxy c)
703819
704−instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
820+instance ( GSumToSchema f
821+ , GSumToSchema g
822+ ) => GToSchema (f :+: g)
823+ where
705824 gdeclareNamedSchema = gdeclareNamedSumSchema
706825
707−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
826+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
708827gdeclareNamedSumSchema opts proxy s
709828 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
710829 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -713,29 +832,29 @@ gdeclareNamedSumSchema opts proxy s
713832 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
714833
715834 toStringTag schema = mempty
716− & type_ .~ SwaggerString
835+ & type_ ?~ SwaggerString
717836 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
718837
719838type AllNullary = All
720839
721−class GSumToSchema f where
722− 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
723842
724843instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
725844 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
726845
727−gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
728− 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
729848gsumConToSchemaWith ref opts _ schema = schema
730− & type_ .~ SwaggerObject
849+ & type_ ?~ SwaggerObject
731850 & properties . at tag ?~ ref
732851 & maxProperties ?~ 1
733852 & minProperties ?~ 1
734853 where
735854 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
736855
737−gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
738− 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
739858gsumConToSchema opts proxy schema = do
740859 ref <- gdeclareSchemaRef opts proxy
741860 return $ gsumConToSchemaWith ref opts proxy schema
@@ -757,3 +876,7 @@ data Proxy2 a b = Proxy2
757876
758877data Proxy3 a b c = Proxy3
759878
879+-- $setup
880+-- >>> import Data.Swagger
881+-- >>> import Data.Aeson (encode)
882+-- >>> import Data.Aeson.Types (toJSONKeyText)