git.iko.soy/typeable/swagger2
on master help sign in
Move Definitions type declaration to Data.Swagger.Internal
commitd676cd9301c2949a83c84efd44f6e18b6db6d29f authorCatherine Galkina <catherine@getshoptv.com> dateThu 21 Jan 2016, 16:05 (11 years ago) parenteb46ba7
4 files changed+31−31
src/Data/Swagger.hs+1 src/Data/Swagger/Internal.hs+12−9 src/Data/Swagger/Internal/Schema.hs+18−21 src/Data/Swagger/Schema.hs−1
src/Data/Swagger.hs+1
@@ -45,6 +45,7 @@ module Data.Swagger (
4545 -- ** Types and formats
4646 SwaggerType(..),
4747 Format,
48+ Definitions,
4849 CollectionFormat(..),
4950
5051 -- ** Parameters
src/Data/Swagger/Internal.hs+12−9
@@ -33,6 +33,9 @@ import Text.Read (readMaybe)
3333
3434import Data.Swagger.Internal.Utils
3535
36+-- | A list of definitions that can be used in references.
37+type Definitions = HashMap Text
38+
3639-- | This is the root document object for the API specification.
3740data Swagger = Swagger
3841 { -- | Provides metadata about the API.
@@ -57,25 +60,25 @@ data Swagger = Swagger
5760 , _consumes :: MimeList
5861
5962 -- | A list of MIME types the APIs can produce.
60− -- This is global to all APIs but can be overridden on specific API calls.
63+ -- This is global to all APIs but can be overridden on specific API calls.
6164 , _produces :: MimeList
6265
6366 -- | The available paths and operations for the API.
6467 , _paths :: Paths
6568
6669 -- | An object to hold data types produced and consumed by operations.
67− , _definitions :: HashMap Text Schema
70+ , _definitions :: Definitions Schema
6871
6972 -- | An object to hold parameters that can be used across operations.
7073 -- This property does not define global parameters for all operations.
71− , _parameters :: HashMap Text Param
74+ , _parameters :: Definitions Param
7275
7376 -- | An object to hold responses that can be used across operations.
7477 -- This property does not define global responses for all operations.
75− , _responses :: HashMap Text Response
78+ , _responses :: Definitions Response
7679
7780 -- | Security scheme definitions that can be used across the specification.
78− , _securityDefinitions :: HashMap Text SecurityScheme
81+ , _securityDefinitions :: Definitions SecurityScheme
7982
8083 -- | A declaration of which security schemes are applied for the API as a whole.
8184 -- The list of values describes alternative security schemes that can be used
@@ -1039,10 +1042,10 @@ instance FromJSON Swagger where
10391042 , "produces" .= (mempty :: MimeList)
10401043 , "security" .= ([] :: [SecurityRequirement])
10411044 , "tags" .= ([] :: [Tag])
1042− , "definitions" .= (mempty :: HashMap Text Schema)
1043− , "parameters" .= (mempty :: HashMap Text Param)
1044− , "responses" .= (mempty :: HashMap Text Response)
1045− , "securityDefinitions" .= (mempty :: HashMap Text SecurityScheme)
1045+ , "definitions" .= (mempty :: Definitions Schema)
1046+ , "parameters" .= (mempty :: Definitions Param)
1047+ , "responses" .= (mempty :: Definitions Response)
1048+ , "securityDefinitions" .= (mempty :: Definitions SecurityScheme)
10461049 ] ) js
10471050 parseJSON _ = empty
10481051
src/Data/Swagger/Internal/Schema.hs+18−21
@@ -45,16 +45,13 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
4545import Data.Swagger.Lens
4646import Data.Swagger.SchemaOptions
4747
48−-- | Schema definitions, a mapping from schema name to @'Schema'@.
49−type Definitions = HashMap T.Text Schema
50−
5148unnamed :: Schema -> NamedSchema
5249unnamed schema = NamedSchema Nothing schema
5350
5451named :: T.Text -> Schema -> NamedSchema
5552named name schema = NamedSchema (Just name) schema
5653
57−plain :: Schema -> Declare Definitions NamedSchema
54+plain :: Schema -> Declare (Definitions Schema) NamedSchema
5855plain = pure . unnamed
5956
6057unname :: NamedSchema -> NamedSchema
@@ -110,12 +107,12 @@ class ToSchema a where
110107 -- together with all used definitions.
111108 -- Note that the schema itself is included in definitions
112109 -- only if it is recursive (and thus needs its definition in scope).
113− declareNamedSchema :: proxy a -> Declare Definitions NamedSchema
114− default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare Definitions NamedSchema
110+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
111+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
115112 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
116113
117114-- | Convert a type into a schema and declare all used schema definitions.
118−declareSchema :: ToSchema a => proxy a -> Declare Definitions Schema
115+declareSchema :: ToSchema a => proxy a -> Declare (Definitions Schema) Schema
119116declareSchema = fmap _namedSchemaSchema . declareNamedSchema
120117
121118-- | Convert a type into an optionally named schema.
@@ -170,7 +167,7 @@ toSchemaRef = undeclare . declareSchemaRef
170167-- Schema definitions are typically declared for every referenced schema.
171168-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
172169-- will be declared (regardless of whether it is recusive or not).
173−declareSchemaRef :: ToSchema a => proxy a -> Declare Definitions (Referenced Schema)
170+declareSchemaRef :: ToSchema a => proxy a -> Declare (Definitions Schema) (Referenced Schema)
174171declareSchemaRef proxy = do
175172 case toNamedSchema proxy of
176173 NamedSchema (Just name) schema -> do
@@ -196,7 +193,7 @@ declareSchemaRef proxy = do
196193--
197194-- __WARNING:__ @'inlineSchemasWhen'@ will produce infinite schemas
198195-- when inlining recursive schemas.
199−inlineSchemasWhen :: Data s => (T.Text -> Bool) -> Definitions -> s -> s
196+inlineSchemasWhen :: Data s => (T.Text -> Bool) -> (Definitions Schema) -> s -> s
200197inlineSchemasWhen p defs = template %~ deref
201198 where
202199 deref r@(Ref (Reference name))
@@ -214,7 +211,7 @@ inlineSchemasWhen p defs = template %~ deref
214211--
215212-- __WARNING:__ @'inlineSchemas'@ will produce infinite schemas
216213-- when inlining recursive schemas.
217−inlineSchemas :: Data s => [T.Text] -> Definitions -> s -> s
214+inlineSchemas :: Data s => [T.Text] -> (Definitions Schema) -> s -> s
218215inlineSchemas names = inlineSchemasWhen (`elem` names)
219216
220217-- | Inline all schema references for which the definition
@@ -222,7 +219,7 @@ inlineSchemas names = inlineSchemasWhen (`elem` names)
222219--
223220-- __WARNING:__ @'inlineAllSchemas'@ will produce infinite schemas
224221-- when inlining recursive schemas.
225−inlineAllSchemas :: Data s => Definitions -> s -> s
222+inlineAllSchemas :: Data s => (Definitions Schema) -> s -> s
226223inlineAllSchemas = inlineSchemasWhen (const True)
227224
228225-- | Convert a type into a schema without references.
@@ -239,7 +236,7 @@ toInlinedSchema proxy = inlineAllSchemas defs schema
239236
240237-- | Inline all /non-recursive/ schemas for which the definition
241238-- can be found in @'Definitions'@.
242−inlineNonRecursiveSchemas :: Data s => Definitions -> s -> s
239+inlineNonRecursiveSchemas :: Data s => (Definitions Schema) -> s -> s
243240inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
244241 where
245242 nonRecursive name =
@@ -259,7 +256,7 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
259256 Inline subschema -> usedNames subschema
260257
261258class GToSchema (f :: * -> *) where
262− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
259+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
263260
264261instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a] where
265262 declareNamedSchema _ = do
@@ -389,14 +386,14 @@ genericToNamedSchemaBoundedIntegral opts proxy
389386 = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
390387
391388-- | A configurable generic @'Schema'@ creator.
392−genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare Definitions Schema
389+genericDeclareSchema :: (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) Schema
393390genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
394391
395392-- | A configurable generic @'NamedSchema'@ creator.
396393-- This function applied to @'defaultSchemaOptions'@
397394-- is used as the default for @'declareNamedSchema'@
398395-- when the type is an instance of @'Generic'@.
399−genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare Definitions NamedSchema
396+genericDeclareNamedSchema :: forall a proxy. (Generic a, GToSchema (Rep a)) => SchemaOptions -> proxy a -> Declare (Definitions Schema) NamedSchema
400397genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
401398
402399gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
@@ -425,7 +422,7 @@ nullarySchema = mempty
425422gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
426423gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
427424
428−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare Definitions Schema
425+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
429426gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
430427
431428instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -459,7 +456,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
459456 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
460457 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
461458
462−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definitions (Referenced Schema)
459+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
463460gdeclareSchemaRef opts proxy = do
464461 case gtoNamedSchema opts proxy of
465462 NamedSchema (Just name) schema -> do
@@ -484,7 +481,7 @@ appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (x:xs))
484481appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
485482
486483withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
487− SchemaOptions -> proxy s f -> Bool -> Schema -> Declare Definitions Schema
484+ SchemaOptions -> proxy s f -> Bool -> Schema -> Declare (Definitions Schema) Schema
488485withFieldSchema opts _ isRequiredField schema = do
489486 ref <- gdeclareSchemaRef opts (Proxy :: Proxy f)
490487 return $
@@ -518,7 +515,7 @@ instance {-# OVERLAPPABLE #-} ToSchema c => GToSchema (K1 i c) where
518515instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
519516 gdeclareNamedSchema = gdeclareNamedSumSchema
520517
521−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
518+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
522519gdeclareNamedSumSchema opts proxy s
523520 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
524521 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -533,13 +530,13 @@ gdeclareNamedSumSchema opts proxy s
533530type AllNullary = All
534531
535532class GSumToSchema f where
536− gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare Definitions) Schema
533+ gsumToSchema :: SchemaOptions -> proxy f -> Schema -> WriterT AllNullary (Declare (Definitions Schema)) Schema
537534
538535instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
539536 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) <=< gsumToSchema opts (Proxy :: Proxy g)
540537
541538gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
542− SchemaOptions -> proxy (C1 c f) -> Schema -> Declare Definitions Schema
539+ SchemaOptions -> proxy (C1 c f) -> Schema -> Declare (Definitions Schema) Schema
543540gsumConToSchema opts _ schema = do
544541 ref <- gdeclareSchemaRef opts (Proxy :: Proxy (C1 c f))
545542 return $ schema
src/Data/Swagger/Schema.hs−1
@@ -9,7 +9,6 @@
99module Data.Swagger.Schema (
1010 -- * Encoding
1111 ToSchema(..),
12− Definitions,
1312 declareSchema,
1413 declareSchemaRef,
1514 toSchema,