@@ -32,7 +32,7 @@ import Data.Data.Lens (template)
33import Control.Monad
33import Control.Monad
34import Control.Monad.Writer
34import Control.Monad.Writer
35−import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..))
35+import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
36import Data.Char
36import Data.Char
37import Data.Data (Data)
37import Data.Data (Data)
38import Data.Foldable (traverse_)
38import Data.Foldable (traverse_)
@@ -44,7 +44,7 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
44import Data.Int
44import Data.Int
45import Data.IntSet (IntSet)
45import Data.IntSet (IntSet)
46import Data.IntMap (IntMap)
46import Data.IntMap (IntMap)
47−import Data.List.NonEmpty (NonEmpty)
47+import Data.List.NonEmpty.Compat (NonEmpty)
48import Data.Map (Map)
48import Data.Map (Map)
49import Data.Proxy
49import Data.Proxy
50import Data.Scientific (Scientific)
50import Data.Scientific (Scientific)
@@ -57,7 +57,8 @@ import qualified Data.Vector as V
57import qualified Data.Vector.Primitive as VP
57import qualified Data.Vector.Primitive as VP
58import qualified Data.Vector.Storable as VS
58import qualified Data.Vector.Storable as VS
59import qualified Data.Vector.Unboxed as VU
59import qualified Data.Vector.Unboxed as VU
60+import Data.Version (Version)
60−import Numeric.Natural
61+import Numeric.Natural.Compat (Natural)
61import Data.Word
62import Data.Word
62import GHC.Generics
63import GHC.Generics
63import qualified Data.UUID.Types as UUID
64import qualified Data.UUID.Types as UUID
@@ -110,7 +111,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
110-- declareNamedSchema _ = do
111-- declareNamedSchema _ = do
111-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
112-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
112-- return $ NamedSchema (Just \"Coord\") $ mempty
113-- return $ NamedSchema (Just \"Coord\") $ mempty
113−-- & type_ .~ SwaggerObject
114+-- & type_ ?~ SwaggerObject
114-- & properties .~
115-- & properties .~
115-- [ (\"x\", doubleSchema)
116-- [ (\"x\", doubleSchema)
116-- , (\"y\", doubleSchema)
117-- , (\"y\", doubleSchema)
@@ -141,13 +142,17 @@ class ToSchema a where
141 -- together with all used definitions.
142 -- together with all used definitions.
142 -- Note that the schema itself is included in definitions
143 -- Note that the schema itself is included in definitions
143 -- only if it is recursive (and thus needs its definition in scope).
144 -- only if it is recursive (and thus needs its definition in scope).
144− declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
145+ declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
145 default declareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
146 default declareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
146− proxy a -> Declare (Definitions Schema) NamedSchema
147+ Proxy a -> Declare (Definitions Schema) NamedSchema
147 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
149-- | Convert a type into a schema and declare all used schema definitions.
154-- | Convert a type into a schema and declare all used schema definitions.
150−declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
155+declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
151declareSchema = fmap _namedSchemaSchema . declareNamedSchema
156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
153-- | Convert a type into an optionally named schema.
158-- | Convert a type into an optionally named schema.
@@ -161,7 +166,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
161-- Just "Day"
166-- Just "Day"
162-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
167-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
163-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
168-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
164−toNamedSchema :: ToSchema a => proxy a -> NamedSchema
169+toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
165toNamedSchema = undeclare . declareNamedSchema
170toNamedSchema = undeclare . declareNamedSchema
167-- | Get type's schema name according to its @'ToSchema'@ instance.
172-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -171,7 +176,7 @@ toNamedSchema = undeclare . declareNamedSchema
172-- >>> schemaName (Proxy :: Proxy UTCTime)
177-- >>> schemaName (Proxy :: Proxy UTCTime)
173-- Just "UTCTime"
178-- Just "UTCTime"
174−schemaName :: ToSchema a => proxy a -> Maybe T.Text
179+schemaName :: ToSchema a => Proxy a -> Maybe T.Text
175schemaName = _namedSchemaName . toNamedSchema
180schemaName = _namedSchemaName . toNamedSchema
177-- | Convert a type into a schema.
182-- | Convert a type into a schema.
@@ -181,7 +186,7 @@ schemaName = _namedSchemaName . toNamedSchema
182-- >>> encode $ toSchema (Proxy :: Proxy [Day])
187-- >>> encode $ toSchema (Proxy :: Proxy [Day])
183-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
188-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
184−toSchema :: ToSchema a => proxy a -> Schema
189+toSchema :: ToSchema a => Proxy a -> Schema
185toSchema = _namedSchemaSchema . toNamedSchema
190toSchema = _namedSchemaSchema . toNamedSchema
187-- | Convert a type into a referenced schema if possible.
192-- | Convert a type into a referenced schema if possible.
@@ -192,7 +197,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
193-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
198-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
194-- "{\"$ref\":\"#/definitions/Day\"}"
199-- "{\"$ref\":\"#/definitions/Day\"}"
195−toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
200+toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
196toSchemaRef = undeclare . declareSchemaRef
201toSchemaRef = undeclare . declareSchemaRef
198-- | Convert a type into a referenced schema if possible
203-- | Convert a type into a referenced schema if possible
@@ -202,7 +207,7 @@ toSchemaRef = undeclare . declareSchemaRef
202-- Schema definitions are typically declared for every referenced schema.
207-- Schema definitions are typically declared for every referenced schema.
203-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
208-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
204-- will be declared (regardless of whether it is recusive or not).
209-- will be declared (regardless of whether it is recusive or not).
205−declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
210+declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
206declareSchemaRef proxy = do
211declareSchemaRef proxy = do
207 case toNamedSchema proxy of
212 case toNamedSchema proxy of
208 NamedSchema (Just name) schema -> do
213 NamedSchema (Just name) schema -> do
@@ -264,7 +269,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
265-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
270-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
266-- when inlining recursive schemas.
271-- when inlining recursive schemas.
267−toInlinedSchema :: ToSchema a => proxy a -> Schema
272+toInlinedSchema :: ToSchema a => Proxy a -> Schema
268toInlinedSchema proxy = inlineAllSchemas defs schema
273toInlinedSchema proxy = inlineAllSchemas defs schema
270 (defs, schema) = runDeclare (declareSchema proxy) mempty
275 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -293,20 +298,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
293-- | Default schema for binary data (any sequence of octets).
298-- | Default schema for binary data (any sequence of octets).
294binarySchema :: Schema
299binarySchema :: Schema
295binarySchema = mempty
300binarySchema = mempty
296− & type_ .~ SwaggerString
301+ & type_ ?~ SwaggerString
297 & format ?~ "binary"
302 & format ?~ "binary"
299-- | Default schema for binary data (base64 encoded).
304-- | Default schema for binary data (base64 encoded).
300byteSchema :: Schema
305byteSchema :: Schema
301byteSchema = mempty
306byteSchema = mempty
302− & type_ .~ SwaggerString
307+ & type_ ?~ SwaggerString
303 & format ?~ "byte"
308 & format ?~ "byte"
305-- | Default schema for password string.
310-- | Default schema for password string.
306-- @"password"@ format is used to hint UIs the input needs to be obscured.
311-- @"password"@ format is used to hint UIs the input needs to be obscured.
307passwordSchema :: Schema
312passwordSchema :: Schema
308passwordSchema = mempty
313passwordSchema = mempty
309− & type_ .~ SwaggerString
314+ & type_ ?~ SwaggerString
310 & format ?~ "password"
315 & format ?~ "password"
312-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
317-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -332,12 +337,12 @@ sketchSchema = sketch . toJSON
332 sketch js@(Bool _) = go js
337 sketch js@(Bool _) = go js
333 sketch js = go js & example ?~ js
338 sketch js = go js & example ?~ js
335− go Null = mempty & type_ .~ SwaggerNull
340+ go Null = mempty & type_ ?~ SwaggerNull
336− go (Bool _) = mempty & type_ .~ SwaggerBoolean
341+ go (Bool _) = mempty & type_ ?~ SwaggerBoolean
337− go (String _) = mempty & type_ .~ SwaggerString
342+ go (String _) = mempty & type_ ?~ SwaggerString
338− go (Number _) = mempty & type_ .~ SwaggerNumber
343+ go (Number _) = mempty & type_ ?~ SwaggerNumber
339 go (Array xs) = mempty
344 go (Array xs) = mempty
340− & type_ .~ SwaggerArray
345+ & type_ ?~ SwaggerArray
341 & items ?~ case ischema of
346 & items ?~ case ischema of
342 Just s -> SwaggerItemsObject (Inline s)
347 Just s -> SwaggerItemsObject (Inline s)
343 _ -> SwaggerItemsArray (map Inline ys)
348 _ -> SwaggerItemsArray (map Inline ys)
@@ -349,7 +354,7 @@ sketchSchema = sketch . toJSON
349 (z:_) | allSame -> Just z
354 (z:_) | allSame -> Just z
350 _ -> Nothing
355 _ -> Nothing
351 go (Object o) = mempty
356 go (Object o) = mempty
352− & type_ .~ SwaggerObject
357+ & type_ ?~ SwaggerObject
353 & required .~ HashMap.keys o
358 & required .~ HashMap.keys o
354 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
359 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
@@ -372,24 +377,24 @@ sketchSchema = sketch . toJSON
372sketchStrictSchema :: ToJSON a => a -> Schema
377sketchStrictSchema :: ToJSON a => a -> Schema
373sketchStrictSchema = go . toJSON
378sketchStrictSchema = go . toJSON
375− go Null = mempty & type_ .~ SwaggerNull
380+ go Null = mempty & type_ ?~ SwaggerNull
376 go js@(Bool _) = mempty
381 go js@(Bool _) = mempty
377− & type_ .~ SwaggerBoolean
382+ & type_ ?~ SwaggerBoolean
378 & enum_ ?~ [js]
383 & enum_ ?~ [js]
379 go js@(String s) = mempty
384 go js@(String s) = mempty
380− & type_ .~ SwaggerString
385+ & type_ ?~ SwaggerString
381 & maxLength ?~ fromIntegral (T.length s)
386 & maxLength ?~ fromIntegral (T.length s)
382 & minLength ?~ fromIntegral (T.length s)
387 & minLength ?~ fromIntegral (T.length s)
383 & pattern ?~ s
388 & pattern ?~ s
384 & enum_ ?~ [js]
389 & enum_ ?~ [js]
385 go js@(Number n) = mempty
390 go js@(Number n) = mempty
386− & type_ .~ SwaggerNumber
391+ & type_ ?~ SwaggerNumber
387 & maximum_ ?~ n
392 & maximum_ ?~ n
388 & minimum_ ?~ n
393 & minimum_ ?~ n
389 & multipleOf ?~ n
394 & multipleOf ?~ n
390 & enum_ ?~ [js]
395 & enum_ ?~ [js]
391 go js@(Array xs) = mempty
396 go js@(Array xs) = mempty
392− & type_ .~ SwaggerArray
397+ & type_ ?~ SwaggerArray
393 & maxItems ?~ fromIntegral sz
398 & maxItems ?~ fromIntegral sz
394 & minItems ?~ fromIntegral sz
399 & minItems ?~ fromIntegral sz
395 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
400 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -399,7 +404,7 @@ sketchStrictSchema = go . toJSON
399 sz = length xs
404 sz = length xs
400 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
405 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
401 go js@(Object o) = mempty
406 go js@(Object o) = mempty
402− & type_ .~ SwaggerObject
407+ & type_ ?~ SwaggerObject
403 & required .~ names
408 & required .~ names
404 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
409 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
405 & maxProperties ?~ fromIntegral (length names)
410 & maxProperties ?~ fromIntegral (length names)
@@ -409,13 +414,13 @@ sketchStrictSchema = go . toJSON
409 names = HashMap.keys o
414 names = HashMap.keys o
411class GToSchema (f :: * -> *) where
416class GToSchema (f :: * -> *) where
412− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
417+ gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
414instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
419instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
415 declareNamedSchema _ = do
420 declareNamedSchema _ = do
416 ref <- declareSchemaRef (Proxy :: Proxy a)
421 ref <- declareSchemaRef (Proxy :: Proxy a)
417 return $ unnamed $ mempty
422 return $ unnamed $ mempty
418− & type_ .~ SwaggerArray
423+ & type_ ?~ SwaggerArray
419 & items ?~ SwaggerItemsObject ref
424 & items ?~ SwaggerItemsObject ref
421instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
426instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
@@ -465,7 +470,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
466timeSchema :: T.Text -> Schema
471timeSchema :: T.Text -> Schema
467timeSchema fmt = mempty
472timeSchema fmt = mempty
468− & type_ .~ SwaggerString
473+ & type_ ?~ SwaggerString
469 & format ?~ fmt
474 & format ?~ fmt
471-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
476-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -498,6 +503,8 @@ instance ToSchema UTCTime where
498instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
503instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
499instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
504instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
506+instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
501#if __GLASGOW_HASKELL__ < 800
508#if __GLASGOW_HASKELL__ < 800
503type family ToSchemaByteStringError bs where
510type family ToSchemaByteStringError bs where
@@ -525,8 +532,8 @@ instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
525 declareObjectMapSchema = do
532 declareObjectMapSchema = do
526 schema <- declareSchemaRef (Proxy :: Proxy v)
533 schema <- declareSchemaRef (Proxy :: Proxy v)
527 return $ unnamed $ mempty
534 return $ unnamed $ mempty
528− & type_ .~ SwaggerObject
535+ & type_ ?~ SwaggerObject
529− & additionalProperties ?~ schema
536+ & additionalProperties ?~ AdditionalPropertiesSchema schema
531instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
538instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
532 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
539 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
@@ -537,7 +544,7 @@ instance ToSchema a => ToSchema (Map String a) where
537 declareNamedSchema _ = do
544 declareNamedSchema _ = do
538 schema <- declareSchemaRef (Proxy :: Proxy a)
545 schema <- declareSchemaRef (Proxy :: Proxy a)
539 return $ unnamed $ mempty
546 return $ unnamed $ mempty
540− & type_ .~ SwaggerObject
547+ & type_ ?~ SwaggerObject
541 & additionalProperties ?~ schema
548 & additionalProperties ?~ schema
543instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
550instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -549,6 +556,12 @@ instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ =
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
552instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
565instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
553instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
566instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
554instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
567instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -562,6 +575,7 @@ instance ToSchema a => ToSchema (Set a) where
563instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
576instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
565instance ToSchema a => ToSchema (NonEmpty a) where
579instance ToSchema a => ToSchema (NonEmpty a) where
566 declareNamedSchema _ = do
580 declareNamedSchema _ = do
567 schema <- declareSchema (Proxy :: Proxy [a])
581 schema <- declareSchema (Proxy :: Proxy [a])
@@ -583,26 +597,26 @@ instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declar
584-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
598-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
585-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
599-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
586−toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
600+toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
587toSchemaBoundedIntegral _ = mempty
601toSchemaBoundedIntegral _ = mempty
588− & type_ .~ SwaggerInteger
602+ & type_ ?~ SwaggerInteger
589 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
603 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
590 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
604 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
592-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
606-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
593−genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
607+genericToNamedSchemaBoundedIntegral :: forall a d f.
594 ( Bounded a, Integral a
608 ( Bounded a, Integral a
595 , Generic a, Rep a ~ D1 d f, Datatype d)
609 , Generic a, Rep a ~ D1 d f, Datatype d)
596− => SchemaOptions -> proxy a -> NamedSchema
610+ => SchemaOptions -> Proxy a -> NamedSchema
597genericToNamedSchemaBoundedIntegral opts proxy
611genericToNamedSchemaBoundedIntegral opts proxy
598 = genericNameSchema opts proxy (toSchemaBoundedIntegral proxy)
612 = genericNameSchema opts proxy (toSchemaBoundedIntegral proxy)
600-- | Declare a named schema for a @newtype@ wrapper.
614-- | Declare a named schema for a @newtype@ wrapper.
601−genericDeclareNamedSchemaNewtype :: forall proxy a d c s i inner.
615+genericDeclareNamedSchemaNewtype :: forall a d c s i inner.
602 (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner))))
616 (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner))))
603 => SchemaOptions -- ^ How to derive the name.
617 => SchemaOptions -- ^ How to derive the name.
604 -> (Proxy inner -> Declare (Definitions Schema) Schema) -- ^ How to create a schema for the wrapped type.
618 -> (Proxy inner -> Declare (Definitions Schema) Schema) -- ^ How to create a schema for the wrapped type.
605− -> proxy a
619+ -> Proxy a
606 -> Declare (Definitions Schema) NamedSchema
620 -> Declare (Definitions Schema) NamedSchema
607genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$> f (Proxy :: Proxy inner)
621genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$> f (Proxy :: Proxy inner)
@@ -619,9 +633,9 @@ genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$>
620-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
634-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
621-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
635-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
622−declareSchemaBoundedEnumKeyMapping :: forall map key value proxy.
636+declareSchemaBoundedEnumKeyMapping :: forall map key value.
623 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
637 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
624− => proxy (map key value) -> Declare (Definitions Schema) Schema
638+ => Proxy (map key value) -> Declare (Definitions Schema) Schema
625declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
639declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
626 ToJSONKeyText keyToText _ -> objectSchema keyToText
640 ToJSONKeyText keyToText _ -> objectSchema keyToText
627 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
641 ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
@@ -631,7 +645,7 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
631 let allKeys = [minBound..maxBound :: key]
645 let allKeys = [minBound..maxBound :: key]
632 mkPair k = (keyToText k, valueRef)
646 mkPair k = (keyToText k, valueRef)
633 return $ mempty
647 return $ mempty
634− & type_ .~ SwaggerObject
648+ & type_ ?~ SwaggerObject
635 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
649 & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
637-- | A 'Schema' for a mapping with 'Bounded' 'Enum' keys.
651-- | A 'Schema' for a mapping with 'Bounded' 'Enum' keys.
@@ -647,46 +661,46 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
648-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
662-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
649-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
663-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
650−toSchemaBoundedEnumKeyMapping :: forall map key value proxy.
664+toSchemaBoundedEnumKeyMapping :: forall map key value.
651 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
665 (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
652− => proxy (map key value) -> Schema
666+ => Proxy (map key value) -> Schema
653toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
667toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
655-- | A configurable generic @'Schema'@ creator.
669-- | A configurable generic @'Schema'@ creator.
656genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
670genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
657− SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
671+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
658genericDeclareSchema = genericDeclareSchemaUnrestricted
672genericDeclareSchema = genericDeclareSchemaUnrestricted
660-- | A configurable generic @'NamedSchema'@ creator.
674-- | A configurable generic @'NamedSchema'@ creator.
661-- This function applied to @'defaultSchemaOptions'@
675-- This function applied to @'defaultSchemaOptions'@
662-- is used as the default for @'declareNamedSchema'@
676-- is used as the default for @'declareNamedSchema'@
663-- when the type is an instance of @'Generic'@.
677-- when the type is an instance of @'Generic'@.
664−genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
678+genericDeclareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
665− SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
679+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
666genericDeclareNamedSchema = genericDeclareNamedSchemaUnrestricted
680genericDeclareNamedSchema = genericDeclareNamedSchemaUnrestricted
668-- | A configurable generic @'Schema'@ creator.
682-- | A configurable generic @'Schema'@ creator.
670-- Unlike 'genericDeclareSchema' also works for mixed sum types.
684-- Unlike 'genericDeclareSchema' also works for mixed sum types.
671-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
685-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
672−genericDeclareSchemaUnrestricted :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
686+genericDeclareSchemaUnrestricted :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
673genericDeclareSchemaUnrestricted opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchemaUnrestricted opts proxy
687genericDeclareSchemaUnrestricted opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchemaUnrestricted opts proxy
675-- | A configurable generic @'NamedSchema'@ creator.
689-- | A configurable generic @'NamedSchema'@ creator.
677-- Unlike 'genericDeclareNamedSchema' also works for mixed sum types.
691-- Unlike 'genericDeclareNamedSchema' also works for mixed sum types.
678-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
692-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
679−genericDeclareNamedSchemaUnrestricted :: forall a proxy. (Generic a, GToSchema (Rep a)) =>
693+genericDeclareNamedSchemaUnrestricted :: forall a. (Generic a, GToSchema (Rep a)) =>
680− SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
694+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
681genericDeclareNamedSchemaUnrestricted opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
695genericDeclareNamedSchemaUnrestricted opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
683-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
697-- | Derive a 'Generic'-based name for a datatype and assign it to a given 'Schema'.
684−genericNameSchema :: forall a d f proxy.
698+genericNameSchema :: forall a d f.
685 (Generic a, Rep a ~ D1 d f, Datatype d)
699 (Generic a, Rep a ~ D1 d f, Datatype d)
686− => SchemaOptions -> proxy a -> Schema -> NamedSchema
700+ => SchemaOptions -> Proxy a -> Schema -> NamedSchema
687genericNameSchema opts _ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d))
701genericNameSchema opts _ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d))
689−gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
703+gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
690gdatatypeSchemaName opts _ = case orig of
704gdatatypeSchemaName opts _ = case orig of
691 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
705 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
692 _ -> Nothing
706 _ -> Nothing
@@ -695,24 +709,23 @@ gdatatypeSchemaName opts _ = case orig of
695 name = datatypeNameModifier opts orig
709 name = datatypeNameModifier opts orig
697-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
711-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
698−paramSchemaToNamedSchema :: forall a d f proxy.
699− (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
712+paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) =>
700− => SchemaOptions -> proxy a -> NamedSchema
713+ SchemaOptions -> Proxy a -> NamedSchema
701paramSchemaToNamedSchema opts proxy = genericNameSchema opts proxy (paramSchemaToSchema proxy)
714paramSchemaToNamedSchema opts proxy = genericNameSchema opts proxy (paramSchemaToSchema proxy)
703-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
716-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
704−paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
717+paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
705−paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
718+paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
707nullarySchema :: Schema
720nullarySchema :: Schema
708nullarySchema = mempty
721nullarySchema = mempty
709− & type_ .~ SwaggerArray
722+ & type_ ?~ SwaggerArray
710 & items ?~ SwaggerItemsArray []
723 & items ?~ SwaggerItemsArray []
712−gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
725+gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
713gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
726gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
715−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
728+gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
716gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
729gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
718instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
731instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -746,7 +759,7 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
746 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
759 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
747 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
760 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
749−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
762+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
750gdeclareSchemaRef opts proxy = do
763gdeclareSchemaRef opts proxy = do
751 case gtoNamedSchema opts proxy of
764 case gtoNamedSchema opts proxy of
752 NamedSchema (Just name) schema -> do
765 NamedSchema (Just name) schema -> do
@@ -777,12 +790,12 @@ withFieldSchema opts _ isRequiredField schema = do
778 if T.null fname
791 if T.null fname
779 then schema
792 then schema
780− & type_ .~ SwaggerArray
793+ & type_ ?~ SwaggerArray
781 & items %~ appendItem ref
794 & items %~ appendItem ref
782 & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
795 & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
783 & minItems %~ Just . maybe 1 (+1) -- increment minItems
796 & minItems %~ Just . maybe 1 (+1) -- increment minItems
784 else schema
797 else schema
785− & type_ .~ SwaggerObject
798+ & type_ ?~ SwaggerObject
786 & properties . at fname ?~ ref
799 & properties . at fname ?~ ref
787 & if isRequiredField
800 & if isRequiredField
788 then required %~ (++ [fname])
801 then required %~ (++ [fname])
@@ -810,7 +823,7 @@ instance ( GSumToSchema f
811 gdeclareNamedSchema = gdeclareNamedSumSchema
824 gdeclareNamedSchema = gdeclareNamedSumSchema
813−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
826+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
814gdeclareNamedSumSchema opts proxy s
827gdeclareNamedSumSchema opts proxy s
815 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
828 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
816 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
829 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -819,29 +832,29 @@ gdeclareNamedSumSchema opts proxy s
819 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
832 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
821 toStringTag schema = mempty
834 toStringTag schema = mempty
822− & type_ .~ SwaggerString
835+ & type_ ?~ SwaggerString
823 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
836 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
825type AllNullary = All
838type AllNullary = All
827class GSumToSchema (f :: * -> *) where
840class GSumToSchema (f :: * -> *) where
828− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
841+ gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
830instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
843instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
831 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
844 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
833−gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
846+gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
834− Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
847+ Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
835gsumConToSchemaWith ref opts _ schema = schema
848gsumConToSchemaWith ref opts _ schema = schema
836− & type_ .~ SwaggerObject
849+ & type_ ?~ SwaggerObject
837 & properties . at tag ?~ ref
850 & properties . at tag ?~ ref
838 & maxProperties ?~ 1
851 & maxProperties ?~ 1
839 & minProperties ?~ 1
852 & minProperties ?~ 1
841 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
854 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
843−gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
856+gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
844− SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
857+ SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
845gsumConToSchema opts proxy schema = do
858gsumConToSchema opts proxy schema = do
846 ref <- gdeclareSchemaRef opts proxy
859 ref <- gdeclareSchemaRef opts proxy
847 return $ gsumConToSchemaWith ref opts proxy schema
860 return $ gsumConToSchemaWith ref opts proxy schema