git.iko.soy/typeable/openapi3
on master help sign in
Move Definitions type declaration to Data.Swagger.Internal
d676cd9Catherine Galkina
src/Data/Swagger.hs+1
@@ -45,6 +45,7 @@ module Data.Swagger (
45 -- ** Types and formats
45 -- ** Types and formats
46 SwaggerType(..),
46 SwaggerType(..),
47 Format,
47 Format,
48+ Definitions,
48 CollectionFormat(..),
49 CollectionFormat(..),
49
50
50 -- ** Parameters
51 -- ** Parameters
src/Data/Swagger/Internal.hs+12−9
@@ -33,6 +33,9 @@ import Text.Read (readMaybe)
33
33
34import Data.Swagger.Internal.Utils
34import Data.Swagger.Internal.Utils
35
35
36+-- | A list of definitions that can be used in references.
37+type Definitions = HashMap Text
38+
36-- | This is the root document object for the API specification.
39-- | This is the root document object for the API specification.
37data Swagger = Swagger
40data Swagger = Swagger
38 { -- | Provides metadata about the API.
41 { -- | Provides metadata about the API.
@@ -57,25 +60,25 @@ data Swagger = Swagger
57 , _consumes :: MimeList
60 , _consumes :: MimeList
58
61
59 -- | A list of MIME types the APIs can produce.
62 -- | 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.
61 , _produces :: MimeList
64 , _produces :: MimeList
62
65
63 -- | The available paths and operations for the API.
66 -- | The available paths and operations for the API.
64 , _paths :: Paths
67 , _paths :: Paths
65
68
66 -- | An object to hold data types produced and consumed by operations.
69 -- | An object to hold data types produced and consumed by operations.
67− , _definitions :: HashMap Text Schema
70+ , _definitions :: Definitions Schema
68
71
69 -- | An object to hold parameters that can be used across operations.
72 -- | An object to hold parameters that can be used across operations.
70 -- This property does not define global parameters for all operations.
73 -- This property does not define global parameters for all operations.
71− , _parameters :: HashMap Text Param
74+ , _parameters :: Definitions Param
72
75
73 -- | An object to hold responses that can be used across operations.
76 -- | An object to hold responses that can be used across operations.
74 -- This property does not define global responses for all operations.
77 -- This property does not define global responses for all operations.
75− , _responses :: HashMap Text Response
78+ , _responses :: Definitions Response
76
79
77 -- | Security scheme definitions that can be used across the specification.
80 -- | Security scheme definitions that can be used across the specification.
78− , _securityDefinitions :: HashMap Text SecurityScheme
81+ , _securityDefinitions :: Definitions SecurityScheme
79
82
80 -- | A declaration of which security schemes are applied for the API as a whole.
83 -- | A declaration of which security schemes are applied for the API as a whole.
81 -- The list of values describes alternative security schemes that can be used
84 -- The list of values describes alternative security schemes that can be used
@@ -1039,10 +1042,10 @@ instance FromJSON Swagger where
1039 , "produces" .= (mempty :: MimeList)
1042 , "produces" .= (mempty :: MimeList)
1040 , "security" .= ([] :: [SecurityRequirement])
1043 , "security" .= ([] :: [SecurityRequirement])
1041 , "tags" .= ([] :: [Tag])
1044 , "tags" .= ([] :: [Tag])
1042− , "definitions" .= (mempty :: HashMap Text Schema)
1045+ , "definitions" .= (mempty :: Definitions Schema)
1043− , "parameters" .= (mempty :: HashMap Text Param)
1046+ , "parameters" .= (mempty :: Definitions Param)
1044− , "responses" .= (mempty :: HashMap Text Response)
1047+ , "responses" .= (mempty :: Definitions Response)
1045− , "securityDefinitions" .= (mempty :: HashMap Text SecurityScheme)
1048+ , "securityDefinitions" .= (mempty :: Definitions SecurityScheme)
1046 ] ) js
1049 ] ) js
1047 parseJSON _ = empty
1050 parseJSON _ = empty
1048
1051
src/Data/Swagger/Internal/Schema.hs+18−21
@@ -45,16 +45,13 @@ import Data.Swagger.Internal.ParamSchema (ToParamSchema(..))
45import Data.Swagger.Lens
45import Data.Swagger.Lens
46import Data.Swagger.SchemaOptions
46import Data.Swagger.SchemaOptions
47
47
48−-- | Schema definitions, a mapping from schema name to @'Schema'@.
49−type Definitions = HashMap T.Text Schema
50−
51unnamed :: Schema -> NamedSchema
48unnamed :: Schema -> NamedSchema
52unnamed schema = NamedSchema Nothing schema
49unnamed schema = NamedSchema Nothing schema
53
50
54named :: T.Text -> Schema -> NamedSchema
51named :: T.Text -> Schema -> NamedSchema
55named name schema = NamedSchema (Just name) schema
52named name schema = NamedSchema (Just name) schema
56
53
57−plain :: Schema -> Declare Definitions NamedSchema
54+plain :: Schema -> Declare (Definitions Schema) NamedSchema
58plain = pure . unnamed
55plain = pure . unnamed
59
56
60unname :: NamedSchema -> NamedSchema
57unname :: NamedSchema -> NamedSchema
@@ -110,12 +107,12 @@ class ToSchema a where
110 -- together with all used definitions.
107 -- together with all used definitions.
111 -- Note that the schema itself is included in definitions
108 -- Note that the schema itself is included in definitions
112 -- only if it is recursive (and thus needs its definition in scope).
109 -- only if it is recursive (and thus needs its definition in scope).
113− declareNamedSchema :: proxy a -> Declare Definitions NamedSchema
110+ declareNamedSchema :: proxy a -> Declare (Definitions Schema) NamedSchema
114− default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare Definitions NamedSchema
111+ default declareNamedSchema :: (Generic a, GToSchema (Rep a)) => proxy a -> Declare (Definitions Schema) NamedSchema
115 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
112 declareNamedSchema = genericDeclareNamedSchema defaultSchemaOptions
116
113
117-- | Convert a type into a schema and declare all used schema definitions.
114-- | 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
119declareSchema = fmap _namedSchemaSchema . declareNamedSchema
116declareSchema = fmap _namedSchemaSchema . declareNamedSchema
120
117
121-- | Convert a type into an optionally named schema.
118-- | Convert a type into an optionally named schema.
@@ -170,7 +167,7 @@ toSchemaRef = undeclare . declareSchemaRef
170-- Schema definitions are typically declared for every referenced schema.
167-- Schema definitions are typically declared for every referenced schema.
171-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
168-- If @'declareSchemaRef'@ returns a reference, a corresponding schema
172-- will be declared (regardless of whether it is recusive or not).
169-- 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)
174declareSchemaRef proxy = do
171declareSchemaRef proxy = do
175 case toNamedSchema proxy of
172 case toNamedSchema proxy of
176 NamedSchema (Just name) schema -> do
173 NamedSchema (Just name) schema -> do
@@ -196,7 +193,7 @@ declareSchemaRef proxy = do
196--
193--
197-- __WARNING:__ @'inlineSchemasWhen'@ will produce infinite schemas
194-- __WARNING:__ @'inlineSchemasWhen'@ will produce infinite schemas
198-- when inlining recursive schemas.
195-- 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
200inlineSchemasWhen p defs = template %~ deref
197inlineSchemasWhen p defs = template %~ deref
201 where
198 where
202 deref r@(Ref (Reference name))
199 deref r@(Ref (Reference name))
@@ -214,7 +211,7 @@ inlineSchemasWhen p defs = template %~ deref
214--
211--
215-- __WARNING:__ @'inlineSchemas'@ will produce infinite schemas
212-- __WARNING:__ @'inlineSchemas'@ will produce infinite schemas
216-- when inlining recursive schemas.
213-- when inlining recursive schemas.
217−inlineSchemas :: Data s => [T.Text] -> Definitions -> s -> s
214+inlineSchemas :: Data s => [T.Text] -> (Definitions Schema) -> s -> s
218inlineSchemas names = inlineSchemasWhen (`elem` names)
215inlineSchemas names = inlineSchemasWhen (`elem` names)
219
216
220-- | Inline all schema references for which the definition
217-- | Inline all schema references for which the definition
@@ -222,7 +219,7 @@ inlineSchemas names = inlineSchemasWhen (`elem` names)
222--
219--
223-- __WARNING:__ @'inlineAllSchemas'@ will produce infinite schemas
220-- __WARNING:__ @'inlineAllSchemas'@ will produce infinite schemas
224-- when inlining recursive schemas.
221-- when inlining recursive schemas.
225−inlineAllSchemas :: Data s => Definitions -> s -> s
222+inlineAllSchemas :: Data s => (Definitions Schema) -> s -> s
226inlineAllSchemas = inlineSchemasWhen (const True)
223inlineAllSchemas = inlineSchemasWhen (const True)
227
224
228-- | Convert a type into a schema without references.
225-- | Convert a type into a schema without references.
@@ -239,7 +236,7 @@ toInlinedSchema proxy = inlineAllSchemas defs schema
239
236
240-- | Inline all /non-recursive/ schemas for which the definition
237-- | Inline all /non-recursive/ schemas for which the definition
241-- can be found in @'Definitions'@.
238-- can be found in @'Definitions'@.
242−inlineNonRecursiveSchemas :: Data s => Definitions -> s -> s
239+inlineNonRecursiveSchemas :: Data s => (Definitions Schema) -> s -> s
243inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
240inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
244 where
241 where
245 nonRecursive name =
242 nonRecursive name =
@@ -259,7 +256,7 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
259 Inline subschema -> usedNames subschema
256 Inline subschema -> usedNames subschema
260
257
261class GToSchema (f :: * -> *) where
258class GToSchema (f :: * -> *) where
262− gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
259+ gdeclareNamedSchema :: SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
263
260
264instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a] where
261instance {-# OVERLAPPABLE #-} ToSchema a => ToSchema [a] where
265 declareNamedSchema _ = do
262 declareNamedSchema _ = do
@@ -389,14 +386,14 @@ genericToNamedSchemaBoundedIntegral opts proxy
389 = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
386 = NamedSchema (gdatatypeSchemaName opts (Proxy :: Proxy d)) (toSchemaBoundedIntegral proxy)
390
387
391-- | A configurable generic @'Schema'@ creator.
388-- | 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
393genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
390genericDeclareSchema opts proxy = _namedSchemaSchema <$> genericDeclareNamedSchema opts proxy
394
391
395-- | A configurable generic @'NamedSchema'@ creator.
392-- | A configurable generic @'NamedSchema'@ creator.
396-- This function applied to @'defaultSchemaOptions'@
393-- This function applied to @'defaultSchemaOptions'@
397-- is used as the default for @'declareNamedSchema'@
394-- is used as the default for @'declareNamedSchema'@
398-- when the type is an instance of @'Generic'@.
395-- 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
400genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
397genericDeclareNamedSchema opts _ = gdeclareNamedSchema opts (Proxy :: Proxy (Rep a)) mempty
401
398
402gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
399gdatatypeSchemaName :: forall proxy d. Datatype d => SchemaOptions -> proxy d -> Maybe T.Text
@@ -425,7 +422,7 @@ nullarySchema = mempty
425gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
422gtoNamedSchema :: GToSchema f => SchemaOptions -> proxy f -> NamedSchema
426gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
423gtoNamedSchema opts proxy = undeclare $ gdeclareNamedSchema opts proxy mempty
427
424
428−gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare Definitions Schema
425+gdeclareSchema :: GToSchema f => SchemaOptions -> proxy f -> Declare (Definitions Schema) Schema
429gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
426gdeclareSchema opts proxy = _namedSchemaSchema <$> gdeclareNamedSchema opts proxy mempty
430
427
431instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
428instance (GToSchema f, GToSchema g) => GToSchema (f :*: g) where
@@ -459,7 +456,7 @@ instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
459 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
456 recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
460 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
457 fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s
461
458
462−gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare Definitions (Referenced Schema)
459+gdeclareSchemaRef :: GToSchema a => SchemaOptions -> proxy a -> Declare (Definitions Schema) (Referenced Schema)
463gdeclareSchemaRef opts proxy = do
460gdeclareSchemaRef opts proxy = do
464 case gtoNamedSchema opts proxy of
461 case gtoNamedSchema opts proxy of
465 NamedSchema (Just name) schema -> do
462 NamedSchema (Just name) schema -> do
@@ -484,7 +481,7 @@ appendItem x (Just (SwaggerItemsArray xs)) = Just (SwaggerItemsArray (x:xs))
484appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
481appendItem _ _ = error "GToSchema.appendItem: cannot append to SwaggerItemsObject"
485
482
486withFieldSchema :: forall proxy s f. (Selector s, GToSchema f) =>
483withFieldSchema :: 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
488withFieldSchema opts _ isRequiredField schema = do
485withFieldSchema opts _ isRequiredField schema = do
489 ref <- gdeclareSchemaRef opts (Proxy :: Proxy f)
486 ref <- gdeclareSchemaRef opts (Proxy :: Proxy f)
490 return $
487 return $
@@ -518,7 +515,7 @@ instance {-# OVERLAPPABLE #-} ToSchema c => GToSchema (K1 i c) where
518instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
515instance (GSumToSchema f, GSumToSchema g) => GToSchema (f :+: g) where
519 gdeclareNamedSchema = gdeclareNamedSumSchema
516 gdeclareNamedSchema = gdeclareNamedSumSchema
520
517
521−gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare Definitions NamedSchema
518+gdeclareNamedSumSchema :: GSumToSchema f => SchemaOptions -> proxy f -> Schema -> Declare (Definitions Schema) NamedSchema
522gdeclareNamedSumSchema opts proxy s
519gdeclareNamedSumSchema opts proxy s
523 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
520 | allNullaryToStringTag opts && allNullary = pure $ unnamed (toStringTag sumSchema)
524 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
521 | otherwise = (unnamed . fst) <$> runWriterT declareSumSchema
@@ -533,13 +530,13 @@ gdeclareNamedSumSchema opts proxy s
533type AllNullary = All
530type AllNullary = All
534
531
535class GSumToSchema f where
532class 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
537
534
538instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
535instance (GSumToSchema f, GSumToSchema g) => GSumToSchema (f :+: g) where
539 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) <=< gsumToSchema opts (Proxy :: Proxy g)
536 gsumToSchema opts _ = gsumToSchema opts (Proxy :: Proxy f) <=< gsumToSchema opts (Proxy :: Proxy g)
540
537
541gsumConToSchema :: forall c f proxy. (GToSchema (C1 c f), Constructor c) =>
538gsumConToSchema :: 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
543gsumConToSchema opts _ schema = do
540gsumConToSchema opts _ schema = do
544 ref <- gdeclareSchemaRef opts (Proxy :: Proxy (C1 c f))
541 ref <- gdeclareSchemaRef opts (Proxy :: Proxy (C1 c f))
545 return $ schema
542 return $ schema
src/Data/Swagger/Schema.hs−1
@@ -9,7 +9,6 @@
9module Data.Swagger.Schema (
9module Data.Swagger.Schema (
10 -- * Encoding
10 -- * Encoding
11 ToSchema(..),
11 ToSchema(..),
12− Definitions,
13 declareSchema,
12 declareSchema,
14 declareSchemaRef,
13 declareSchemaRef,
15 toSchema,
14 toSchema,