-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add default endpoint descriptions #60
Comments
I remember there was discussion about Though default endpoint would be still cool |
Hi. I think this library is enormously awesome, except for the fact that it doesn't automatically generate any endpoint descriptions or operation IDs for the endpoints. The swagger specification says that operation IDs may be used by clients to uniquely identify endpoints. This causes libraries like martian to not return any endpoints, because it uses the operation IDs to construct the structure which contains the endpoints(An operation with ID "fooBar" lets me call that endpoint using a keyword I would definitely be willing to look into implementing this, but I'm not familiar with the type-level magic that is going on, and I'm not sure I'm willing to spend too much time on it if it's too complicated. Do you guys have a recommended starting point for adding such combinators? Also, if I were to implement support for, say, a |
Disclaimer: this code is untested. To add a description combinator you can do this: -- | Add a description for a (part of) API.
data Description (sym :: Symbol)
-- | A handy infix version of 'Description'.
type (--|) api sym = Description sym :> api
type MyAPI
= GetResource --| "Get resource by ID."
:<|> DeleteResource --| "Delete resource by ID."
type GetResource = QueryParam "resource_id" ResourceId :> Get '[JSON] Resource
type DeleteResource = QueryParam "resource_id" ResourceId :> Delete '[JSON] Resource
-- or
type MyAPI = GetResource :<|> DeleteResource
type GetResource
= Description "Get resource by ID."
:> QueryParam "resource_id" ResourceId
:> Get '[JSON] Resource
type DeleteResource
= Description "Delete resource by ID."
:> QueryParam "resource_id" ResourceId
:> Delete '[JSON] Resource Instances for {-# LANGUAGE ScopedTypeVariables #-}
instance HasServer api => HasServer (Description desc :> api) where
type ServerT (Description desc :> api) m = ServerT api m
route _ = route (Proxy :: Proxy api)
instance HasClient api => HasClient (Description desc :> api) where
type Client (Description desc :> api) = Client api
clientWithRoute _ = clientWithRoute (Proxy :: Proxy api)
instance HasSwagger api => HasSwagger (Description desc :> api) where
toSwagger _ = toSwagger (Proxy :: Proxy api)
& allOperations.description %~ (Just (symbolVal (Proxy :: Proxy desc)) <>) It is possible that you want |
Hey! For those still interested, the following actually works fine for me (thanks @fizruk): import Control.Lens ((&), (?~))
import GHC.TypeLits (KnownSymbol, Symbol)
import Servant ((:>), HasServer, ServerT)
import Data.Proxy (Proxy (..))
import Data.Swagger (ToSchema)
import qualified Data.Swagger as Swagger
import qualified Data.Text as Text
import qualified GHC.TypeLits as GHC
import qualified Servant
import qualified Servant.Swagger as Servant
data Description (desc :: Symbol)
type (--|) api desc =
Description desc :> api
instance HasServer api ctx => HasServer (Description desc :> api) ctx where
type ServerT (Description desc :> api) m =
ServerT api m
route _ =
Servant.route (Proxy :: Proxy api)
instance (KnownSymbol desc, HasSwagger api) => HasSwagger (Description desc :> api) where
toSwagger _ =
Servant.toSwagger (Proxy :: Proxy api)
& Swagger.allOperations . Swagger.description ?~ Text.pack (GHC.symbolVal (Proxy :: Proxy desc)) |
fix reference to cryptobox-haskell
Hi, Thanks in advance! |
Writing endpoint descriptions is important, but sometimes we don't do this.
This is fine for default Swagger UI, but some more fancy versions (like https://github.com/jensoleg/swagger-ui) don't display the list of endpoints nicely when there are no descriptions:
So maybe we should provide some mechanism to generate at least some sort of descriptions
(semi-)automatically.
The text was updated successfully, but these errors were encountered: