@@ -1,3 +1,4 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE DeriveTraversable #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleContexts #-}
@@ -16,6 +17,7 @@ import Control.Monad
16import Data.Aeson
17import Data.Aeson
17import Data.Aeson.TH (deriveJSON)
18import Data.Aeson.TH (deriveJSON)
18import qualified Data.Aeson.Types as JSON
19import qualified Data.Aeson.Types as JSON
20+import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
19import Data.HashMap.Strict (HashMap)
21import Data.HashMap.Strict (HashMap)
20import qualified Data.HashMap.Strict as HashMap
22import qualified Data.HashMap.Strict as HashMap
21import Data.Map (Map)
23import Data.Map (Map)
@@ -91,7 +93,7 @@ data Swagger = Swagger
92 -- | Additional external documentation.
94 -- | Additional external documentation.
93 , _externalDocs :: Maybe ExternalDocs
95 , _externalDocs :: Maybe ExternalDocs
94− } deriving (Eq, Show, Generic)
96+ } deriving (Eq, Show, Generic, Data, Typeable)
96-- | The object provides metadata about the API.
98-- | The object provides metadata about the API.
97-- The metadata can be used by the clients if needed,
99-- The metadata can be used by the clients if needed,
@@ -116,7 +118,7 @@ data Info = Info
116 -- | Provides the version of the application API
118 -- | Provides the version of the application API
117 -- (not to be confused with the specification version).
119 -- (not to be confused with the specification version).
118 , _infoVersion :: Text
120 , _infoVersion :: Text
119− } deriving (Eq, Show, Generic)
121+ } deriving (Eq, Show, Generic, Data, Typeable)
121-- | Contact information for the exposed API.
123-- | Contact information for the exposed API.
122data Contact = Contact
124data Contact = Contact
@@ -128,7 +130,7 @@ data Contact = Contact
129 -- | The email address of the contact person/organization.
131 -- | The email address of the contact person/organization.
130 , _contactEmail :: Maybe Text
132 , _contactEmail :: Maybe Text
131− } deriving (Eq, Show)
133+ } deriving (Eq, Show, Generic, Data, Typeable)
133-- | License information for the exposed API.
135-- | License information for the exposed API.
134data License = License
136data License = License
@@ -137,13 +139,26 @@ data License = License
138 -- | A URL to the license used for the API.
140 -- | A URL to the license used for the API.
139 , _licenseUrl :: Maybe URL
141 , _licenseUrl :: Maybe URL
140− } deriving (Eq, Show)
142+ } deriving (Eq, Show, Generic, Data, Typeable)
142-- | The host (name or ip) serving the API. It MAY include a port.
144-- | The host (name or ip) serving the API. It MAY include a port.
143data Host = Host
145data Host = Host
144 { _hostName :: HostName -- ^ Host name.
146 { _hostName :: HostName -- ^ Host name.
145 , _hostPort :: Maybe PortNumber -- ^ Optional port.
147 , _hostPort :: Maybe PortNumber -- ^ Optional port.
146− } deriving (Eq, Show)
148+ } deriving (Eq, Show, Generic, Typeable)
151+hostConstr = mkConstr hostDataType "Host" [] Prefix
153+hostDataType :: DataType
154+hostDataType = mkDataType "Data.Swagger.Host" [hostConstr]
156+instance Data Host where
157+ gunfold k z c = case constrIndex c of
158+ 1 -> k (k (z (\name mport -> Host name (fromInteger <$> mport))))
159+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type Host."
160+ toConstr (Host _ _) = hostConstr
161+ dataTypeOf _ = hostDataType
148-- | The transfer protocol of the API.
163-- | The transfer protocol of the API.
149data Scheme
164data Scheme
@@ -151,14 +166,14 @@ data Scheme
154− deriving (Eq, Show)
169+ deriving (Eq, Show, Generic, Data, Typeable)
156-- | The available paths and operations for the API.
171-- | The available paths and operations for the API.
157data Paths = Paths
172data Paths = Paths
158 { -- | Holds the relative paths to the individual endpoints.
173 { -- | Holds the relative paths to the individual endpoints.
159 -- The path is appended to the @'basePath'@ in order to construct the full URL.
174 -- The path is appended to the @'basePath'@ in order to construct the full URL.
160 _pathsMap :: HashMap FilePath PathItem
175 _pathsMap :: HashMap FilePath PathItem
161− } deriving (Eq, Show, Generic)
176+ } deriving (Eq, Show, Generic, Data, Typeable)
163-- | Describes the operations available on a single path.
178-- | Describes the operations available on a single path.
164-- A @'PathItem'@ may be empty, due to ACL constraints.
179-- A @'PathItem'@ may be empty, due to ACL constraints.
@@ -191,7 +206,7 @@ data PathItem = PathItem
191 -- The list MUST NOT include duplicated parameters.
206 -- The list MUST NOT include duplicated parameters.
192 -- A unique parameter is defined by a combination of a name and location.
207 -- A unique parameter is defined by a combination of a name and location.
193 , _pathItemParameters :: [Referenced Param]
208 , _pathItemParameters :: [Referenced Param]
194− } deriving (Eq, Show, Generic)
209+ } deriving (Eq, Show, Generic, Data, Typeable)
196-- | Describes a single API operation on a path.
211-- | Describes a single API operation on a path.
197data Operation = Operation
212data Operation = Operation
@@ -251,10 +266,23 @@ data Operation = Operation
251 -- This definition overrides any declared top-level security.
266 -- This definition overrides any declared top-level security.
252 -- To remove a top-level security declaration, @Just []@ can be used.
267 -- To remove a top-level security declaration, @Just []@ can be used.
253 , _operationSecurity :: [SecurityRequirement]
268 , _operationSecurity :: [SecurityRequirement]
254− } deriving (Eq, Show, Generic)
269+ } deriving (Eq, Show, Generic, Data, Typeable)
256newtype MimeList = MimeList { getMimeList :: [MediaType] }
271newtype MimeList = MimeList { getMimeList :: [MediaType] }
257− deriving (Eq, Show, Monoid)
272+ deriving (Eq, Show, Monoid, Typeable)
274+mimeListConstr :: Constr
275+mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
277+mimeListDataType :: DataType
278+mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
280+instance Data MimeList where
281+ gunfold k z c = case constrIndex c of
282+ 1 -> k (z (\xs -> MimeList (map fromString xs)))
283+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type MimeList."
284+ toConstr (MimeList _) = mimeListConstr
285+ dataTypeOf _ = mimeListDataType
259-- | Describes a single operation parameter.
287-- | Describes a single operation parameter.
260-- A unique parameter is defined by a combination of a name and location.
288-- A unique parameter is defined by a combination of a name and location.
@@ -275,12 +303,12 @@ data Param = Param
276 -- | Parameter schema.
304 -- | Parameter schema.
277 , _paramSchema :: ParamAnySchema
305 , _paramSchema :: ParamAnySchema
278− } deriving (Eq, Show, Generic)
306+ } deriving (Eq, Show, Generic, Data, Typeable)
280data ParamAnySchema
308data ParamAnySchema
281 = ParamBody (Referenced Schema)
309 = ParamBody (Referenced Schema)
282 | ParamOther ParamOtherSchema
310 | ParamOther ParamOtherSchema
283− deriving (Eq, Show)
311+ deriving (Eq, Show, Generic, Data, Typeable)
285data ParamOtherSchema = ParamOtherSchema
313data ParamOtherSchema = ParamOtherSchema
286 { -- | The location of the parameter.
314 { -- | The location of the parameter.
@@ -297,7 +325,7 @@ data ParamOtherSchema = ParamOtherSchema
297 , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
325 , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
299 , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema Items
327 , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema Items
300− } deriving (Eq, Show, Generic)
328+ } deriving (Eq, Show, Generic, Data, Typeable)
302data SwaggerType t where
330data SwaggerType t where
303 SwaggerString :: SwaggerType t
331 SwaggerString :: SwaggerType t
@@ -311,6 +339,41 @@ data SwaggerType t where
312deriving instance Eq (SwaggerType t)
340deriving instance Eq (SwaggerType t)
313deriving instance Show (SwaggerType t)
341deriving instance Show (SwaggerType t)
342+deriving instance Typeable (SwaggerType t)
344+swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
345+swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
347+swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
348+swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
350+swaggerCommonTypes :: [SwaggerType t]
351+swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
353+swaggerParamTypes :: [SwaggerType ParamOtherSchema]
354+swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
356+swaggerSchemaTypes :: [SwaggerType Schema]
357+swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
359+swaggerTypeConstrs :: [Constr]
360+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
361+ ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
363+instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerType t) where
364+ gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
365+ toConstr = swaggerTypeConstr
366+ dataTypeOf = swaggerTypeDataType
368+instance {-# OVERLAPPING #-} Data (SwaggerType ParamOtherSchema) where
369+ gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
370+ toConstr = swaggerTypeConstr
371+ dataTypeOf = swaggerTypeDataType
373+instance {-# OVERLAPPING #-} Data (SwaggerType Schema) where
374+ gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
375+ toConstr = swaggerTypeConstr
376+ dataTypeOf = swaggerTypeDataType
315data ParamLocation
378data ParamLocation
316 = -- | Parameters that are appended to the URL.
379 = -- | Parameters that are appended to the URL.
@@ -330,7 +393,7 @@ data ParamLocation
330 -- Form parameters have a different format based on the content-type used
393 -- Form parameters have a different format based on the content-type used
331 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
394 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
332 | ParamFormData
395 | ParamFormData
333− deriving (Eq, Show)
396+ deriving (Eq, Show, Generic, Data, Typeable)
335type Format = Text
398type Format = Text
@@ -351,6 +414,24 @@ data CollectionFormat t where
352deriving instance Eq (CollectionFormat t)
415deriving instance Eq (CollectionFormat t)
353deriving instance Show (CollectionFormat t)
416deriving instance Show (CollectionFormat t)
417+deriving instance Typeable (CollectionFormat t)
419+collectionFormatConstr :: CollectionFormat t -> Constr
420+collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
422+collectionFormatDataType :: DataType
423+collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
424+ map collectionFormatConstr collectionCommonFormats
426+collectionCommonFormats :: [CollectionFormat t]
427+collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
429+instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
430+ gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
431+ toConstr = collectionFormatConstr
432+ dataTypeOf _ = collectionFormatDataType
434+deriving instance {-# OVERLAPPING #-} Data (CollectionFormat Param)
355type ParamName = Text
436type ParamName = Text
@@ -373,12 +454,12 @@ data Schema = Schema
373 , _schemaMinProperties :: Maybe Integer
454 , _schemaMinProperties :: Maybe Integer
375 , _schemaParamSchema :: ParamSchema Schema SchemaItems
456 , _schemaParamSchema :: ParamSchema Schema SchemaItems
376− } deriving (Eq, Show, Generic)
457+ } deriving (Eq, Show, Generic, Data, Typeable)
378data SchemaItems
459data SchemaItems
379 = SchemaItemsObject (Referenced Schema)
460 = SchemaItemsObject (Referenced Schema)
380 | SchemaItemsArray [Referenced Schema]
461 | SchemaItemsArray [Referenced Schema]
381− deriving (Eq, Show)
462+ deriving (Eq, Show, Generic, Data, Typeable)
383data ParamSchema t items = ParamSchema
464data ParamSchema t items = ParamSchema
384 { -- | Declares the value of the parameter that the server will use if none is provided,
465 { -- | Declares the value of the parameter that the server will use if none is provided,
@@ -403,7 +484,9 @@ data ParamSchema t items = ParamSchema
403 , _paramSchemaUniqueItems :: Maybe Bool
484 , _paramSchemaUniqueItems :: Maybe Bool
404 , _paramSchemaEnum :: Maybe [Value]
485 , _paramSchemaEnum :: Maybe [Value]
405 , _paramSchemaMultipleOf :: Maybe Scientific
486 , _paramSchemaMultipleOf :: Maybe Scientific
406− } deriving (Eq, Show, Generic)
487+ } deriving (Eq, Show, Generic, Typeable)
489+deriving instance (Data t, Data (SwaggerType t), Data i) => Data (ParamSchema t i)
408data Xml = Xml
491data Xml = Xml
409 { -- | Replaces the name of the element/attribute used for the described schema property.
492 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -431,7 +514,7 @@ data Xml = Xml
431 -- Default value is @False@.
514 -- Default value is @False@.
432 -- The definition takes effect only when defined alongside type being array (outside the items).
515 -- The definition takes effect only when defined alongside type being array (outside the items).
433 , _xmlWrapped :: Maybe Bool
516 , _xmlWrapped :: Maybe Bool
434− } deriving (Eq, Show, Generic)
517+ } deriving (Eq, Show, Generic, Data, Typeable)
436data Items = Items
519data Items = Items
437 { -- | Determines the format of the array if type array is used.
520 { -- | Determines the format of the array if type array is used.
@@ -439,7 +522,7 @@ data Items = Items
439 _itemsCollectionFormat :: Maybe (CollectionFormat Items)
522 _itemsCollectionFormat :: Maybe (CollectionFormat Items)
441 , _itemsParamSchema :: ParamSchema Items Items
524 , _itemsParamSchema :: ParamSchema Items Items
442− } deriving (Eq, Show, Generic)
525+ } deriving (Eq, Show, Generic, Data, Typeable)
444-- | A container for the expected responses of an operation.
527-- | A container for the expected responses of an operation.
445-- The container maps a HTTP response code to the expected response.
528-- The container maps a HTTP response code to the expected response.
@@ -454,7 +537,7 @@ data Responses = Responses
454 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
537 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
455 -- Describes the expected response for those HTTP status codes.
538 -- Describes the expected response for those HTTP status codes.
456 , _responsesResponses :: HashMap HttpStatusCode (Referenced Response)
539 , _responsesResponses :: HashMap HttpStatusCode (Referenced Response)
457− } deriving (Eq, Show, Generic)
540+ } deriving (Eq, Show, Generic, Data, Typeable)
459type HttpStatusCode = Int
542type HttpStatusCode = Int
@@ -476,7 +559,7 @@ data Response = Response
477 -- | An example of the response message.
560 -- | An example of the response message.
478 , _responseExamples :: Maybe Example
561 , _responseExamples :: Maybe Example
479− } deriving (Eq, Show, Generic)
562+ } deriving (Eq, Show, Generic, Data, Typeable)
481type HeaderName = Text
564type HeaderName = Text
@@ -489,16 +572,29 @@ data Header = Header
489 , _headerCollectionFormat :: Maybe (CollectionFormat Items)
572 , _headerCollectionFormat :: Maybe (CollectionFormat Items)
491 , _headerParamSchema :: ParamSchema Header Items
574 , _headerParamSchema :: ParamSchema Header Items
492− } deriving (Eq, Show, Generic)
575+ } deriving (Eq, Show, Generic, Data, Typeable)
494data Example = Example { getExample :: Map MediaType Value }
577data Example = Example { getExample :: Map MediaType Value }
495− deriving (Eq, Show)
578+ deriving (Eq, Show, Generic, Typeable)
580+exampleConstr :: Constr
581+exampleConstr = mkConstr exampleDataType "Example" ["getExample"] Prefix
583+exampleDataType :: DataType
584+exampleDataType = mkDataType "Data.Swagger.Example" [exampleConstr]
586+instance Data Example where
587+ gunfold k z c = case constrIndex c of
588+ 1 -> k (z (\m -> Example (Map.mapKeys fromString m)))
589+ _ -> error $ "Data.Data.gunfold: Constructor " ++ show c ++ " is not of type Example."
590+ toConstr (Example _) = exampleConstr
591+ dataTypeOf _ = exampleDataType
497-- | The location of the API key.
593-- | The location of the API key.
498data ApiKeyLocation
594data ApiKeyLocation
499 = ApiKeyQuery
595 = ApiKeyQuery
500 | ApiKeyHeader
596 | ApiKeyHeader
501− deriving (Eq, Show)
597+ deriving (Eq, Show, Generic, Data, Typeable)
503data ApiKeyParams = ApiKeyParams
599data ApiKeyParams = ApiKeyParams
504 { -- | The name of the header or query parameter to be used.
600 { -- | The name of the header or query parameter to be used.
@@ -506,7 +602,7 @@ data ApiKeyParams = ApiKeyParams
507 -- | The location of the API key.
603 -- | The location of the API key.
508 , _apiKeyIn :: ApiKeyLocation
604 , _apiKeyIn :: ApiKeyLocation
509− } deriving (Eq, Show)
605+ } deriving (Eq, Show, Generic, Data, Typeable)
511-- | The authorization URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
607-- | The authorization URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
512type AuthorizationURL = Text
608type AuthorizationURL = Text
@@ -519,7 +615,7 @@ data OAuth2Flow
519 | OAuth2Password TokenURL
615 | OAuth2Password TokenURL
520 | OAuth2Application TokenURL
616 | OAuth2Application TokenURL
521 | OAuth2AccessCode AuthorizationURL TokenURL
617 | OAuth2AccessCode AuthorizationURL TokenURL
522− deriving (Eq, Show)
618+ deriving (Eq, Show, Generic, Data, Typeable)
524data OAuth2Params = OAuth2Params
620data OAuth2Params = OAuth2Params
525 { -- | The flow used by the OAuth2 security scheme.
621 { -- | The flow used by the OAuth2 security scheme.
@@ -527,13 +623,13 @@ data OAuth2Params = OAuth2Params
528 -- | The available scopes for the OAuth2 security scheme.
624 -- | The available scopes for the OAuth2 security scheme.
529 , _oauth2Scopes :: HashMap Text Text
625 , _oauth2Scopes :: HashMap Text Text
530− } deriving (Eq, Show, Generic)
626+ } deriving (Eq, Show, Generic, Data, Typeable)
532data SecuritySchemeType
628data SecuritySchemeType
533 = SecuritySchemeBasic
629 = SecuritySchemeBasic
534 | SecuritySchemeApiKey ApiKeyParams
630 | SecuritySchemeApiKey ApiKeyParams
535 | SecuritySchemeOAuth2 OAuth2Params
631 | SecuritySchemeOAuth2 OAuth2Params
536− deriving (Eq, Show)
632+ deriving (Eq, Show, Generic, Data, Typeable)
538data SecurityScheme = SecurityScheme
634data SecurityScheme = SecurityScheme
539 { -- | The type of the security scheme.
635 { -- | The type of the security scheme.
@@ -541,14 +637,14 @@ data SecurityScheme = SecurityScheme
542 -- | A short description for security scheme.
638 -- | A short description for security scheme.
543 , _securitySchemeDescription :: Maybe Text
639 , _securitySchemeDescription :: Maybe Text
544− } deriving (Eq, Show, Generic)
640+ } deriving (Eq, Show, Generic, Data, Typeable)
546-- | Lists the required security schemes to execute this operation.
642-- | Lists the required security schemes to execute this operation.
547-- The object can have multiple security schemes declared in it which are all required
643-- The object can have multiple security schemes declared in it which are all required
548-- (that is, there is a logical AND between the schemes).
644-- (that is, there is a logical AND between the schemes).
549newtype SecurityRequirement = SecurityRequirement
645newtype SecurityRequirement = SecurityRequirement
550 { getSecurityRequirement :: HashMap Text [Text]
646 { getSecurityRequirement :: HashMap Text [Text]
551− } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON)
647+ } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
553-- | Allows adding meta data to a single tag that is used by @Operation@.
649-- | Allows adding meta data to a single tag that is used by @Operation@.
554-- It is not mandatory to have a @Tag@ per tag used there.
650-- It is not mandatory to have a @Tag@ per tag used there.
@@ -562,7 +658,7 @@ data Tag = Tag
563 -- | Additional external documentation for this tag.
659 -- | Additional external documentation for this tag.
564 , _tagExternalDocs :: Maybe ExternalDocs
660 , _tagExternalDocs :: Maybe ExternalDocs
565− } deriving (Eq, Show)
661+ } deriving (Eq, Show, Generic, Data, Typeable)
567-- | Allows referencing an external resource for extended documentation.
663-- | Allows referencing an external resource for extended documentation.
568data ExternalDocs = ExternalDocs
664data ExternalDocs = ExternalDocs
@@ -572,19 +668,19 @@ data ExternalDocs = ExternalDocs
573 -- | The URL for the target documentation.
669 -- | The URL for the target documentation.
574 , _externalDocsUrl :: URL
670 , _externalDocsUrl :: URL
575− } deriving (Eq, Show, Generic)
671+ } deriving (Eq, Show, Generic, Data, Typeable)
577-- | A simple object to allow referencing other definitions in the specification.
673-- | A simple object to allow referencing other definitions in the specification.
578-- It can be used to reference parameters and responses that are defined at the top level for reuse.
674-- It can be used to reference parameters and responses that are defined at the top level for reuse.
579newtype Reference = Reference { getReference :: Text }
675newtype Reference = Reference { getReference :: Text }
580− deriving (Eq, Show)
676+ deriving (Eq, Show, Data, Typeable)
582data Referenced a
678data Referenced a
583 = Ref Reference
679 = Ref Reference
584 | Inline a
680 | Inline a
585− deriving (Eq, Show)
681+ deriving (Eq, Show, Data, Typeable)
587−newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON)
683+newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
589-- =======================================================================
685-- =======================================================================
590-- Monoid instances
686-- Monoid instances