git.iko.soy/typeable/swagger2
on master help sign in
Add IsString instances for some types
commitee1ebb72ddd98693705c5208cc0a2ff48ca25d3d authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateTue 26 Jan 2016, 15:25 (11 years ago) parentab65c4a
4 files changed+30−10
src/Data/Swagger.hs+4−3 src/Data/Swagger/Internal.hs+18−2 src/Data/Swagger/Lens.hs+2 test/Data/SwaggerSpec.hs+6−5
src/Data/Swagger.hs+4−3
@@ -168,9 +168,10 @@ import Data.Swagger.Internal
168168-- & paths .~
169169-- [ ("/user", mempty & get ?~ (mempty
170170-- & 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")) ]
172173-- :}
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\"]}}}}"
174175--
175176-- In the snippet above we declare API with a single path @/user@ providing method @GET@
176177-- which produces @application/json@ output and should respond with code @200@ and body specified
@@ -202,7 +203,7 @@ import Data.Swagger.Internal
202203--
203204-- >>> :{
204205-- encode $ (mempty :: Operation)
205−-- & at 404 ?~ Inline (mempty & description .~ "Not found")
206+-- & at 404 ?~ "Not found"
206207-- :}
207208-- "{\"responses\":{\"404\":{\"description\":\"Not found\"}}}"
208209--
src/Data/Swagger/Internal.hs+18−2
@@ -1,4 +1,5 @@
11{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveFunctor #-}
23{-# LANGUAGE DeriveGeneric #-}
34{-# LANGUAGE DeriveTraversable #-}
45{-# LANGUAGE FlexibleContexts #-}
@@ -23,7 +24,7 @@ import Data.Map (Map)
2324import qualified Data.Map as Map
2425import Data.Monoid
2526import Data.Scientific (Scientific)
26−import Data.String (fromString)
27+import Data.String (IsString(..))
2728import Data.Text (Text)
2829import qualified Data.Text as Text
2930import GHC.Generics (Generic)
@@ -145,12 +146,18 @@ data License = License
145146 , _licenseUrl :: Maybe URL
146147 } deriving (Eq, Show, Generic, Data, Typeable)
147148
149+instance IsString License where
150+ fromString s = License (fromString s) Nothing
151+
148152-- | The host (name or ip) serving the API. It MAY include a port.
149153data Host = Host
150154 { _hostName :: HostName -- ^ Host name.
151155 , _hostPort :: Maybe PortNumber -- ^ Optional port.
152156 } deriving (Eq, Show, Generic, Typeable)
153157
158+instance IsString Host where
159+ fromString s = Host s Nothing
160+
154161hostConstr :: Constr
155162hostConstr = mkConstr hostDataType "Host" [] Prefix
156163
@@ -582,6 +589,9 @@ data Response = Response
582589 , _responseExamples :: Maybe Example
583590 } deriving (Eq, Show, Generic, Data, Typeable)
584591
592+instance IsString Response where
593+ fromString s = Response (fromString s) Nothing mempty Nothing
594+
585595type HeaderName = Text
586596
587597data Header = Header
@@ -680,6 +690,9 @@ data Tag = Tag
680690 , _tagExternalDocs :: Maybe ExternalDocs
681691 } deriving (Eq, Show, Generic, Data, Typeable)
682692
693+instance IsString Tag where
694+ fromString s = Tag (fromString s) Nothing Nothing
695+
683696-- | Allows referencing an external resource for extended documentation.
684697data ExternalDocs = ExternalDocs
685698 { -- | A short description of the target documentation.
@@ -698,7 +711,10 @@ newtype Reference = Reference { getReference :: Text }
698711data Referenced a
699712 = Ref Reference
700713 | Inline a
701− deriving (Eq, Show, Data, Typeable)
714+ deriving (Eq, Show, Functor, Data, Typeable)
715+
716+instance IsString a => IsString (Referenced a) where
717+ fromString = Inline . fromString
702718
703719newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
704720
src/Data/Swagger/Lens.hs+2
@@ -45,6 +45,8 @@ makeFields ''ExternalDocs
4545makePrisms ''ParamAnySchema
4646-- ** 'SecuritySchemeType' prisms
4747makePrisms ''SecuritySchemeType
48+-- ** 'Referenced' prisms
49+makePrisms ''Referenced
4850
4951-- ** 'SwaggerItems' prisms
5052
test/Data/SwaggerSpec.hs+6−5
@@ -95,7 +95,8 @@ contactExampleJSON = [aesonQQ|
9595-- =======================================================================
9696
9797licenseExample :: License
98−licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
98+licenseExample = "Apache 2.0"
99+ & url ?~ URL "http://www.apache.org/licenses/LICENSE-2.0.html"
99100
100101licenseExampleJSON :: Value
101102licenseExampleJSON = [aesonQQ|
@@ -136,9 +137,8 @@ operationExample = mempty
136137 & schema .~ ParamOther (stringSchema ParamFormData)
137138 ]
138139
139− & responses .~ (mempty & responses .~
140− [ (200, Inline (mempty & description .~ "Pet updated."))
141− , (405, Inline (mempty & description .~ "Invalid input")) ])
140+ & at 200 ?~ "Pet updated."
141+ & at 405 ?~ "Invalid input"
142142 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
143143 where
144144 stringSchema :: ParamLocation -> ParamOtherSchema
@@ -476,7 +476,8 @@ swaggerExample = mempty
476476 & info .~ (mempty
477477 & version .~ "1.0"
478478 & title .~ "Todo API"
479− & license ?~ License "MIT" (Just (URL "http://mit.com"))
479+ & license ?~ "MIT"
480+ & license._Just.url ?~ URL "http://mit.com"
480481 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
481482 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
482483 & at 200 ?~ Inline (mempty