@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ConstraintKinds #-}
23{-# LANGUAGE DefaultSignatures #-}
34{-# LANGUAGE FlexibleContexts #-}
45{-# LANGUAGE FlexibleInstances #-}
@@ -31,7 +32,7 @@ import Data.Data.Lens (template)
3132
3233import Control.Monad
3334import Control.Monad.Writer
34−import Data.Aeson
35+import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
3536import Data.Char
3637import Data.Data (Data)
3738import Data.Foldable (traverse_)
@@ -43,9 +44,11 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4344import Data.Int
4445import Data.IntSet (IntSet)
4546import Data.IntMap (IntMap)
47+import Data.List.NonEmpty.Compat (NonEmpty)
4648import Data.Map (Map)
4749import Data.Proxy
4850import Data.Scientific (Scientific)
51+import Data.Fixed (Fixed, HasResolution, Pico)
4952import Data.Set (Set)
5053import qualified Data.Text as T
5154import qualified Data.Text.Lazy as TL
@@ -54,6 +57,8 @@ import qualified Data.Vector as V
5457import qualified Data.Vector.Primitive as VP
5558import qualified Data.Vector.Storable as VS
5659import qualified Data.Vector.Unboxed as VU
60+import Data.Version (Version)
61+import Numeric.Natural.Compat (Natural)
5762import Data.Word
5863import GHC.Generics
5964import qualified Data.UUID.Types as UUID
@@ -106,7 +111,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
106111-- declareNamedSchema _ = do
107112-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
108113-- return $ NamedSchema (Just \"Coord\") $ mempty
109−-- & type_ .~ SwaggerObject
114+-- & type_ ?~ SwaggerObject
110115-- & properties .~
111116-- [ (\"x\", doubleSchema)
112117-- , (\"y\", doubleSchema)
@@ -137,13 +142,17 @@ class ToSchema a where
137142 -- together with all used definitions.
138143 -- Note that the schema itself is included in definitions
139144 -- only if it is recursive (and thus needs its definition in scope).
140− declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
145+ declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
141146 default declareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
142− proxy a -> Declare (Definitions Schema) NamedSchema
147+ Proxy a -> Declare (Definitions Schema) NamedSchema
143148 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
144149
150+instance ToSchema TimeOfDay where
151+ declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152+ & example ?~ toJSON (TimeOfDay 12 33 15)
153+
145154-- | Convert a type into a schema and declare all used schema definitions.
146−declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
155+declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
147156declareSchema = fmap _namedSchemaSchema . declareNamedSchema
148157
149158-- | Convert a type into an optionally named schema.
@@ -157,7 +166,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
157166-- Just "Day"
158167-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
159168-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
160−toNamedSchema :: ToSchema a => proxy a -> NamedSchema
169+toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
161170toNamedSchema = undeclare . declareNamedSchema
162171
163172-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -167,7 +176,7 @@ toNamedSchema = undeclare . declareNamedSchema
167176--
168177-- >>> schemaName (Proxy :: Proxy UTCTime)
169178-- Just "UTCTime"
170−schemaName :: ToSchema a => proxy a -> Maybe T.Text
179+schemaName :: ToSchema a => Proxy a -> Maybe T.Text
171180schemaName = _namedSchemaName . toNamedSchema
172181
173182-- | Convert a type into a schema.
@@ -177,7 +186,7 @@ schemaName = _namedSchemaName . toNamedSchema
177186--
178187-- >>> encode $ toSchema (Proxy :: Proxy [Day])
179188-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
180−toSchema :: ToSchema a => proxy a -> Schema
189+toSchema :: ToSchema a => Proxy a -> Schema
181190toSchema = _namedSchemaSchema . toNamedSchema
182191
183192-- | Convert a type into a referenced schema if possible.
@@ -188,7 +197,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
188197--
189198-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
190199-- "{\"$ref\":\"#/definitions/Day\"}"
191−toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
200+toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
192201toSchemaRef = undeclare . declareSchemaRef
193202
194203-- | Convert a type into a referenced schema if possible
@@ -198,7 +207,7 @@ toSchemaRef = undeclare . declareSchemaRef
198207-- Schema definitions are typically declared for every referenced schema.
199208-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
200209-- will be declared (regardless of whether it is recusive or not).
201−declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
210+declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
202211declareSchemaRef proxy = do
203212 case toNamedSchema proxy of
204213 NamedSchema (Just name) schema -> do
@@ -260,7 +269,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
260269--
261270-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
262271-- when inlining recursive schemas.
263−toInlinedSchema :: ToSchema a => proxy a -> Schema
272+toInlinedSchema :: ToSchema a => Proxy a -> Schema
264273toInlinedSchema proxy = inlineAllSchemas defs schema
265274 where
266275 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -289,20 +298,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
289298-- | Default schema for binary data (any sequence of octets).
290299binarySchema :: Schema
291300binarySchema = mempty
292− & type_ .~ SwaggerString
301+ & type_ ?~ SwaggerString
293302 & format ?~ "binary"
294303
295304-- | Default schema for binary data (base64 encoded).
296305byteSchema :: Schema
297306byteSchema = mempty
298− & type_ .~ SwaggerString
307+ & type_ ?~ SwaggerString
299308 & format ?~ "byte"
300309
301310-- | Default schema for password string.
302311-- @"password"@ format is used to hint UIs the input needs to be obscured.
303312passwordSchema :: Schema
304313passwordSchema = mempty
305− & type_ .~ SwaggerString
314+ & type_ ?~ SwaggerString
306315 & format ?~ "password"
307316
308317-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -328,12 +337,12 @@ sketchSchema = sketch . toJSON
328337 sketch js@(Bool _) = go js
329338 sketch js = go js & example ?~ js
330339
331− go Null = mempty & type_ .~ SwaggerNull
332− go (Bool _) = mempty & type_ .~ SwaggerBoolean
333− go (String _) = mempty & type_ .~ SwaggerString
334− 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
335344 go (Array xs) = mempty
336− & type_ .~ SwaggerArray
345+ & type_ ?~ SwaggerArray
337346 & items ?~ case ischema of
338347 Just s -> SwaggerItemsObject (Inline s)
339348 _ -> SwaggerItemsArray (map Inline ys)
@@ -345,7 +354,7 @@ sketchSchema = sketch . toJSON
345354 (z:_) | allSame -> Just z
346355 _ -> Nothing
347356 go (Object o) = mempty
348− & type_ .~ SwaggerObject
357+ & type_ ?~ SwaggerObject
349358 & required .~ HashMap.keys o
350359 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
351360
@@ -368,24 +377,24 @@ sketchSchema = sketch . toJSON
368377sketchStrictSchema :: ToJSON a => a -> Schema
369378sketchStrictSchema = go . toJSON
370379 where
371− go Null = mempty & type_ .~ SwaggerNull
380+ go Null = mempty & type_ ?~ SwaggerNull
372381 go js@(Bool _) = mempty
373− & type_ .~ SwaggerBoolean
382+ & type_ ?~ SwaggerBoolean
374383 & enum_ ?~ [js]
375384 go js@(String s) = mempty
376− & type_ .~ SwaggerString
385+ & type_ ?~ SwaggerString
377386 & maxLength ?~ fromIntegral (T.length s)
378387 & minLength ?~ fromIntegral (T.length s)
379388 & pattern ?~ s
380389 & enum_ ?~ [js]
381390 go js@(Number n) = mempty
382− & type_ .~ SwaggerNumber
391+ & type_ ?~ SwaggerNumber
383392 & maximum_ ?~ n
384393 & minimum_ ?~ n
385394 & multipleOf ?~ n
386395 & enum_ ?~ [js]
387396 go js@(Array xs) = mempty
388− & type_ .~ SwaggerArray
397+ & type_ ?~ SwaggerArray
389398 & maxItems ?~ fromIntegral sz
390399 & minItems ?~ fromIntegral sz
391400 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -395,7 +404,7 @@ sketchStrictSchema = go . toJSON
395404 sz = length xs
396405 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
397406 go js@(Object o) = mempty
398− & type_ .~ SwaggerObject
407+ & type_ ?~ SwaggerObject
399408 & required .~ names
400409 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
401410 & maxProperties ?~ fromIntegral (length names)
@@ -405,18 +414,19 @@ sketchStrictSchema = go . toJSON
405414 names = HashMap.keys o
406415
407416class GToSchema (f :: * -> *) where
408− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
417+ gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
409418
410419instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
411420 declareNamedSchema _ = do
412421 ref <- declareSchemaRef (Proxy :: Proxy a)
413422 return $ unnamed $ mempty
414− & type_ .~ SwaggerArray
423+ & type_ ?~ SwaggerArray
415424 & items ?~ SwaggerItemsObject ref
416425
417426instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
418427instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
419428instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429+instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
420430instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
421431instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
422432instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -436,6 +446,8 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
436446instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
437447instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
438448
449+instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450+
439451instance ToSchema a => ToSchema (Maybe a) where
440452 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
441453
@@ -458,7 +470,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
458470
459471timeSchema :: T.Text -> Schema
460472timeSchema fmt = mempty
461− & type_ .~ SwaggerString
473+ & type_ ?~ SwaggerString
462474 & format ?~ fmt
463475
464476-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -479,7 +491,7 @@ instance ToSchema ZonedTime where
479491 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
480492
481493instance ToSchema NominalDiffTime where
482− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
494+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
483495
484496-- |
485497-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -491,6 +503,8 @@ instance ToSchema UTCTime where
491503instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
492504instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
493505
506+instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507+
494508#if __GLASGOW_HASKELL__ < 800
495509#else
496510type family ToSchemaByteStringError bs where
@@ -518,8 +532,8 @@ instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
518532 declareObjectMapSchema = do
519533 schema <- declareSchemaRef (Proxy :: Proxy v)
520534 return $ unnamed $ mempty
521− & type_ .~ SwaggerObject
522− & additionalProperties ?~ schema
535+ & type_ ?~ SwaggerObject
536+ & additionalProperties ?~ AdditionalPropertiesSchema schema
523537
524538instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
525539 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
@@ -530,7 +544,7 @@ instance ToSchema a => ToSchema (Map String a) where
530544 declareNamedSchema _ = do
531545 schema <- declareSchemaRef (Proxy :: Proxy a)
532546 return $ unnamed $ mempty
533− & type_ .~ SwaggerObject
547+ & type_ ?~ SwaggerObject
534548 & additionalProperties ?~ schema
535549
536550instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -542,6 +556,12 @@ instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ =
542556
543557#endif
544558
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+
545565instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
546566instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
547567instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -555,6 +575,13 @@ instance ToSchema a => ToSchema (Set a) where
555575
556576instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
557577
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+
558585instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
559586instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
560587
@@ -564,78 +591,141 @@ instance ToSchema a => ToSchema (First a) where declareNamedSchema _ = unname
564591instance ToSchema a => ToSchema (Last a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
565592instance ToSchema a => ToSchema (Dual a) where declareNamedSchema _ = unname <$> declareNamedSchema (Proxy :: Proxy a)
566593
594+instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
595+
567596-- | Default schema for @'Bounded'@, @'Integral'@ types.
568597--
569598-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
570599-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
571−toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
600+toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
572601toSchemaBoundedIntegral _ = mempty
573− & type_ .~ SwaggerInteger
602+ & type_ ?~ SwaggerInteger
574603 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
575604 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
576605
577606-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
578−genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
607+genericToNamedSchemaBoundedIntegral :: forall a d f.
579608 ( Bounded a, Integral a
580609 , Generic a, Rep a ~ D1 d f, Datatype d)
581− => SchemaOptions -> proxy a -> NamedSchema
610+ => SchemaOptions -> Proxy a -> NamedSchema
582611genericToNamedSchemaBoundedIntegral opts proxy
583− = 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
584668
585669-- | A configurable generic @'Schema'@ creator.
586670genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
587− SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
671+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
588672genericDeclareSchema = genericDeclareSchemaUnrestricted
589673
590674-- | A configurable generic @'NamedSchema'@ creator.
591675-- This function applied to @'defaultSchemaOptions'@
592676-- is used as the default for @'declareNamedSchema'@
593677-- when the type is an instance of @'Generic'@.
594−genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
595− SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
678+genericDeclareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
679+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
596680genericDeclareNamedSchema = genericDeclareNamedSchemaUnrestricted
597681
598682-- | A configurable generic @'Schema'@ creator.
599683--
600684-- Unlike 'genericDeclareSchema' also works for mixed sum types.
601685-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
602−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
603687genericDeclareSchemaUnrestricted opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchemaUnrestricted opts proxy
604688
605689-- | A configurable generic @'NamedSchema'@ creator.
606690--
607691-- Unlike 'genericDeclareNamedSchema' also works for mixed sum types.
608692-- Use with care since some Swagger tools do not support well schemas for mixed sum types.
609−genericDeclareNamedSchemaUnrestricted :: forall a proxy. (Generic a, GToSchema (Rep a)) =>
610− SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
693+genericDeclareNamedSchemaUnrestricted :: forall a. (Generic a, GToSchema (Rep a)) =>
694+ SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
611695genericDeclareNamedSchemaUnrestricted opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
612696
613−gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
614−gdatatypeSchemaName opts _ = case name of
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))
702+
703+gdatatypeSchemaName :: forall d. Datatype d => SchemaOptions -> Proxy d -> Maybe T.Text
704+gdatatypeSchemaName opts _ = case orig of
615705 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
616706 _ -> Nothing
617707 where
618− name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
708+ orig = datatypeName (Proxy3 :: Proxy3 d f a)
709+ name = datatypeNameModifier opts orig
619710
620711-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
621−paramSchemaToNamedSchema :: forall a d f proxy.
622− (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
623− => SchemaOptions -> proxy a -> NamedSchema
624−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)
625715
626716-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
627−paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
628−paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
717+paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718+paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
629719
630720nullarySchema :: Schema
631721nullarySchema = mempty
632− & type_ .~ SwaggerArray
722+ & type_ ?~ SwaggerArray
633723 & items ?~ SwaggerItemsArray []
634724
635−gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
725+gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
636726gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
637727
638−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
728+gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
639729gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
640730
641731instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -655,7 +745,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
655745 gdeclareNamedSchema = gdeclareNamedSumSchema
656746
657747-- | Single field constructor.
658−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
659749 gdeclareNamedSchema opts _ s
660750 | unwrapUnaryRecords opts = fieldSchema
661751 | otherwise =
@@ -669,7 +759,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
669759 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
670760 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
671761
672−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
762+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
673763gdeclareSchemaRef opts proxy = do
674764 case gtoNamedSchema opts proxy of
675765 NamedSchema (Just name) schema -> do
@@ -700,12 +790,12 @@ withFieldSchema opts _ isRequiredField schema = do
700790 return $
701791 if T.null fname
702792 then schema
703− & type_ .~ SwaggerArray
793+ & type_ ?~ SwaggerArray
704794 & items %~ appendItem ref
705795 & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
706796 & minItems %~ Just . maybe 1 (+1) -- increment minItems
707797 else schema
708− & type_ .~ SwaggerObject
798+ & type_ ?~ SwaggerObject
709799 & properties . at fname ?~ ref
710800 & if isRequiredField
711801 then required %~ (++ [fname])
@@ -733,7 +823,7 @@ instance ( GSumToSchema f
733823 where
734824 gdeclareNamedSchema = gdeclareNamedSumSchema
735825
736−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
826+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
737827gdeclareNamedSumSchema opts proxy s
738828 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
739829 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -742,29 +832,29 @@ gdeclareNamedSumSchema opts proxy s
742832 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
743833
744834 toStringTag schema = mempty
745− & type_ .~ SwaggerString
835+ & type_ ?~ SwaggerString
746836 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
747837
748838type AllNullary = All
749839
750840class GSumToSchema (f :: * -> *) where
751− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
841+ gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
752842
753843instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
754844 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
755845
756−gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
757− 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
758848gsumConToSchemaWith ref opts _ schema = schema
759− & type_ .~ SwaggerObject
849+ & type_ ?~ SwaggerObject
760850 & properties . at tag ?~ ref
761851 & maxProperties ?~ 1
762852 & minProperties ?~ 1
763853 where
764854 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
765855
766−gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
767− 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
768858gsumConToSchema opts proxy schema = do
769859 ref <- gdeclareSchemaRef opts proxy
770860 return $ gsumConToSchemaWith ref opts proxy schema
@@ -788,3 +878,5 @@ data Proxy3 a b c = Proxy3
788878
789879-- $setup
790880-- >>> import Data.Swagger
881+-- >>> import Data.Aeson (encode)
882+-- >>> import Data.Aeson.Types (toJSONKeyText)