Skip to content

Commit e3a29ad

Browse files
author
Szabo Gergely
authored
Fix servant-docs code sample in README (#1335)
1 parent 1760cc8 commit e3a29ad

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

servant-docs/README.md

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,56 @@
22

33
![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)
44

5-
Generate API docs for your *servant* webservice. Feel free to also take a look at [servant-pandoc](https://github.com/mpickering/servant-pandoc) which uses this package to target a broad range of output formats using the excellent **pandoc**.
5+
Generate API docs for your _servant_ webservice. Feel free to also take a look at [servant-pandoc](https://github.com/mpickering/servant-pandoc) which uses this package to target a broad range of output formats using the excellent **pandoc**.
66

77
## Example
88

99
See [here](https://github.com/haskell-servant/servant/blob/master/servant-docs/example/greet.md) for the output of the following program.
1010

11-
``` haskell
11+
```haskell
1212
{-# LANGUAGE DataKinds #-}
13-
{-# LANGUAGE PolyKinds #-}
14-
{-# LANGUAGE TypeFamilies #-}
1513
{-# LANGUAGE DeriveGeneric #-}
16-
{-# LANGUAGE TypeOperators #-}
1714
{-# LANGUAGE FlexibleInstances #-}
15+
{-# LANGUAGE MultiParamTypeClasses #-}
1816
{-# LANGUAGE OverloadedStrings #-}
17+
{-# LANGUAGE PolyKinds #-}
18+
{-# LANGUAGE TypeFamilies #-}
19+
{-# LANGUAGE TypeOperators #-}
1920

20-
import Data.Proxy
21-
import Data.Text
21+
import Data.Aeson (FromJSON, ToJSON)
22+
import Data.Proxy (Proxy (..))
23+
import Data.String.Conversions (cs)
24+
import Data.Text (Text)
25+
import GHC.Generics (Generic)
26+
import Servant.API
27+
( (:<|>),
28+
(:>),
29+
Capture,
30+
Delete,
31+
Get,
32+
JSON,
33+
MimeRender,
34+
PlainText,
35+
Post,
36+
QueryParam,
37+
ReqBody,
38+
mimeRender,
39+
)
2240
import Servant.Docs
41+
( API,
42+
DocCapture (..),
43+
DocQueryParam (..),
44+
ParamKind (..),
45+
ToCapture,
46+
ToParam,
47+
ToSample,
48+
docs,
49+
markdown,
50+
singleSample,
51+
toCapture,
52+
toParam,
53+
toSamples,
54+
)
2355

2456
-- our type for a Greeting message
2557
data Greet = Greet { _msg :: Text }
@@ -29,22 +61,23 @@ data Greet = Greet { _msg :: Text }
2961
-- 'MimeRender' instance for 'JSON'.
3062
instance FromJSON Greet
3163
instance ToJSON Greet
64+
instance ToSample ()
3265

3366
-- We can also implement 'MimeRender' explicitly for additional formats.
3467
instance MimeRender PlainText Greet where
3568
mimeRender Proxy (Greet s) = "<h1>" <> cs s <> "</h1>"
3669

3770
-- we provide a sample value for the 'Greet' type
3871
instance ToSample Greet where
39-
toSample = Just g
40-
72+
toSamples _ = singleSample g
4173
where g = Greet "Hello, haskeller!"
4274

4375
instance ToParam (QueryParam "capital" Bool) where
4476
toParam _ =
4577
DocQueryParam "capital"
4678
["true", "false"]
4779
"Get the greeting message in uppercase (true) or not (false). Default is false."
80+
Normal
4881

4982
instance ToCapture (Capture "name" Text) where
5083
toCapture _ = DocCapture "name" "name of the person to greet"
@@ -55,8 +88,8 @@ instance ToCapture (Capture "greetid" Text) where
5588
-- API specification
5689
type TestApi =
5790
"hello" :> Capture "name" Text :> QueryParam "capital" Bool :> Get '[JSON,PlainText] Greet
58-
:<|> "greet" :> RQBody '[JSON] Greet :> Post '[JSON] Greet
59-
:<|> "delete" :> Capture "greetid" Text :> Delete '[] ()
91+
:<|> "greet" :> ReqBody '[JSON] Greet :> Post '[JSON] Greet
92+
:<|> "delete" :> Capture "greetid" Text :> Delete '[JSON] ()
6093

6194
testApi :: Proxy TestApi
6295
testApi = Proxy

0 commit comments

Comments
 (0)