git.iko.soy/typeable/swagger2
on master help sign in
Merge pull request #47 from GetShopTV/isstring-#43
104bce6Nickolay Kudasov
src/Data/Swagger.hs+4−3
@@ -168,9 +168,10 @@ import Data.Swagger.Internal
168-- & paths .~
168-- & paths .~
169-- [ ("/user", mempty & get ?~ (mempty
169-- [ ("/user", mempty & get ?~ (mempty
170-- & produces ?~ MimeList ["application/json"]
170-- & produces ?~ MimeList ["application/json"]
171−-- & at 200 ?~ Inline (mempty & schema ?~ Ref (Reference "User")))) ]
171+-- & at 200 ?~ ("OK" & _Inline.schema ?~ Ref (Reference "User"))
172+-- & at 404 ?~ "User info not found")) ]
172-- :}
173-- :}
173−-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"\"}},\"produces\":[\"application/json\"]}}}}"
174+-- "{\"swagger\":\"2.0\",\"info\":{\"version\":\"\",\"title\":\"\"},\"definitions\":{\"User\":{\"type\":\"string\"}},\"paths\":{\"/user\":{\"get\":{\"responses\":{\"404\":{\"description\":\"User info not found\"},\"200\":{\"schema\":{\"$ref\":\"#/definitions/User\"},\"description\":\"OK\"}},\"produces\":[\"application/json\"]}}}}"
174--
175--
175-- In the snippet above we declare API with a single path @/user@ providing method @GET@
176-- In the snippet above we declare API with a single path @/user@ providing method @GET@
176-- which produces @application/json@ output and should respond with code @200@ and body specified
177-- which produces @application/json@ output and should respond with code @200@ and body specified
@@ -202,7 +203,7 @@ import Data.Swagger.Internal
202--
203--
203-- >>> :{
204-- >>> :{
204-- encode $ (mempty :: Operation)
205-- encode $ (mempty :: Operation)
205−-- & at 404 ?~ Inline (mempty & description .~ "Not found")
206+-- & at 404 ?~ "Not found"
206-- :}
207-- :}
207-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
208-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
208--
209--
src/Data/Swagger/Internal.hs+18−2
@@ -1,5 +1,6 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3+{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE DeriveTraversable #-}
5{-# LANGUAGE DeriveTraversable #-}
5{-# LANGUAGE FlexibleContexts #-}
6{-# LANGUAGE FlexibleContexts #-}
@@ -28,7 +29,7 @@ import Data.Map (Map)
28import qualified Data.Map as Map
29import qualified Data.Map as Map
29import Data.Monoid
30import Data.Monoid
30import Data.Scientific (Scientific)
31import Data.Scientific (Scientific)
31−import Data.String (fromString)
32+import Data.String (IsString(..))
32import Data.Text (Text)
33import Data.Text (Text)
33import qualified Data.Text as Text
34import qualified Data.Text as Text
34import GHC.Generics (Generic)
35import GHC.Generics (Generic)
@@ -150,12 +151,18 @@ data License = License
150 , _licenseUrl :: Maybe URL
151 , _licenseUrl :: Maybe URL
151 } deriving (Eq, Show, Generic, Data, Typeable)
152 } deriving (Eq, Show, Generic, Data, Typeable)
152
153
154+instance IsString License where
155+ fromString s = License (fromString s) Nothing
156+
153-- | The host (name or ip) serving the API. It MAY include a port.
157-- | The host (name or ip) serving the API. It MAY include a port.
154data Host = Host
158data Host = Host
155 { _hostName :: HostName -- ^ Host name.
159 { _hostName :: HostName -- ^ Host name.
156 , _hostPort :: Maybe PortNumber -- ^ Optional port.
160 , _hostPort :: Maybe PortNumber -- ^ Optional port.
157 } deriving (Eq, Show, Generic, Typeable)
161 } deriving (Eq, Show, Generic, Typeable)
158
162
163+instance IsString Host where
164+ fromString s = Host s Nothing
165+
159hostConstr :: Constr
166hostConstr :: Constr
160hostConstr = mkConstr hostDataType "Host" [] Prefix
167hostConstr = mkConstr hostDataType "Host" [] Prefix
161
168
@@ -591,6 +598,9 @@ data Response = Response
591 , _responseExamples :: Maybe Example
598 , _responseExamples :: Maybe Example
592 } deriving (Eq, Show, Generic, Data, Typeable)
599 } deriving (Eq, Show, Generic, Data, Typeable)
593
600
601+instance IsString Response where
602+ fromString s = Response (fromString s) Nothing mempty Nothing
603+
594type HeaderName = Text
604type HeaderName = Text
595
605
596data Header = Header
606data Header = Header
@@ -689,6 +699,9 @@ data Tag = Tag
689 , _tagExternalDocs :: Maybe ExternalDocs
699 , _tagExternalDocs :: Maybe ExternalDocs
690 } deriving (Eq, Show, Generic, Data, Typeable)
700 } deriving (Eq, Show, Generic, Data, Typeable)
691
701
702+instance IsString Tag where
703+ fromString s = Tag (fromString s) Nothing Nothing
704+
692-- | Allows referencing an external resource for extended documentation.
705-- | Allows referencing an external resource for extended documentation.
693data ExternalDocs = ExternalDocs
706data ExternalDocs = ExternalDocs
694 { -- | A short description of the target documentation.
707 { -- | A short description of the target documentation.
@@ -707,7 +720,10 @@ newtype Reference = Reference { getReference :: Text }
707data Referenced a
720data Referenced a
708 = Ref Reference
721 = Ref Reference
709 | Inline a
722 | Inline a
710− deriving (Eq, Show, Data, Typeable)
723+ deriving (Eq, Show, Functor, Data, Typeable)
724+
725+instance IsString a => IsString (Referenced a) where
726+ fromString = Inline . fromString
711
727
712newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
728newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
713
729
src/Data/Swagger/Lens.hs+2
@@ -48,6 +48,8 @@ makeFields ''ExternalDocs
48makePrisms ''ParamAnySchema
48makePrisms ''ParamAnySchema
49-- ** 'SecuritySchemeType' prisms
49-- ** 'SecuritySchemeType' prisms
50makePrisms ''SecuritySchemeType
50makePrisms ''SecuritySchemeType
51+-- ** 'Referenced' prisms
52+makePrisms ''Referenced
51
53
52-- ** 'SwaggerItems' prisms
54-- ** 'SwaggerItems' prisms
53
55
test/Data/SwaggerSpec.hs+6−5
@@ -98,7 +98,8 @@ contactExampleJSON = [aesonQQ|
98-- =======================================================================
98-- =======================================================================
99
99
100licenseExample :: License
100licenseExample :: License
101+licenseExample = "Apache 2.0"
101−licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
102+ & url ?~ URL "http://www.apache.org/licenses/LICENSE-2.0.html"
102
103
103licenseExampleJSON :: Value
104licenseExampleJSON :: Value
104licenseExampleJSON = [aesonQQ|
105licenseExampleJSON = [aesonQQ|
@@ -139,9 +140,8 @@ operationExample = mempty
139 & schema .~ ParamOther (stringSchema ParamFormData)
140 & schema .~ ParamOther (stringSchema ParamFormData)
140 ]
141 ]
141
142
142− & responses .~ (mempty & responses .~
143− [ (200, Inline (mempty & description .~ "Pet updated."))
143+ & at 200 ?~ "Pet updated."
144− , (405, Inline (mempty & description .~ "Invalid input")) ])
144+ & at 405 ?~ "Invalid input"
145 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
145 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
146 where
146 where
147 stringSchema :: ParamLocation -> ParamOtherSchema
147 stringSchema :: ParamLocation -> ParamOtherSchema
@@ -479,7 +479,8 @@ swaggerExample = mempty
479 & info .~ (mempty
479 & info .~ (mempty
480 & version .~ "1.0"
480 & version .~ "1.0"
481 & title .~ "Todo API"
481 & title .~ "Todo API"
482− & license ?~ License "MIT" (Just (URL "http://mit.com"))
482+ & license ?~ "MIT"
483+ & license._Just.url ?~ URL "http://mit.com"
483 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
484 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
484 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
485 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
485 & at 200 ?~ Inline (mempty
486 & at 200 ?~ Inline (mempty