@@ -1,9 +1,11 @@
1{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE DeriveTraversable #-}
2{-# LANGUAGE DeriveTraversable #-}
3+{-# LANGUAGE FlexibleInstances #-}
3{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE RecordWildCards #-}
6{-# LANGUAGE ScopedTypeVariables #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8+{-# LANGUAGE StandaloneDeriving #-}
7{-# LANGUAGE TemplateHaskell #-}
9{-# LANGUAGE TemplateHaskell #-}
8module Data.Swagger.Internal where
10module Data.Swagger.Internal where
@@ -16,6 +18,7 @@ import Data.HashMap.Strict (HashMap)
16import qualified Data.HashMap.Strict as HashMap
18import qualified Data.HashMap.Strict as HashMap
17import Data.Map (Map)
19import Data.Map (Map)
18import qualified Data.Map as Map
20import qualified Data.Map as Map
19import Data.String (fromString)
22import Data.String (fromString)
20import Data.Text (Text)
23import Data.Text (Text)
21import qualified Data.Text as Text
24import qualified Data.Text as Text
@@ -112,7 +115,7 @@ data SwaggerInfo = SwaggerInfo
112 -- | Provides the version of the application API
115 -- | Provides the version of the application API
113 -- (not to be confused with the specification version).
116 -- (not to be confused with the specification version).
114 , swaggerInfoVersion :: Text
117 , swaggerInfoVersion :: Text
115− } deriving (Eq, Show)
118+ } deriving (Eq, Show, Generic)
117-- | Contact information for the exposed API.
120-- | Contact information for the exposed API.
118data SwaggerContact = SwaggerContact
121data SwaggerContact = SwaggerContact
@@ -154,7 +157,7 @@ data SwaggerPaths = SwaggerPaths
154 { -- | Holds the relative paths to the individual endpoints.
157 { -- | Holds the relative paths to the individual endpoints.
155 -- The path is appended to the @'swaggerBasePath'@ in order to construct the full URL.
158 -- The path is appended to the @'swaggerBasePath'@ in order to construct the full URL.
156 swaggerPathsMap :: HashMap FilePath SwaggerPathItem
159 swaggerPathsMap :: HashMap FilePath SwaggerPathItem
157− } deriving (Eq, Show)
160+ } deriving (Eq, Show, Generic)
159-- | Describes the operations available on a single path.
162-- | Describes the operations available on a single path.
160-- A @'SwaggerPathItem'@ may be empty, due to ACL constraints.
163-- A @'SwaggerPathItem'@ may be empty, due to ACL constraints.
@@ -187,7 +190,7 @@ data SwaggerPathItem = SwaggerPathItem
187 -- The list MUST NOT include duplicated parameters.
190 -- The list MUST NOT include duplicated parameters.
188 -- A unique parameter is defined by a combination of a name and location.
191 -- A unique parameter is defined by a combination of a name and location.
189 , swaggerPathItemParameters :: [SwaggerParameter]
192 , swaggerPathItemParameters :: [SwaggerParameter]
190− } deriving (Eq, Show)
193+ } deriving (Eq, Show, Generic)
192-- | Describes a single API operation on a path.
195-- | Describes a single API operation on a path.
193data SwaggerOperation = SwaggerOperation
196data SwaggerOperation = SwaggerOperation
@@ -250,7 +253,7 @@ data SwaggerOperation = SwaggerOperation
250 } deriving (Eq, Show, Generic)
253 } deriving (Eq, Show, Generic)
252newtype SwaggerMimeList = SwaggerMimeList { getSwaggerMimeList :: [MediaType] }
255newtype SwaggerMimeList = SwaggerMimeList { getSwaggerMimeList :: [MediaType] }
253− deriving (Eq, Show)
256+ deriving (Eq, Show, Monoid)
255-- | Describes a single operation parameter.
258-- | Describes a single operation parameter.
256-- A unique parameter is defined by a combination of a name and location.
259-- A unique parameter is defined by a combination of a name and location.
@@ -488,7 +491,7 @@ data SwaggerResponses = SwaggerResponses
488 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
491 -- | Any HTTP status code can be used as the property name (one property per HTTP status code).
489 -- Describes the expected response for those HTTP status codes.
492 -- Describes the expected response for those HTTP status codes.
490 , swaggerResponsesResponses :: HashMap HttpStatusCode SwaggerResponse
493 , swaggerResponsesResponses :: HashMap HttpStatusCode SwaggerResponse
491− } deriving (Eq, Show)
494+ } deriving (Eq, Show, Generic)
493type HttpStatusCode = Int
496type HttpStatusCode = Int
@@ -616,10 +619,127 @@ data SwaggerExternalDocs = SwaggerExternalDocs
617 -- | The URL for the target documentation.
620 -- | The URL for the target documentation.
618 , swaggerExternalDocsUrl :: URL
621 , swaggerExternalDocsUrl :: URL
619− } deriving (Eq, Show)
622+ } deriving (Eq, Show, Generic)
621newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON)
624newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON)
626+-- =======================================================================
628+-- =======================================================================
630+instance Monoid Swagger where
631+ mempty = genericMempty
632+ mappend = genericMappend
634+instance Monoid SwaggerInfo where
635+ mempty = genericMempty
636+ mappend = genericMappend
638+instance Monoid SwaggerPaths where
639+ mempty = genericMempty
640+ mappend = genericMappend
642+instance Monoid SwaggerPathItem where
643+ mempty = genericMempty
644+ mappend = genericMappend
646+instance Monoid SwaggerSchema where
647+ mempty = genericMempty
648+ mappend = genericMappend
650+instance Monoid SwaggerSchemaCommon where
651+ mempty = genericMempty
652+ mappend = genericMappend
654+instance Monoid SwaggerParameter where
655+ mempty = genericMempty
656+ mappend = genericMappend
658+instance Monoid SwaggerParameterOtherSchema where
659+ mempty = genericMempty
660+ mappend = genericMappend
662+instance Monoid SwaggerResponses where
663+ mempty = genericMempty
664+ mappend = genericMappend
666+instance Monoid SwaggerResponse where
667+ mempty = genericMempty
668+ mappend = genericMappend
670+instance Monoid SwaggerExternalDocs where
671+ mempty = genericMempty
672+ mappend = genericMappend
674+instance Monoid SwaggerOperation where
675+ mempty = genericMempty
676+ mappend = genericMappend
678+-- =======================================================================
679+-- SwaggerMonoid helper instances
680+-- =======================================================================
682+instance SwaggerMonoid SwaggerInfo
683+instance SwaggerMonoid SwaggerPaths
684+instance SwaggerMonoid SwaggerPathItem
685+instance SwaggerMonoid SwaggerSchema
686+instance SwaggerMonoid SwaggerSchemaCommon
687+instance SwaggerMonoid SwaggerParameter
688+instance SwaggerMonoid SwaggerParameterOtherSchema
689+instance SwaggerMonoid SwaggerResponses
690+instance SwaggerMonoid SwaggerResponse
691+instance SwaggerMonoid SwaggerExternalDocs
692+instance SwaggerMonoid SwaggerOperation
694+instance SwaggerMonoid SwaggerMimeList
695+deriving instance SwaggerMonoid URL
697+instance SwaggerMonoid SwaggerSchemaType where
698+ swaggerMempty = SwaggerSchemaNull
699+ swaggerMappend _ y = y
701+instance SwaggerMonoid SwaggerParameterType where
702+ swaggerMempty = SwaggerParamString
703+ swaggerMappend _ y = y
705+instance SwaggerMonoid SwaggerParameterLocation where
706+ swaggerMempty = SwaggerParameterQuery
707+ swaggerMappend _ y = y
709+instance SwaggerMonoid (HashMap Text SwaggerSchema) where
710+ swaggerMempty = HashMap.empty
711+ swaggerMappend = HashMap.unionWith mappend
713+instance SwaggerMonoid (HashMap Text SwaggerParameter) where
714+ swaggerMempty = HashMap.empty
715+ swaggerMappend = HashMap.unionWith mappend
717+instance SwaggerMonoid (HashMap Text SwaggerResponse) where
718+ swaggerMempty = HashMap.empty
719+ swaggerMappend = flip HashMap.union
721+instance SwaggerMonoid (HashMap Text SwaggerSecurityScheme) where
722+ swaggerMempty = HashMap.empty
723+ swaggerMappend = flip HashMap.union
725+instance SwaggerMonoid (HashMap FilePath SwaggerPathItem) where
726+ swaggerMempty = HashMap.empty
727+ swaggerMappend = HashMap.unionWith mappend
729+instance SwaggerMonoid (HashMap HeaderName SwaggerHeader) where
730+ swaggerMempty = HashMap.empty
731+ swaggerMappend = flip HashMap.union
733+instance SwaggerMonoid (HashMap HttpStatusCode SwaggerResponse) where
734+ swaggerMempty = HashMap.empty
735+ swaggerMappend = flip HashMap.union
737+instance SwaggerMonoid SwaggerParameterSchema where
738+ swaggerMempty = SwaggerParameterOther swaggerMempty
739+ swaggerMappend (SwaggerParameterBody x) (SwaggerParameterBody y) = SwaggerParameterBody (swaggerMappend x y)
740+ swaggerMappend (SwaggerParameterOther x) (SwaggerParameterOther y) = SwaggerParameterOther (swaggerMappend x y)
741+ swaggerMappend _ y = y
623-- =======================================================================
743-- =======================================================================
624-- TH derived ToJSON and FromJSON instances
744-- TH derived ToJSON and FromJSON instances
625-- =======================================================================
745-- =======================================================================
@@ -832,3 +952,4 @@ instance FromJSON SwaggerResponse where
832instance FromJSON SwaggerOperation where
952instance FromJSON SwaggerOperation where
833 parseJSON = genericParseJSON (jsonPrefix "swaggerOperation")
953 parseJSON = genericParseJSON (jsonPrefix "swaggerOperation")
834 `withDefaults` [ "deprecated" .= False ]
954 `withDefaults` [ "deprecated" .= False ]