Skip to content

Commit 0ac4728

Browse files
committed
Support for MultiVerb
This commit brings support for MultiVerb, introduced in servant-0.20.3.0. The cabal.project file is used to depend on the pre-release.
1 parent 59b90df commit 0ac4728

File tree

6 files changed

+402
-166
lines changed

6 files changed

+402
-166
lines changed

cabal.project

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ packages:
22
servant-openapi3.cabal,
33
example/example.cabal
44
tests: true
5+
6+
source-repository-package
7+
type: git
8+
location: https://github.com/haskell-servant/servant
9+
tag: servant-0.20.3.0
10+
subdir:
11+
./servant
12+
./servant-server
13+
./servant-client
14+
./servant-client-core

example/example.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ library
4141
, openapi3
4242
, text
4343
, time
44+
, generics-sop
4445
default-language: Haskell2010
4546

4647
executable swagger-server

example/src/Todo.hs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55
{-# LANGUAGE OverloadedStrings #-}
66
{-# LANGUAGE TypeOperators #-}
7+
{-# LANGUAGE DerivingVia #-}
78
module Todo where
89

910
import Control.Lens
@@ -18,6 +19,8 @@ import Data.Typeable (Typeable)
1819
import GHC.Generics
1920
import Servant
2021
import Servant.OpenApi
22+
import qualified Generics.SOP as GSOP
23+
import Servant.API.MultiVerb
2124

2225
todoAPI :: Proxy TodoAPI
2326
todoAPI = Proxy
@@ -28,7 +31,7 @@ type TodoAPI
2831
:<|> "todo" :> ReqBody '[JSON] Todo :> Post '[JSON] TodoId
2932
:<|> "todo" :> Capture "id" TodoId :> Get '[JSON] Todo
3033
:<|> "todo" :> Capture "id" TodoId :> ReqBody '[JSON] Todo :> Put '[JSON] TodoId
31-
34+
:<|> "todo" :> "choices" :> MultipleChoicesInt
3235
-- | API for serving @swagger.json@.
3336
type SwaggerAPI = "swagger.json" :> Get '[JSON] OpenApi
3437

@@ -71,3 +74,28 @@ server = return todoSwagger :<|> error "not implemented"
7174
-- | Output generated @swagger.json@ file for the @'TodoAPI'@.
7275
writeSwaggerJSON :: IO ()
7376
writeSwaggerJSON = BL8.writeFile "example/swagger.json" (encodePretty todoSwagger)
77+
78+
type MultiResponses =
79+
'[ RespondEmpty 400 "Negative"
80+
, Respond 200 "Even number" Bool
81+
, Respond 200 "Odd number" Int
82+
]
83+
84+
-- All possible return types
85+
data MultiResult
86+
= NegativeNumber
87+
| Even Bool
88+
| Odd Int
89+
deriving stock (Generic)
90+
deriving (AsUnion MultiResponses)
91+
via GenericAsUnion MultiResponses MultiResult
92+
93+
instance GSOP.Generic MultiResult
94+
95+
type MultipleChoicesInt =
96+
Capture "int" Int
97+
:> MultiVerb
98+
'GET
99+
'[JSON]
100+
MultiResponses
101+
MultiResult

0 commit comments

Comments
 (0)