git.iko.soy/typeable/openapi3
on master help sign in
Add IsString instances for some types
ee1ebb7Nickolay 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,4 +1,5 @@
1{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE DeriveTraversable #-}
4{-# LANGUAGE DeriveTraversable #-}
4{-# LANGUAGE FlexibleContexts #-}
5{-# LANGUAGE FlexibleContexts #-}
@@ -23,7 +24,7 @@ import Data.Map (Map)
23import qualified Data.Map as Map
24import qualified Data.Map as Map
24import Data.Monoid
25import Data.Monoid
25import Data.Scientific (Scientific)
26import Data.Scientific (Scientific)
26−import Data.String (fromString)
27+import Data.String (IsString(..))
27import Data.Text (Text)
28import Data.Text (Text)
28import qualified Data.Text as Text
29import qualified Data.Text as Text
29import GHC.Generics (Generic)
30import GHC.Generics (Generic)
@@ -145,12 +146,18 @@ data License = License
145 , _licenseUrl :: Maybe URL
146 , _licenseUrl :: Maybe URL
146 } deriving (Eq, Show, Generic, Data, Typeable)
147 } deriving (Eq, Show, Generic, Data, Typeable)
147
148
149+instance IsString License where
150+ fromString s = License (fromString s) Nothing
151+
148-- | The host (name or ip) serving the API. It MAY include a port.
152-- | The host (name or ip) serving the API. It MAY include a port.
149data Host = Host
153data Host = Host
150 { _hostName :: HostName -- ^ Host name.
154 { _hostName :: HostName -- ^ Host name.
151 , _hostPort :: Maybe PortNumber -- ^ Optional port.
155 , _hostPort :: Maybe PortNumber -- ^ Optional port.
152 } deriving (Eq, Show, Generic, Typeable)
156 } deriving (Eq, Show, Generic, Typeable)
153
157
158+instance IsString Host where
159+ fromString s = Host s Nothing
160+
154hostConstr :: Constr
161hostConstr :: Constr
155hostConstr = mkConstr hostDataType "Host" [] Prefix
162hostConstr = mkConstr hostDataType "Host" [] Prefix
156
163
@@ -582,6 +589,9 @@ data Response = Response
582 , _responseExamples :: Maybe Example
589 , _responseExamples :: Maybe Example
583 } deriving (Eq, Show, Generic, Data, Typeable)
590 } deriving (Eq, Show, Generic, Data, Typeable)
584
591
592+instance IsString Response where
593+ fromString s = Response (fromString s) Nothing mempty Nothing
594+
585type HeaderName = Text
595type HeaderName = Text
586
596
587data Header = Header
597data Header = Header
@@ -680,6 +690,9 @@ data Tag = Tag
680 , _tagExternalDocs :: Maybe ExternalDocs
690 , _tagExternalDocs :: Maybe ExternalDocs
681 } deriving (Eq, Show, Generic, Data, Typeable)
691 } deriving (Eq, Show, Generic, Data, Typeable)
682
692
693+instance IsString Tag where
694+ fromString s = Tag (fromString s) Nothing Nothing
695+
683-- | Allows referencing an external resource for extended documentation.
696-- | Allows referencing an external resource for extended documentation.
684data ExternalDocs = ExternalDocs
697data ExternalDocs = ExternalDocs
685 { -- | A short description of the target documentation.
698 { -- | A short description of the target documentation.
@@ -698,7 +711,10 @@ newtype Reference = Reference { getReference :: Text }
698data Referenced a
711data Referenced a
699 = Ref Reference
712 = Ref Reference
700 | Inline a
713 | 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
702
718
703newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
719newtype URL = URL { getUrl :: Text } deriving (Eq, Show, ToJSON, FromJSON, Data, Typeable)
704
720
src/Data/Swagger/Lens.hs+2
@@ -45,6 +45,8 @@ makeFields ''ExternalDocs
45makePrisms ''ParamAnySchema
45makePrisms ''ParamAnySchema
46-- ** 'SecuritySchemeType' prisms
46-- ** 'SecuritySchemeType' prisms
47makePrisms ''SecuritySchemeType
47makePrisms ''SecuritySchemeType
48+-- ** 'Referenced' prisms
49+makePrisms ''Referenced
48
50
49-- ** 'SwaggerItems' prisms
51-- ** 'SwaggerItems' prisms
50
52
test/Data/SwaggerSpec.hs+6−5
@@ -95,7 +95,8 @@ contactExampleJSON = [aesonQQ|
95-- =======================================================================
95-- =======================================================================
96
96
97licenseExample :: License
97licenseExample :: License
98+licenseExample = "Apache 2.0"
98−licenseExample = License "Apache 2.0" (Just (URL "http://www.apache.org/licenses/LICENSE-2.0.html"))
99+ & url ?~ URL "http://www.apache.org/licenses/LICENSE-2.0.html"
99
100
100licenseExampleJSON :: Value
101licenseExampleJSON :: Value
101licenseExampleJSON = [aesonQQ|
102licenseExampleJSON = [aesonQQ|
@@ -136,9 +137,8 @@ operationExample = mempty
136 & schema .~ ParamOther (stringSchema ParamFormData)
137 & schema .~ ParamOther (stringSchema ParamFormData)
137 ]
138 ]
138
139
139− & responses .~ (mempty & responses .~
140− [ (200, Inline (mempty & description .~ "Pet updated."))
140+ & at 200 ?~ "Pet updated."
141− , (405, Inline (mempty & description .~ "Invalid input")) ])
141+ & at 405 ?~ "Invalid input"
142 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
142 & security .~ [SecurityRequirement [("petstore_auth", ["write:pets", "read:pets"])]]
143 where
143 where
144 stringSchema :: ParamLocation -> ParamOtherSchema
144 stringSchema :: ParamLocation -> ParamOtherSchema
@@ -476,7 +476,8 @@ swaggerExample = mempty
476 & info .~ (mempty
476 & info .~ (mempty
477 & version .~ "1.0"
477 & version .~ "1.0"
478 & title .~ "Todo API"
478 & title .~ "Todo API"
479− & license ?~ License "MIT" (Just (URL "http://mit.com"))
479+ & license ?~ "MIT"
480+ & license._Just.url ?~ URL "http://mit.com"
480 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
481 & description ?~ "This is a an API that tests servant-swagger support for a Todo API")
481 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
482 & paths.at "/todo/{id}" ?~ (mempty & get ?~ ((mempty :: Operation)
482 & at 200 ?~ Inline (mempty
483 & at 200 ?~ Inline (mempty