Skip to content
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 OperationId combinator. #1277

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.d/operation-id-combinator
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
synopsis: Add OperationId combinator.
prs: #1277
issues: #1276

description: {

Adds a combinator that allows overriding the `operationId` field of a Swagger entry.

}
20 changes: 14 additions & 6 deletions servant-client-core/src/Servant/Client/Core/HasClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ import Servant.API
EmptyAPI, FramingRender (..), FramingUnrender (..),
FromSourceIO (..), Header', Headers (..), HttpVersion,
IsSecure, MimeRender (mimeRender),
MimeUnrender (mimeUnrender), NoContent (NoContent), QueryFlag,
QueryParam', QueryParams, Raw, ReflectMethod (..), RemoteHost,
ReqBody', SBoolI, Stream, StreamBody', Summary, ToHttpApiData,
ToSourceIO (..), Vault, Verb, NoContentVerb, WithNamedContext,
contentType, getHeadersHList, getResponse, toQueryParam,
toUrlPiece)
MimeUnrender (mimeUnrender), NoContent (NoContent), OperationId,
QueryFlag, QueryParam', QueryParams, Raw, ReflectMethod (..),
RemoteHost, ReqBody', SBoolI, Stream, StreamBody', Summary,
ToHttpApiData, ToSourceIO (..), Vault, Verb, NoContentVerb,
WithNamedContext, contentType, getHeadersHList, getResponse,
toQueryParam, toUrlPiece)
import Servant.API.ContentTypes
(contentTypes)
import Servant.API.Modifiers
Expand Down Expand Up @@ -404,6 +404,14 @@ instance HasClient m api => HasClient m (Description desc :> api) where

hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl

-- | Ignore @'Description'@ in client functions.
instance (KnownSymbol opid, HasClient m api) => HasClient m (OperationId opid :> api) where
type Client m (OperationId opid :> api) = Client m api

clientWithRoute pm Proxy = clientWithRoute pm (Proxy :: Proxy api)

hoistClientMonad pm _ f cl = hoistClientMonad pm (Proxy :: Proxy api) f cl

-- | If you use a 'QueryParam' in one of your endpoints in your API,
-- the corresponding querying function will automatically take
-- an additional argument of the type specified by your 'QueryParam',
Expand Down
17 changes: 12 additions & 5 deletions servant-server/src/Servant/Server/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ import Servant.API
((:<|>) (..), (:>), Accept (..), BasicAuth, Capture',
CaptureAll, Description, EmptyAPI, FramingRender (..),
FramingUnrender (..), FromSourceIO (..), Header', If,
IsSecure (..), QueryFlag, QueryParam', QueryParams, Raw,
ReflectMethod (reflectMethod), RemoteHost, ReqBody',
SBool (..), SBoolI (..), SourceIO, Stream, StreamBody',
Summary, ToSourceIO (..), Vault, Verb, NoContentVerb,
WithNamedContext)
IsSecure (..), OperationId, QueryFlag, QueryParam',
QueryParams, Raw, ReflectMethod (reflectMethod), RemoteHost,
ReqBody', SBool (..), SBoolI (..), SourceIO, Stream,
StreamBody', Summary, ToSourceIO (..), Vault, Verb,
NoContentVerb, WithNamedContext)
import Servant.API.ContentTypes
(AcceptHeader (..), AllCTRender (..), AllCTUnrender (..),
AllMime, MimeRender (..), MimeUnrender (..), canHandleAcceptH,
Expand Down Expand Up @@ -718,6 +718,13 @@ instance HasServer api ctx => HasServer (Description desc :> api) ctx where
route _ = route (Proxy :: Proxy api)
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt s

-- | Ignore @'OperationId'@ in server handlers.
instance HasServer api ctx => HasServer (OperationId opid :> api) ctx where
type ServerT (OperationId opid :> api) m = ServerT api m

route _ = route (Proxy :: Proxy api)
hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt s

-- | Singleton type representing a server that serves an empty API.
data EmptyServer = EmptyServer deriving (Typeable, Eq, Show, Bounded, Enum)

Expand Down
2 changes: 1 addition & 1 deletion servant/src/Servant/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import Servant.API.ContentTypes
MimeUnrender (..), NoContent (NoContent), OctetStream,
PlainText)
import Servant.API.Description
(Description, Summary)
(Description, OperationId, Summary)
import Servant.API.Empty
(EmptyAPI (..))
import Servant.API.Experimental.Auth
Expand Down
12 changes: 12 additions & 0 deletions servant/src/Servant/API/Description.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Servant.API.Description (
-- * Combinators
Description,
Summary,
OperationId,
-- * Used as modifiers
FoldDescription,
FoldDescription',
Expand Down Expand Up @@ -46,6 +47,17 @@ data Summary (sym :: Symbol)
data Description (sym :: Symbol)
deriving (Typeable)

-- | Specify the operation ID used by swagger.
--
-- Example:
--
-- >>> :{
--type MyApi = OperationId "getBooks"
-- :> Get '[JSON] Book
-- :}
data OperationId (sym :: Symbol)
deriving (Typeable)

-- | Fold list of modifiers to extract description as a type-level String.
--
-- >>> :kind! FoldDescription '[]
Expand Down