@@ -32,7 +32,7 @@ import Data.Data.Lens (template)
3232
3333import Control.Monad
3434import Control.Monad.Writer
35−import Data.Aeson (ToJSON (..), ToJSONKey (..), ToJSONKeyFunction (..), Value (..), Object(..))
35+import Data.Aeson
3636import Data.Char
3737import Data.Data (Data)
3838import Data.Foldable (traverse_)
@@ -44,11 +44,9 @@ import qualified Data.HashMap.Strict.InsOrd as InsOrdHashMap
4444import Data.Int
4545import Data.IntSet (IntSet)
4646import Data.IntMap (IntMap)
47−import Data.List.NonEmpty.Compat (NonEmpty)
4847import Data.Map (Map)
4948import Data.Proxy
5049import Data.Scientific (Scientific)
51−import Data.Fixed (Fixed, HasResolution, Pico)
5250import Data.Set (Set)
5351import qualified Data.Text as T
5452import qualified Data.Text.Lazy as TL
@@ -57,8 +55,6 @@ import qualified Data.Vector as V
5755import qualified Data.Vector.Primitive as VP
5856import qualified Data.Vector.Storable as VS
5957import qualified Data.Vector.Unboxed as VU
60−import Data.Version (Version)
61−import Numeric.Natural.Compat (Natural)
6258import Data.Word
6359import GHC.Generics
6460import qualified Data.UUID.Types as UUID
@@ -111,7 +107,7 @@ rename name (NamedSchema _ schema) = NamedSchema name schema
111107-- declareNamedSchema _ = do
112108-- doubleSchema <- declareSchemaRef (Proxy :: Proxy Double)
113109-- return $ NamedSchema (Just \"Coord\") $ mempty
114−-- & type_ ?~ SwaggerObject
110+-- & type_ .~ SwaggerObject
115111-- & properties .~
116112-- [ (\"x\", doubleSchema)
117113-- , (\"y\", doubleSchema)
@@ -142,17 +138,13 @@ class ToSchema a where
142138 -- together with all used definitions.
143139 -- Note that the schema itself is included in definitions
144140 -- only if it is recursive (and thus needs its definition in scope).
145− declareNamedSchema :: Proxy a -> Declare (Definitions Schema) NamedSchema
141+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
146142 default declareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
147− Proxy a -> Declare (Definitions Schema) NamedSchema
143+ proxy a -> Declare (Definitions Schema) NamedSchema
148144 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
149145
150−instance ToSchema TimeOfDay where
151− declareNamedSchema _ = pure $ named "TimeOfDay" $ timeSchema "hh:MM:ss"
152− & example ?~ toJSON (TimeOfDay 12 33 15)
153−
154146-- | Convert a type into a schema and declare all used schema definitions.
155−declareSchema :: ToSchema a => Proxy a -> Declare (Definitions Schema) Schema
147+declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
156148declareSchema = fmap _namedSchemaSchema . declareNamedSchema
157149
158150-- | Convert a type into an optionally named schema.
@@ -166,7 +158,7 @@ declareSchema = fmap _namedSchemaSchema . declareNamedSchema
166158-- Just "Day"
167159-- >>> encode (toNamedSchema (Proxy :: Proxy Day) ^. schema)
168160-- "{\"example\":\"2016-07-22\",\"format\":\"date\",\"type\":\"string\"}"
169−toNamedSchema :: ToSchema a => Proxy a -> NamedSchema
161+toNamedSchema :: ToSchema a => proxy a -> NamedSchema
170162toNamedSchema = undeclare . declareNamedSchema
171163
172164-- | Get type's schema name according to its @'ToSchema'@ instance.
@@ -176,7 +168,7 @@ toNamedSchema = undeclare . declareNamedSchema
176168--
177169-- >>> schemaName (Proxy :: Proxy UTCTime)
178170-- Just "UTCTime"
179−schemaName :: ToSchema a => Proxy a -> Maybe T.Text
171+schemaName :: ToSchema a => proxy a -> Maybe T.Text
180172schemaName = _namedSchemaName . toNamedSchema
181173
182174-- | Convert a type into a schema.
@@ -186,7 +178,7 @@ schemaName = _namedSchemaName . toNamedSchema
186178--
187179-- >>> encode $ toSchema (Proxy :: Proxy [Day])
188180-- "{\"items\":{\"$ref\":\"#/definitions/Day\"},\"type\":\"array\"}"
189−toSchema :: ToSchema a => Proxy a -> Schema
181+toSchema :: ToSchema a => proxy a -> Schema
190182toSchema = _namedSchemaSchema . toNamedSchema
191183
192184-- | Convert a type into a referenced schema if possible.
@@ -197,7 +189,7 @@ toSchema = _namedSchemaSchema . toNamedSchema
197189--
198190-- >>> encode $ toSchemaRef (Proxy :: Proxy Day)
199191-- "{\"$ref\":\"#/definitions/Day\"}"
200−toSchemaRef :: ToSchema a => Proxy a -> Referenced Schema
192+toSchemaRef :: ToSchema a => proxy a -> Referenced Schema
201193toSchemaRef = undeclare . declareSchemaRef
202194
203195-- | Convert a type into a referenced schema if possible
@@ -207,7 +199,7 @@ toSchemaRef = undeclare . declareSchemaRef
207199-- Schema definitions are typically declared for every referenced schema.
208200-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
209201-- will be declared (regardless of whether it is recusive or not).
210−declareSchemaRef :: ToSchema a => Proxy a -> Declare (Definitions Schema) (Referenced Schema)
202+declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
211203declareSchemaRef proxy = do
212204 case toNamedSchema proxy of
213205 NamedSchema (Just name) schema -> do
@@ -269,7 +261,7 @@ inlineAllSchemas = inlineSchemasWhen (const True)
269261--
270262-- __WARNING:__ @'toInlinedSchema'@ will produce infinite schema
271263-- when inlining recursive schemas.
272−toInlinedSchema :: ToSchema a => Proxy a -> Schema
264+toInlinedSchema :: ToSchema a => proxy a -> Schema
273265toInlinedSchema proxy = inlineAllSchemas defs schema
274266 where
275267 (defs, schema) = runDeclare (declareSchema proxy) mempty
@@ -298,20 +290,20 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
298290-- | Default schema for binary data (any sequence of octets).
299291binarySchema :: Schema
300292binarySchema = mempty
301− & type_ ?~ SwaggerString
293+ & type_ .~ SwaggerString
302294 & format ?~ "binary"
303295
304296-- | Default schema for binary data (base64 encoded).
305297byteSchema :: Schema
306298byteSchema = mempty
307− & type_ ?~ SwaggerString
299+ & type_ .~ SwaggerString
308300 & format ?~ "byte"
309301
310302-- | Default schema for password string.
311303-- @"password"@ format is used to hint UIs the input needs to be obscured.
312304passwordSchema :: Schema
313305passwordSchema = mempty
314− & type_ ?~ SwaggerString
306+ & type_ .~ SwaggerString
315307 & format ?~ "password"
316308
317309-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
@@ -337,12 +329,12 @@ sketchSchema = sketch . toJSON
337329 sketch js@(Bool _) = go js
338330 sketch js = go js & example ?~ js
339331
340− go Null = mempty & type_ ?~ SwaggerNull
341− go (Bool _) = mempty & type_ ?~ SwaggerBoolean
342− go (String _) = mempty & type_ ?~ SwaggerString
343− go (Number _) = mempty & type_ ?~ SwaggerNumber
332+ go Null = mempty & type_ .~ SwaggerNull
333+ go (Bool _) = mempty & type_ .~ SwaggerBoolean
334+ go (String _) = mempty & type_ .~ SwaggerString
335+ go (Number _) = mempty & type_ .~ SwaggerNumber
344336 go (Array xs) = mempty
345− & type_ ?~ SwaggerArray
337+ & type_ .~ SwaggerArray
346338 & items ?~ case ischema of
347339 Just s -> SwaggerItemsObject (Inline s)
348340 _ -> SwaggerItemsArray (map Inline ys)
@@ -354,7 +346,7 @@ sketchSchema = sketch . toJSON
354346 (z:_) | allSame -> Just z
355347 _ -> Nothing
356348 go (Object o) = mempty
357− & type_ ?~ SwaggerObject
349+ & type_ .~ SwaggerObject
358350 & required .~ HashMap.keys o
359351 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
360352
@@ -377,24 +369,24 @@ sketchSchema = sketch . toJSON
377369sketchStrictSchema :: ToJSON a => a -> Schema
378370sketchStrictSchema = go . toJSON
379371 where
380− go Null = mempty & type_ ?~ SwaggerNull
372+ go Null = mempty & type_ .~ SwaggerNull
381373 go js@(Bool _) = mempty
382− & type_ ?~ SwaggerBoolean
374+ & type_ .~ SwaggerBoolean
383375 & enum_ ?~ [js]
384376 go js@(String s) = mempty
385− & type_ ?~ SwaggerString
377+ & type_ .~ SwaggerString
386378 & maxLength ?~ fromIntegral (T.length s)
387379 & minLength ?~ fromIntegral (T.length s)
388380 & pattern ?~ s
389381 & enum_ ?~ [js]
390382 go js@(Number n) = mempty
391− & type_ ?~ SwaggerNumber
383+ & type_ .~ SwaggerNumber
392384 & maximum_ ?~ n
393385 & minimum_ ?~ n
394386 & multipleOf ?~ n
395387 & enum_ ?~ [js]
396388 go js@(Array xs) = mempty
397− & type_ ?~ SwaggerArray
389+ & type_ .~ SwaggerArray
398390 & maxItems ?~ fromIntegral sz
399391 & minItems ?~ fromIntegral sz
400392 & items ?~ SwaggerItemsArray (map (Inline . go) (V.toList xs))
@@ -404,7 +396,7 @@ sketchStrictSchema = go . toJSON
404396 sz = length xs
405397 allUnique = sz == HashSet.size (HashSet.fromList (V.toList xs))
406398 go js@(Object o) = mempty
407− & type_ ?~ SwaggerObject
399+ & type_ .~ SwaggerObject
408400 & required .~ names
409401 & properties .~ fmap (Inline . go) (InsOrdHashMap.fromHashMap o)
410402 & maxProperties ?~ fromIntegral (length names)
@@ -414,19 +406,18 @@ sketchStrictSchema = go . toJSON
414406 names = HashMap.keys o
415407
416408class GToSchema (f :: * -> *) where
417− gdeclareNamedSchema :: SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
409+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
418410
419411instance OVERLAPPABLE_ ToSchema a => ToSchema [a] where
420412 declareNamedSchema _ = do
421413 ref <- declareSchemaRef (Proxy :: Proxy a)
422414 return $ unnamed $ mempty
423− & type_ ?~ SwaggerArray
415+ & type_ .~ SwaggerArray
424416 & items ?~ SwaggerItemsObject ref
425417
426418instance OVERLAPPING_ ToSchema String where declareNamedSchema = plain . paramSchemaToSchema
427419instance ToSchema Bool where declareNamedSchema = plain . paramSchemaToSchema
428420instance ToSchema Integer where declareNamedSchema = plain . paramSchemaToSchema
429−instance ToSchema Natural where declareNamedSchema = plain . paramSchemaToSchema
430421instance ToSchema Int where declareNamedSchema = plain . paramSchemaToSchema
431422instance ToSchema Int8 where declareNamedSchema = plain . paramSchemaToSchema
432423instance ToSchema Int16 where declareNamedSchema = plain . paramSchemaToSchema
@@ -446,8 +437,6 @@ instance ToSchema Scientific where declareNamedSchema = plain . paramSchemaToSc
446437instance ToSchema Double where declareNamedSchema = plain . paramSchemaToSchema
447438instance ToSchema Float where declareNamedSchema = plain . paramSchemaToSchema
448439
449−instance HasResolution a => ToSchema (Fixed a) where declareNamedSchema = plain . paramSchemaToSchema
450−
451440instance ToSchema a => ToSchema (Maybe a) where
452441 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy a)
453442
@@ -470,7 +459,7 @@ instance (ToSchema a, ToSchema b, ToSchema c, ToSchema d, ToSchema e, ToSchema f
470459
471460timeSchema :: T.Text -> Schema
472461timeSchema fmt = mempty
473− & type_ ?~ SwaggerString
462+ & type_ .~ SwaggerString
474463 & format ?~ fmt
475464
476465-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
@@ -491,7 +480,7 @@ instance ToSchema ZonedTime where
491480 & example ?~ toJSON (ZonedTime (LocalTime (fromGregorian 2016 7 22) (TimeOfDay 7 40 0)) (hoursToTimeZone 3))
492481
493482instance ToSchema NominalDiffTime where
494− declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Pico)
483+ declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy Integer)
495484
496485-- |
497486-- >>> toSchema (Proxy :: Proxy UTCTime) ^. format
@@ -503,8 +492,6 @@ instance ToSchema UTCTime where
503492instance ToSchema T.Text where declareNamedSchema = plain . paramSchemaToSchema
504493instance ToSchema TL.Text where declareNamedSchema = plain . paramSchemaToSchema
505494
506−instance ToSchema Version where declareNamedSchema = plain . paramSchemaToSchema
507−
508495#if __GLASGOW_HASKELL__ < 800
509496#else
510497type family ToSchemaByteStringError bs where
@@ -532,8 +519,8 @@ instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (Map k v) where
532519 declareObjectMapSchema = do
533520 schema <- declareSchemaRef (Proxy :: Proxy v)
534521 return $ unnamed $ mempty
535− & type_ ?~ SwaggerObject
536− & additionalProperties ?~ AdditionalPropertiesSchema schema
522+ & type_ .~ SwaggerObject
523+ & additionalProperties ?~ schema
537524
538525instance (ToJSONKey k, ToSchema k, ToSchema v) => ToSchema (HashMap k v) where
539526 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map k v))
@@ -544,7 +531,7 @@ instance ToSchema a => ToSchema (Map String a) where
544531 declareNamedSchema _ = do
545532 schema <- declareSchemaRef (Proxy :: Proxy a)
546533 return $ unnamed $ mempty
547− & type_ ?~ SwaggerObject
534+ & type_ .~ SwaggerObject
548535 & additionalProperties ?~ schema
549536
550537instance ToSchema a => ToSchema (Map T.Text a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Map String a))
@@ -556,12 +543,6 @@ instance ToSchema a => ToSchema (HashMap TL.Text a) where declareNamedSchema _ =
556543
557544#endif
558545
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−
565546instance ToSchema a => ToSchema (V.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
566547instance ToSchema a => ToSchema (VU.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
567548instance ToSchema a => ToSchema (VS.Vector a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy [a])
@@ -575,13 +556,6 @@ instance ToSchema a => ToSchema (Set a) where
575556
576557instance ToSchema a => ToSchema (HashSet a) where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy (Set a))
577558
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−
585559instance ToSchema All where declareNamedSchema = plain . paramSchemaToSchema
586560instance ToSchema Any where declareNamedSchema = plain . paramSchemaToSchema
587561
@@ -597,135 +571,74 @@ instance ToSchema a => ToSchema (Identity a) where declareNamedSchema _ = declar
597571--
598572-- >>> encode $ toSchemaBoundedIntegral (Proxy :: Proxy Int16)
599573-- "{\"maximum\":32767,\"minimum\":-32768,\"type\":\"integer\"}"
600−toSchemaBoundedIntegral :: forall a. (Bounded a, Integral a) => Proxy a -> Schema
574+toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
601575toSchemaBoundedIntegral _ = mempty
602− & type_ ?~ SwaggerInteger
576+ & type_ .~ SwaggerInteger
603577 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
604578 & maximum_ ?~ fromInteger (toInteger (maxBound :: a))
605579
606580-- | Default generic named schema for @'Bounded'@, @'Integral'@ types.
607−genericToNamedSchemaBoundedIntegral :: forall a d f.
581+genericToNamedSchemaBoundedIntegral :: forall a d f proxy.
608582 ( Bounded a, Integral a
609583 , Generic a, Rep a ~ D1 d f, Datatype d)
610− => SchemaOptions -> Proxy a -> NamedSchema
584+ => SchemaOptions -> proxy a -> NamedSchema
611585genericToNamedSchemaBoundedIntegral opts proxy
612− = genericNameSchema opts proxy (toSchemaBoundedIntegral proxy)
613−
614−-- | Declare a named schema for a @newtype@ wrapper.
615−genericDeclareNamedSchemaNewtype :: forall a d c s i inner.
616− (Generic a, Datatype d, Rep a ~ D1 d (C1 c (S1 s (K1 i inner))))
617− => SchemaOptions -- ^ How to derive the name.
618− -> (Proxy inner -> Declare (Definitions Schema) Schema) -- ^ How to create a schema for the wrapped type.
619− -> Proxy a
620− -> Declare (Definitions Schema) NamedSchema
621−genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$> f (Proxy :: Proxy inner)
622−
623−-- | Declare 'Schema' for a mapping with 'Bounded' 'Enum' keys.
624−-- This makes a much more useful schema when there aren't many options for key values.
625−--
626−-- >>> data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
627−-- >>> instance ToJSON ButtonState
628−-- >>> instance ToSchema ButtonState
629−-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
630−-- >>> type ImageUrl = T.Text
631−-- >>> encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
632−-- "{\"properties\":{\"Neutral\":{\"type\":\"string\"},\"Focus\":{\"type\":\"string\"},\"Active\":{\"type\":\"string\"},\"Hover\":{\"type\":\"string\"},\"Disabled\":{\"type\":\"string\"}},\"type\":\"object\"}"
633−--
634−-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
635−-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
636−declareSchemaBoundedEnumKeyMapping :: forall map key value.
637− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
638− => Proxy (map key value) -> Declare (Definitions Schema) Schema
639−declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key of
640− ToJSONKeyText keyToText _ -> objectSchema keyToText
641− ToJSONKeyValue _ _ -> declareSchema (Proxy :: Proxy [(key, value)])
642− where
643− objectSchema keyToText = do
644− valueRef <- declareSchemaRef (Proxy :: Proxy value)
645− let allKeys = [minBound..maxBound :: key]
646− mkPair k = (keyToText k, valueRef)
647− return $ mempty
648− & type_ ?~ SwaggerObject
649− & properties .~ InsOrdHashMap.fromList (map mkPair allKeys)
650−
651−-- | A 'Schema' for a mapping with 'Bounded' 'Enum' keys.
652−-- This makes a much more useful schema when there aren't many options for key values.
653−--
654−-- >>> data ButtonState = Neutral | Focus | Active | Hover | Disabled deriving (Show, Bounded, Enum, Generic)
655−-- >>> instance ToJSON ButtonState
656−-- >>> instance ToSchema ButtonState
657−-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
658−-- >>> type ImageUrl = T.Text
659−-- >>> encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
660−-- "{\"properties\":{\"Neutral\":{\"type\":\"string\"},\"Focus\":{\"type\":\"string\"},\"Active\":{\"type\":\"string\"},\"Hover\":{\"type\":\"string\"},\"Disabled\":{\"type\":\"string\"}},\"type\":\"object\"}"
661−--
662−-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
663−-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
664−toSchemaBoundedEnumKeyMapping :: forall map key value.
665− (Bounded key, Enum key, ToJSONKey key, ToSchema key, ToSchema value)
666− => Proxy (map key value) -> Schema
667−toSchemaBoundedEnumKeyMapping = flip evalDeclare mempty . declareSchemaBoundedEnumKeyMapping
586+ = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
668587
669588-- | A configurable generic @'Schema'@ creator.
670589genericDeclareSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareSchemaUnrestricted") =>
671− SchemaOptions -> Proxy a -> Declare (Definitions Schema) Schema
590+ SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
672591genericDeclareSchema = genericDeclareSchemaUnrestricted
673592
674593-- | A configurable generic @'NamedSchema'@ creator.
675594-- This function applied to @'defaultSchemaOptions'@
676595-- is used as the default for @'declareNamedSchema'@
677596-- when the type is an instance of @'Generic'@.
678−genericDeclareNamedSchema :: (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
679− SchemaOptions -> Proxy a -> Declare (Definitions Schema) NamedSchema
597+genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") =>
598+ SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
680599genericDeclareNamedSchema = genericDeclareNamedSchemaUnrestricted
681600
682601-- | A configurable generic @'Schema'@ creator.
683602--
684603-- Unlike 'genericDeclareSchema' also works for mixed sum types.
685604-- 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
605+genericDeclareSchemaUnrestricted :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
687606genericDeclareSchemaUnrestricted opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchemaUnrestricted opts proxy
688607
689608-- | A configurable generic @'NamedSchema'@ creator.
690609--
691610-- Unlike 'genericDeclareNamedSchema' also works for mixed sum types.
692611-- 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
612+genericDeclareNamedSchemaUnrestricted :: forall a proxy. (Generic a, GToSchema (Rep a)) =>
613+ SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
695614genericDeclareNamedSchemaUnrestricted opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
696615
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
616+gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
617+gdatatypeSchemaName opts _ = case name of
705618 (c:_) | isAlpha c && isUpper c -> Just (T.pack name)
706619 _ -> Nothing
707620 where
708− orig = datatypeName (Proxy3 :: Proxy3 d f a)
709− name = datatypeNameModifier opts orig
621+ name = datatypeNameModifier opts (datatypeName (Proxy3 :: Proxy3 d f a))
710622
711623-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
712−paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) =>
713− SchemaOptions -> Proxy a -> NamedSchema
714−paramSchemaToNamedSchema opts proxy = genericNameSchema opts proxy (paramSchemaToSchema proxy)
624+paramSchemaToNamedSchema :: forall a d f proxy.
625+ (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d)
626+ => SchemaOptions -> proxy a -> NamedSchema
627+paramSchemaToNamedSchema opts proxy = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (paramSchemaToSchema proxy)
715628
716629-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
717−paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
718−paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
630+paramSchemaToSchema :: forall a proxy. ToParamSchema a => proxy a -> Schema
631+paramSchemaToSchema _ = mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a)
719632
720633nullarySchema :: Schema
721634nullarySchema = mempty
722− & type_ ?~ SwaggerArray
635+ & type_ .~ SwaggerArray
723636 & items ?~ SwaggerItemsArray []
724637
725−gtoNamedSchema :: GToSchema f => SchemaOptions -> Proxy f -> NamedSchema
638+gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
726639gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
727640
728−gdeclareSchema :: GToSchema f => SchemaOptions -> Proxy f -> Declare (Definitions Schema) Schema
641+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
729642gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
730643
731644instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -745,7 +658,7 @@ instance OVERLAPPING_ Constructor c => GToSchema (C1 c U1) where
745658 gdeclareNamedSchema = gdeclareNamedSumSchema
746659
747660-- | Single field constructor.
748−instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s f)) where
661+instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
749662 gdeclareNamedSchema opts _ s
750663 | unwrapUnaryRecords opts = fieldSchema
751664 | otherwise =
@@ -759,7 +672,7 @@ instance (Selector s, GToSchema f, GToSchema (S1 s f)) => GToSchema (C1 c (S1 s
759672 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
760673 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
761674
762−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> Proxy a -> Declare (Definitions Schema) (Referenced Schema)
675+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
763676gdeclareSchemaRef opts proxy = do
764677 case gtoNamedSchema opts proxy of
765678 NamedSchema (Just name) schema -> do
@@ -790,12 +703,12 @@ withFieldSchema opts _ isRequiredField schema = do
790703 return $
791704 if T.null fname
792705 then schema
793− & type_ ?~ SwaggerArray
706+ & type_ .~ SwaggerArray
794707 & items %~ appendItem ref
795708 & maxItems %~ Just . maybe 1 (+1) -- increment maxItems
796709 & minItems %~ Just . maybe 1 (+1) -- increment minItems
797710 else schema
798− & type_ ?~ SwaggerObject
711+ & type_ .~ SwaggerObject
799712 & properties . at fname ?~ ref
800713 & if isRequiredField
801714 then required %~ (++ [fname])
@@ -823,7 +736,7 @@ instance ( GSumToSchema f
823736 where
824737 gdeclareNamedSchema = gdeclareNamedSumSchema
825738
826−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> Proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
739+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
827740gdeclareNamedSumSchema opts proxy s
828741 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
829742 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -832,29 +745,29 @@ gdeclareNamedSumSchema opts proxy s
832745 (sumSchema, All allNullary) = undeclare (runWriterT declareSumSchema)
833746
834747 toStringTag schema = mempty
835− & type_ ?~ SwaggerString
748+ & type_ .~ SwaggerString
836749 & enum_ ?~ map toJSON (schema ^.. properties.ifolded.asIndex)
837750
838751type AllNullary = All
839752
840753class GSumToSchema (f :: * -> *) where
841− gsumToSchema :: SchemaOptions -> Proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
754+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
842755
843756instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
844757 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) >=> gsumToSchema opts (Proxy :: Proxy g)
845758
846−gsumConToSchemaWith :: forall c f. (GToSchema (C1 c f), Constructor c) =>
847− Referenced Schema -> SchemaOptions -> Proxy (C1 c f) -> Schema -> Schema
759+gsumConToSchemaWith :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
760+ Referenced Schema -> SchemaOptions -> proxy (C1 c f) -> Schema -> Schema
848761gsumConToSchemaWith ref opts _ schema = schema
849− & type_ ?~ SwaggerObject
762+ & type_ .~ SwaggerObject
850763 & properties . at tag ?~ ref
851764 & maxProperties ?~ 1
852765 & minProperties ?~ 1
853766 where
854767 tag = T.pack (constructorTagModifier opts (conName (Proxy3 :: Proxy3 c f p)))
855768
856−gsumConToSchema :: (GToSchema (C1 c f), Constructor c) =>
857− SchemaOptions -> Proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
769+gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
770+ SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
858771gsumConToSchema opts proxy schema = do
859772 ref <- gdeclareSchemaRef opts proxy
860773 return $ gsumConToSchemaWith ref opts proxy schema
@@ -878,5 +791,3 @@ data Proxy3 a b c = Proxy3
878791
879792-- $setup
880793-- >>> import Data.Swagger
881−-- >>> import Data.Aeson (encode)
882−-- >>> import Data.Aeson.Types (toJSONKeyText)