4
4
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5
5
{-# LANGUAGE OverloadedStrings #-}
6
6
{-# LANGUAGE TypeOperators #-}
7
+ {-# LANGUAGE DerivingVia #-}
7
8
module Todo where
8
9
9
10
import Control.Lens
@@ -18,6 +19,8 @@ import Data.Typeable (Typeable)
18
19
import GHC.Generics
19
20
import Servant
20
21
import Servant.OpenApi
22
+ import qualified Generics.SOP as GSOP
23
+ import Servant.API.MultiVerb
21
24
22
25
todoAPI :: Proxy TodoAPI
23
26
todoAPI = Proxy
@@ -28,7 +31,7 @@ type TodoAPI
28
31
:<|> " todo" :> ReqBody '[JSON ] Todo :> Post '[JSON ] TodoId
29
32
:<|> " todo" :> Capture " id" TodoId :> Get '[JSON ] Todo
30
33
:<|> " todo" :> Capture " id" TodoId :> ReqBody '[JSON ] Todo :> Put '[JSON ] TodoId
31
-
34
+ :<|> " todo " :> " choices " :> MultipleChoicesInt
32
35
-- | API for serving @swagger.json@.
33
36
type SwaggerAPI = " swagger.json" :> Get '[JSON ] OpenApi
34
37
@@ -71,3 +74,28 @@ server = return todoSwagger :<|> error "not implemented"
71
74
-- | Output generated @swagger.json@ file for the @'TodoAPI'@.
72
75
writeSwaggerJSON :: IO ()
73
76
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