@@ -1,3 +1,4 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
12{-# LANGUAGE DeriveGeneric #-}
23{-# LANGUAGE DeriveTraversable #-}
34{-# LANGUAGE FlexibleContexts #-}
@@ -16,6 +17,7 @@ import Control.Monad
1617import Data.Aeson
1718import Data.Aeson.TH (deriveJSON)
1819import qualified Data.Aeson.Types as JSON
20+import Data.Data (Data(..), Typeable, mkConstr, mkDataType, Fixity(..), Constr, DataType, constrIndex)
1921import Data.HashMap.Strict (HashMap)
2022import qualified Data.HashMap.Strict as HashMap
2123import Data.Map (Map)
@@ -91,7 +93,7 @@ data Swagger = Swagger
9193
9294 -- | Additional external documentation.
9395 , _externalDocs :: Maybe ExternalDocs
94− } deriving (Eq, Show, Generic)
96+ } deriving (Eq, Show, Generic, Data, Typeable)
9597
9698-- | The object provides metadata about the API.
9799-- The metadata can be used by the clients if needed,
@@ -116,7 +118,7 @@ data Info = Info
116118 -- | Provides the version of the application API
117119 -- (not to be confused with the specification version).
118120 , _infoVersion :: Text
119− } deriving (Eq, Show, Generic)
121+ } deriving (Eq, Show, Generic, Data, Typeable)
120122
121123-- | Contact information for the exposed API.
122124data Contact = Contact
@@ -128,7 +130,7 @@ data Contact = Contact
128130
129131 -- | The email address of the contact person/organization.
130132 , _contactEmail :: Maybe Text
131− } deriving (Eq, Show)
133+ } deriving (Eq, Show, Generic, Data, Typeable)
132134
133135-- | License information for the exposed API.
134136data License = License
@@ -137,13 +139,26 @@ data License = License
137139
138140 -- | A URL to the license used for the API.
139141 , _licenseUrl :: Maybe URL
140− } deriving (Eq, Show)
142+ } deriving (Eq, Show, Generic, Data, Typeable)
141143
142144-- | The host (name or ip) serving the API. It MAY include a port.
143145data Host = Host
144146 { _hostName :: HostName -- ^ Host name.
145147 , _hostPort :: Maybe PortNumber -- ^ Optional port.
146− } deriving (Eq, Show)
148+ } deriving (Eq, Show, Generic, Typeable)
149+
150+hostConstr :: Constr
151+hostConstr = mkConstr hostDataType "Host" [] Prefix
152+
153+hostDataType :: DataType
154+hostDataType = mkDataType "Data.Swagger.Host" [hostConstr]
155+
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
147162
148163-- | The transfer protocol of the API.
149164data Scheme
@@ -151,14 +166,14 @@ data Scheme
151166 | Https
152167 | Ws
153168 | Wss
154− deriving (Eq, Show)
169+ deriving (Eq, Show, Generic, Data, Typeable)
155170
156171-- | The available paths and operations for the API.
157172data Paths = Paths
158173 { -- | Holds the relative paths to the individual endpoints.
159174 -- The path is appended to the @'basePath'@ in order to construct the full URL.
160175 _pathsMap :: HashMap FilePath PathItem
161− } deriving (Eq, Show, Generic)
176+ } deriving (Eq, Show, Generic, Data, Typeable)
162177
163178-- | Describes the operations available on a single path.
164179-- A @'PathItem'@ may be empty, due to ACL constraints.
@@ -191,7 +206,7 @@ data PathItem = PathItem
191206 -- The list MUST NOT include duplicated parameters.
192207 -- A unique parameter is defined by a combination of a name and location.
193208 , _pathItemParameters :: [Referenced Param]
194− } deriving (Eq, Show, Generic)
209+ } deriving (Eq, Show, Generic, Data, Typeable)
195210
196211-- | Describes a single API operation on a path.
197212data Operation = Operation
@@ -251,10 +266,23 @@ data Operation = Operation
251266 -- This definition overrides any declared top-level security.
252267 -- To remove a top-level security declaration, @Just []@ can be used.
253268 , _operationSecurity :: [SecurityRequirement]
254− } deriving (Eq, Show, Generic)
269+ } deriving (Eq, Show, Generic, Data, Typeable)
255270
256271newtype MimeList = MimeList { getMimeList :: [MediaType] }
257− deriving (Eq, Show, Monoid)
272+ deriving (Eq, Show, Monoid, Typeable)
273+
274+mimeListConstr :: Constr
275+mimeListConstr = mkConstr mimeListDataType "MimeList" ["getMimeList"] Prefix
276+
277+mimeListDataType :: DataType
278+mimeListDataType = mkDataType "Data.Swagger.MimeList" [mimeListConstr]
279+
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
258286
259287-- | Describes a single operation parameter.
260288-- A unique parameter is defined by a combination of a name and location.
@@ -275,12 +303,12 @@ data Param = Param
275303
276304 -- | Parameter schema.
277305 , _paramSchema :: ParamAnySchema
278− } deriving (Eq, Show, Generic)
306+ } deriving (Eq, Show, Generic, Data, Typeable)
279307
280308data ParamAnySchema
281309 = ParamBody (Referenced Schema)
282310 | ParamOther ParamOtherSchema
283− deriving (Eq, Show)
311+ deriving (Eq, Show, Generic, Data, Typeable)
284312
285313data ParamOtherSchema = ParamOtherSchema
286314 { -- | The location of the parameter.
@@ -297,7 +325,7 @@ data ParamOtherSchema = ParamOtherSchema
297325 , _paramOtherSchemaCollectionFormat :: Maybe (CollectionFormat Param)
298326
299327 , _paramOtherSchemaParamSchema :: ParamSchema ParamOtherSchema Items
300− } deriving (Eq, Show, Generic)
328+ } deriving (Eq, Show, Generic, Data, Typeable)
301329
302330data SwaggerType t where
303331 SwaggerString :: SwaggerType t
@@ -311,6 +339,41 @@ data SwaggerType t where
311339
312340deriving instance Eq (SwaggerType t)
313341deriving instance Show (SwaggerType t)
342+deriving instance Typeable (SwaggerType t)
343+
344+swaggerTypeConstr :: Data (SwaggerType t) => SwaggerType t -> Constr
345+swaggerTypeConstr t = mkConstr (dataTypeOf t) (show t) [] Prefix
346+
347+swaggerTypeDataType :: Data (SwaggerType t) => SwaggerType t -> DataType
348+swaggerTypeDataType _ = mkDataType "Data.Swagger.SwaggerType" swaggerTypeConstrs
349+
350+swaggerCommonTypes :: [SwaggerType t]
351+swaggerCommonTypes = [SwaggerString, SwaggerNumber, SwaggerInteger, SwaggerBoolean, SwaggerArray]
352+
353+swaggerParamTypes :: [SwaggerType ParamOtherSchema]
354+swaggerParamTypes = swaggerCommonTypes ++ [SwaggerFile]
355+
356+swaggerSchemaTypes :: [SwaggerType Schema]
357+swaggerSchemaTypes = swaggerCommonTypes ++ [error "SwaggerFile is invalid SwaggerType Schema", SwaggerNull, SwaggerObject]
358+
359+swaggerTypeConstrs :: [Constr]
360+swaggerTypeConstrs = map swaggerTypeConstr (swaggerCommonTypes :: [SwaggerType Schema])
361+ ++ [swaggerTypeConstr SwaggerFile, swaggerTypeConstr SwaggerNull, swaggerTypeConstr SwaggerObject]
362+
363+instance {-# OVERLAPPABLE #-} Typeable t => Data (SwaggerType t) where
364+ gunfold = gunfoldEnum "SwaggerType" swaggerCommonTypes
365+ toConstr = swaggerTypeConstr
366+ dataTypeOf = swaggerTypeDataType
367+
368+instance {-# OVERLAPPING #-} Data (SwaggerType ParamOtherSchema) where
369+ gunfold = gunfoldEnum "SwaggerType ParamOtherSchema" swaggerParamTypes
370+ toConstr = swaggerTypeConstr
371+ dataTypeOf = swaggerTypeDataType
372+
373+instance {-# OVERLAPPING #-} Data (SwaggerType Schema) where
374+ gunfold = gunfoldEnum "SwaggerType Schema" swaggerSchemaTypes
375+ toConstr = swaggerTypeConstr
376+ dataTypeOf = swaggerTypeDataType
314377
315378data ParamLocation
316379 = -- | Parameters that are appended to the URL.
@@ -330,7 +393,7 @@ data ParamLocation
330393 -- Form parameters have a different format based on the content-type used
331394 -- (for further details, consult <http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>).
332395 | ParamFormData
333− deriving (Eq, Show)
396+ deriving (Eq, Show, Generic, Data, Typeable)
334397
335398type Format = Text
336399
@@ -351,6 +414,24 @@ data CollectionFormat t where
351414
352415deriving instance Eq (CollectionFormat t)
353416deriving instance Show (CollectionFormat t)
417+deriving instance Typeable (CollectionFormat t)
418+
419+collectionFormatConstr :: CollectionFormat t -> Constr
420+collectionFormatConstr cf = mkConstr collectionFormatDataType (show cf) [] Prefix
421+
422+collectionFormatDataType :: DataType
423+collectionFormatDataType = mkDataType "Data.Swagger.CollectionFormat" $
424+ map collectionFormatConstr collectionCommonFormats
425+
426+collectionCommonFormats :: [CollectionFormat t]
427+collectionCommonFormats = [ CollectionCSV, CollectionSSV, CollectionTSV, CollectionPipes ]
428+
429+instance {-# OVERLAPPABLE #-} Data t => Data (CollectionFormat t) where
430+ gunfold = gunfoldEnum "CollectionFormat" collectionCommonFormats
431+ toConstr = collectionFormatConstr
432+ dataTypeOf _ = collectionFormatDataType
433+
434+deriving instance {-# OVERLAPPING #-} Data (CollectionFormat Param)
354435
355436type ParamName = Text
356437
@@ -373,12 +454,12 @@ data Schema = Schema
373454 , _schemaMinProperties :: Maybe Integer
374455
375456 , _schemaParamSchema :: ParamSchema Schema SchemaItems
376− } deriving (Eq, Show, Generic)
457+ } deriving (Eq, Show, Generic, Data, Typeable)
377458
378459data SchemaItems
379460 = SchemaItemsObject (Referenced Schema)
380461 | SchemaItemsArray [Referenced Schema]
381− deriving (Eq, Show)
462+ deriving (Eq, Show, Generic, Data, Typeable)
382463
383464data ParamSchema t items = ParamSchema
384465 { -- | Declares the value of the parameter that the server will use if none is provided,
@@ -403,7 +484,9 @@ data ParamSchema t items = ParamSchema
403484 , _paramSchemaUniqueItems :: Maybe Bool
404485 , _paramSchemaEnum :: Maybe [Value]
405486 , _paramSchemaMultipleOf :: Maybe Scientific
406− } deriving (Eq, Show, Generic)
487+ } deriving (Eq, Show, Generic, Typeable)
488+
489+deriving instance (Data t, Data (SwaggerType t), Data i) => Data (ParamSchema t i)
407490
408491data Xml = Xml
409492 { -- | Replaces the name of the element/attribute used for the described schema property.
@@ -431,7 +514,7 @@ data Xml = Xml
431514 -- Default value is @False@.
432515 -- The definition takes effect only when defined alongside type being array (outside the items).
433516 , _xmlWrapped :: Maybe Bool
434− } deriving (Eq, Show, Generic)
517+ } deriving (Eq, Show, Generic, Data, Typeable)
435518
436519data Items = Items
437520 { -- | Determines the format of the array if type array is used.
@@ -439,7 +522,7 @@ data Items = Items
439522 _itemsCollectionFormat :: Maybe (CollectionFormat Items)
440523
441524 , _itemsParamSchema :: ParamSchema Items Items
442− } deriving (Eq, Show, Generic)
525+ } deriving (Eq, Show, Generic, Data, Typeable)
443526
444527-- | A container for the expected responses of an operation.
445528-- The container maps a HTTP response code to the expected response.
@@ -454,7 +537,7 @@ data Responses = Responses
454537 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
455538 -- Describes the expected response for those HTTP status codes.
456539 , _responsesResponses :: HashMap HttpStatusCode (Referenced Response)
457− } deriving (Eq, Show, Generic)
540+ } deriving (Eq, Show, Generic, Data, Typeable)
458541
459542type HttpStatusCode = Int
460543
@@ -476,7 +559,7 @@ data Response = Response
476559
477560 -- | An example of the response message.
478561 , _responseExamples :: Maybe Example
479− } deriving (Eq, Show, Generic)
562+ } deriving (Eq, Show, Generic, Data, Typeable)
480563
481564type HeaderName = Text
482565
@@ -489,16 +572,29 @@ data Header = Header
489572 , _headerCollectionFormat :: Maybe (CollectionFormat Items)
490573
491574 , _headerParamSchema :: ParamSchema Header Items
492− } deriving (Eq, Show, Generic)
575+ } deriving (Eq, Show, Generic, Data, Typeable)
493576
494577data Example = Example { getExample :: Map MediaType Value }
495− deriving (Eq, Show)
578+ deriving (Eq, Show, Generic, Typeable)
579+
580+exampleConstr :: Constr
581+exampleConstr = mkConstr exampleDataType "Example" ["getExample"] Prefix
582+
583+exampleDataType :: DataType
584+exampleDataType = mkDataType "Data.Swagger.Example" [exampleConstr]
585+
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
496592
497593-- | The location of the API key.
498594data ApiKeyLocation
499595 = ApiKeyQuery
500596 | ApiKeyHeader
501− deriving (Eq, Show)
597+ deriving (Eq, Show, Generic, Data, Typeable)
502598
503599data ApiKeyParams = ApiKeyParams
504600 { -- | The name of the header or query parameter to be used.
@@ -506,7 +602,7 @@ data ApiKeyParams = ApiKeyParams
506602
507603 -- | The location of the API key.
508604 , _apiKeyIn :: ApiKeyLocation
509− } deriving (Eq, Show)
605+ } deriving (Eq, Show, Generic, Data, Typeable)
510606
511607-- | The authorization URL to be used for OAuth2 flow. This SHOULD be in the form of a URL.
512608type AuthorizationURL = Text
@@ -519,7 +615,7 @@ data OAuth2Flow
519615 | OAuth2Password TokenURL
520616 | OAuth2Application TokenURL
521617 | OAuth2AccessCode AuthorizationURL TokenURL
522− deriving (Eq, Show)
618+ deriving (Eq, Show, Generic, Data, Typeable)
523619
524620data OAuth2Params = OAuth2Params
525621 { -- | The flow used by the OAuth2 security scheme.
@@ -527,13 +623,13 @@ data OAuth2Params = OAuth2Params
527623
528624 -- | The available scopes for the OAuth2 security scheme.
529625 , _oauth2Scopes :: HashMap Text Text
530− } deriving (Eq, Show, Generic)
626+ } deriving (Eq, Show, Generic, Data, Typeable)
531627
532628data SecuritySchemeType
533629 = SecuritySchemeBasic
534630 | SecuritySchemeApiKey ApiKeyParams
535631 | SecuritySchemeOAuth2 OAuth2Params
536− deriving (Eq, Show)
632+ deriving (Eq, Show, Generic, Data, Typeable)
537633
538634data SecurityScheme = SecurityScheme
539635 { -- | The type of the security scheme.
@@ -541,14 +637,14 @@ data SecurityScheme = SecurityScheme
541637
542638 -- | A short description for security scheme.
543639 , _securitySchemeDescription :: Maybe Text
544− } deriving (Eq, Show, Generic)
640+ } deriving (Eq, Show, Generic, Data, Typeable)
545641
546642-- | Lists the required security schemes to execute this operation.
547643-- The object can have multiple security schemes declared in it which are all required
548644-- (that is, there is a logical AND between the schemes).
549645newtype SecurityRequirement = SecurityRequirement
550646 { getSecurityRequirement :: HashMap Text [Text]
551− } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON)
647+ } deriving (Eq, Read, Show, Monoid, ToJSON, FromJSON, Data, Typeable)
552648
553649-- | Allows adding meta data to a single tag that is used by @Operation@.
554650-- It is not mandatory to have a @Tag@ per tag used there.
@@ -562,7 +658,7 @@ data Tag = Tag
562658
563659 -- | Additional external documentation for this tag.
564660 , _tagExternalDocs :: Maybe ExternalDocs
565− } deriving (Eq, Show)
661+ } deriving (Eq, Show, Generic, Data, Typeable)
566662
567663-- | Allows referencing an external resource for extended documentation.
568664data ExternalDocs = ExternalDocs
@@ -572,19 +668,19 @@ data ExternalDocs = ExternalDocs
572668
573669 -- | The URL for the target documentation.
574670 , _externalDocsUrl :: URL
575− } deriving (Eq, Show, Generic)
671+ } deriving (Eq, Show, Generic, Data, Typeable)
576672
577673-- | A simple object to allow referencing other definitions in the specification.
578674-- It can be used to reference parameters and responses that are defined at the top level for reuse.
579675newtype Reference = Reference { getReference :: Text }
580− deriving (Eq, Show)
676+ deriving (Eq, Show, Data, Typeable)
581677
582678data Referenced a
583679 = Ref Reference
584680 | Inline a
585− deriving (Eq, Show)
681+ deriving (Eq, Show, Data, Typeable)
586682
587−newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON)
683+newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
588684
589685-- =======================================================================
590686-- Monoid instances