git.iko.soy/typeable/swagger2
on master help sign in
Move SchemaOptions into a separate module
commitab6029e396d891e3b4db5dfc403c6d54ebae6c97 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateWed 9 Dec 2015, 14:54 (11 years ago) parent7034d20
5 files changed+66−61
src/Data/Swagger/Internal/ParamSchema.hs+8−8 src/Data/Swagger/Internal/Schema.hs+1−53 src/Data/Swagger/Schema.hs+1 src/Data/Swagger/SchemaOptions.hs+55 swagger2.cabal+1
src/Data/Swagger/Internal/ParamSchema.hs+8−8
@@ -22,13 +22,13 @@ import Data.Time
2222import Data.Word
2323
2424import Data.Swagger.Internal
25−import Data.Swagger.Internal.Schema (SchemaOptions(..), defaultSchemaOptions)
2625import Data.Swagger.Lens
26+import Data.Swagger.SchemaOptions
2727
2828class ToParamSchema a where
2929 -- | Convert a type into a non-body parameter schema.
30− toParamSchema :: proxy a -> ParamSchema t Items
31− default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => proxy a -> ParamSchema t Items
30+ toParamSchema :: proxy a -> ParamSchema t i
31+ default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => proxy a -> ParamSchema t i
3232 toParamSchema = genericToParamSchema defaultSchemaOptions
3333
3434instance {-# OVERLAPPING #-} ToParamSchema String where
@@ -52,7 +52,7 @@ instance ToParamSchema Word16 where toParamSchema = toParamSchemaBoundedIntegral
5252instance ToParamSchema Word32 where toParamSchema = toParamSchemaBoundedIntegral
5353instance ToParamSchema Word64 where toParamSchema = toParamSchemaBoundedIntegral
5454
55−toParamSchemaBoundedIntegral :: forall proxy a t. (Bounded a, Integral a) => proxy a -> ParamSchema t Items
55+toParamSchemaBoundedIntegral :: forall proxy a t i. (Bounded a, Integral a) => proxy a -> ParamSchema t i
5656toParamSchemaBoundedIntegral _ = mempty
5757 & schemaType .~ SwaggerInteger
5858 & schemaMinimum ?~ fromInteger (toInteger (minBound :: a))
@@ -73,7 +73,7 @@ instance ToParamSchema Double where
7373instance ToParamSchema Float where
7474 toParamSchema _ = mempty & schemaType .~ SwaggerNumber
7575
76−timeParamSchema :: String -> ParamSchema t Items
76+timeParamSchema :: String -> ParamSchema t i
7777timeParamSchema format = mempty
7878 & schemaType .~ SwaggerString
7979 & schemaFormat ?~ T.pack format
@@ -120,11 +120,11 @@ instance ToParamSchema a => ToParamSchema (First a) where toParamSchema _ = to
120120instance ToParamSchema a => ToParamSchema (Last a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
121121instance ToParamSchema a => ToParamSchema (Dual a) where toParamSchema _ = toParamSchema (Proxy :: Proxy a)
122122
123−genericToParamSchema :: forall proxy a t. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> proxy a -> ParamSchema t Items
123+genericToParamSchema :: forall proxy a t i. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> proxy a -> ParamSchema t i
124124genericToParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy (Rep a)) mempty
125125
126126class GToParamSchema (f :: * -> *) where
127− gtoParamSchema :: SchemaOptions -> proxy f -> ParamSchema t Items -> ParamSchema t Items
127+ gtoParamSchema :: SchemaOptions -> proxy f -> ParamSchema t i -> ParamSchema t i
128128
129129instance GToParamSchema f => GToParamSchema (D1 d f) where
130130 gtoParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy f)
@@ -139,7 +139,7 @@ instance (GEnumParamSchema f, GEnumParamSchema g) => GToParamSchema (f :+: g) wh
139139 gtoParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy (f :+: g))
140140
141141class GEnumParamSchema (f :: * -> *) where
142− genumParamSchema :: SchemaOptions -> proxy f -> ParamSchema t Items -> ParamSchema t Items
142+ genumParamSchema :: SchemaOptions -> proxy f -> ParamSchema t i -> ParamSchema t i
143143
144144instance (GEnumParamSchema f, GEnumParamSchema g) => GEnumParamSchema (f :+: g) where
145145 genumParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy f) . genumParamSchema opts (Proxy :: Proxy g)
src/Data/Swagger/Internal/Schema.hs+1−53
@@ -33,6 +33,7 @@ import GHC.Generics
3333
3434import Data.Swagger.Internal
3535import Data.Swagger.Lens
36+import Data.Swagger.SchemaOptions
3637
3738-- | A @'Schema'@ with an optional name.
3839-- This name can be used in references.
@@ -239,59 +240,6 @@ instance ToSchema a => ToSchema (First a) where toNamedSchema _ = unnamed $ to
239240instance ToSchema a => ToSchema (Last a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
240241instance ToSchema a => ToSchema (Dual a) where toNamedSchema _ = unnamed $ toSchema (Proxy :: Proxy a)
241242
242−-- | Options that specify how to encode your type to Swagger schema.
243−data SchemaOptions = SchemaOptions
244− { -- | Function applied to field labels. Handy for removing common record prefixes for example.
245− fieldLabelModifier :: String -> String
246− -- | Function applied to constructor tags which could be handy for lower-casing them for example.
247− , constructorTagModifier :: String -> String
248− -- | Function applied to datatype name.
249− , datatypeNameModifier :: String -> String
250− -- | If @'True'@ the constructors of a datatype, with all nullary constructors,
251− -- will be encoded to a string enumeration schema with the constructor tags as possible values.
252− , allNullaryToStringTag :: Bool
253− -- | If @'True'@ direct subschemas will be referenced if possible (rather than inlined).
254− -- Note that this option does not influence nested schemas, e.g. for these types
255− --
256− -- @
257− -- data Object = Object String deriving Generic
258− -- instance ToSchema Object
259− --
260− -- newtype Objects = Objects [Object] deriving Generic
261− -- instance ToSchema Objects where
262− -- toNamedSchema = genericToNamedSchema defaultSchemaOptions
263− -- { useReferences = False }
264− -- @
265− --
266− -- Schema for @Objects@ __will not__ inline @Object@ schema because
267− -- it is nested in a @[]@ schema.
268− , useReferences :: Bool
269− -- | Hide the field name when a record constructor has only one field, like a newtype.
270− , unwrapUnaryRecords :: Bool
271− }
272−
273−-- | Default encoding @'SchemaOptions'@.
274−--
275−-- @
276−-- 'SchemaOptions'
277−-- { 'fieldLabelModifier' = id
278−-- , 'constructorTagModifier' = id
279−-- , 'datatypeNameModifier' = id
280−-- , 'allNullaryToStringTag' = True
281−-- , 'useReferences' = True
282−-- , 'unwrapUnaryRecords' = False
283−-- }
284−-- @
285−defaultSchemaOptions :: SchemaOptions
286−defaultSchemaOptions = SchemaOptions
287− { fieldLabelModifier = id
288− , constructorTagModifier = id
289− , datatypeNameModifier = id
290− , allNullaryToStringTag = True
291− , useReferences = True
292− , unwrapUnaryRecords = False
293− }
294−
295243-- | Default schema for @'Bounded'@, @'Integral'@ types.
296244toSchemaBoundedIntegral :: forall a proxy. (Bounded a, Integral a) => proxy a -> Schema
297245toSchemaBoundedIntegral _ = mempty
src/Data/Swagger/Schema.hs+1
@@ -26,3 +26,4 @@ module Data.Swagger.Schema (
2626) where
2727
2828import Data.Swagger.Internal.Schema
29+import Data.Swagger.SchemaOptions
src/Data/Swagger/SchemaOptions.hsnew file+55
@@ -0,0 +1,55 @@
1+module Data.Swagger.SchemaOptions where
2+
3+-- | Options that specify how to encode your type to Swagger schema.
4+data SchemaOptions = SchemaOptions
5+ { -- | Function applied to field labels. Handy for removing common record prefixes for example.
6+ fieldLabelModifier :: String -> String
7+ -- | Function applied to constructor tags which could be handy for lower-casing them for example.
8+ , constructorTagModifier :: String -> String
9+ -- | Function applied to datatype name.
10+ , datatypeNameModifier :: String -> String
11+ -- | If @'True'@ the constructors of a datatype, with all nullary constructors,
12+ -- will be encoded to a string enumeration schema with the constructor tags as possible values.
13+ , allNullaryToStringTag :: Bool
14+ -- | If @'True'@ direct subschemas will be referenced if possible (rather than inlined).
15+ -- Note that this option does not influence nested schemas, e.g. for these types
16+ --
17+ -- @
18+ -- data Object = Object String deriving Generic
19+ -- instance ToSchema Object
20+ --
21+ -- newtype Objects = Objects [Object] deriving Generic
22+ -- instance ToSchema Objects where
23+ -- toNamedSchema = genericToNamedSchema defaultSchemaOptions
24+ -- { useReferences = False }
25+ -- @
26+ --
27+ -- Schema for @Objects@ __will not__ inline @Object@ schema because
28+ -- it is nested in a @[]@ schema.
29+ , useReferences :: Bool
30+ -- | Hide the field name when a record constructor has only one field, like a newtype.
31+ , unwrapUnaryRecords :: Bool
32+ }
33+
34+-- | Default encoding @'SchemaOptions'@.
35+--
36+-- @
37+-- 'SchemaOptions'
38+-- { 'fieldLabelModifier' = id
39+-- , 'constructorTagModifier' = id
40+-- , 'datatypeNameModifier' = id
41+-- , 'allNullaryToStringTag' = True
42+-- , 'useReferences' = True
43+-- , 'unwrapUnaryRecords' = False
44+-- }
45+-- @
46+defaultSchemaOptions :: SchemaOptions
47+defaultSchemaOptions = SchemaOptions
48+ { fieldLabelModifier = id
49+ , constructorTagModifier = id
50+ , datatypeNameModifier = id
51+ , allNullaryToStringTag = True
52+ , useReferences = True
53+ , unwrapUnaryRecords = False
54+ }
55+
swagger2.cabal+1
@@ -22,6 +22,7 @@ library
2222 Data.Swagger
2323 Data.Swagger.Lens
2424 Data.Swagger.Schema
25+ Data.Swagger.SchemaOptions
2526
2627 -- internal modules
2728 Data.Swagger.Internal