Skip to content

Commit 0cb2d60

Browse files
author
Samuel Gélineau
authored
use Capture Description if available (#1423)
* use Capture Description if available * update golden/comprehensive.md This is technically a breaking change, because if a Capture has both a Description and a ToCapture instance, the Description now takes precedence. Since this Description wasn't doing anything before, I am guessing that most projects currently only use Description to describe their endpoints and not their Captures, and thus that few people will be affected by this breaking change. * test the "no ToCapture instance" case The case in which there is both a Description and a ToCapture instance seems like a corner case. The more interesting cases are the one in which there is a Description but no ToCapture instance, and the case in which there is a ToCapture instance but no description.
1 parent da8e64b commit 0cb2d60

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

servant-docs/golden/comprehensive.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@
5252

5353
```
5454

55-
## GET /capture/:foo
55+
## GET /capture/:bar
5656

5757
### Captures:
5858

59-
- *foo*: Capture foo Int
59+
- *bar*: example description
6060

6161
### Response:
6262

servant-docs/src/Servant/Docs/Internal.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ instance HasDocs EmptyAPI where
862862
-- | @"books" :> 'Capture' "isbn" Text@ will appear as
863863
-- @/books/:isbn@ in the docs.
864864
instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs api)
865-
=> HasDocs (Capture' mods sym a :> api) where
865+
=> HasDocs (Capture' '[] sym a :> api) where
866866

867867
docsFor Proxy (endpoint, action) =
868868
docsFor subApiP (endpoint', action')
@@ -874,6 +874,28 @@ instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs api)
874874
endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint
875875
symP = Proxy :: Proxy sym
876876

877+
instance (KnownSymbol descr, KnownSymbol sym, HasDocs api)
878+
=> HasDocs (Capture' (Description descr ': mods) sym a :> api) where
879+
880+
docsFor Proxy (endpoint, action) =
881+
docsFor subApiP (endpoint', action')
882+
883+
where subApiP = Proxy :: Proxy api
884+
885+
docCapture = DocCapture (symbolVal symP) (symbolVal descrP)
886+
action' = over captures (|> docCapture) action
887+
endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint
888+
descrP = Proxy :: Proxy descr
889+
symP = Proxy :: Proxy sym
890+
891+
instance {-# OVERLAPPABLE #-} HasDocs (Capture' mods sym a :> api)
892+
=> HasDocs (Capture' (mod ': mods) sym a :> api) where
893+
894+
docsFor Proxy =
895+
docsFor apiP
896+
897+
where apiP = Proxy :: Proxy (Capture' mods sym a :> api)
898+
877899

878900
-- | @"books" :> 'CaptureAll' "isbn" Text@ will appear as
879901
-- @/books/:isbn@ in the docs.

servant/src/Servant/Test/ComprehensiveAPI.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ comprehensiveAPIWithoutStreaming = Proxy
4848
type ComprehensiveAPIWithoutStreamingOrRaw' endpoint =
4949
GET
5050
:<|> "get-int" :> Get '[JSON] Int
51-
:<|> "capture" :> Capture' '[Description "example description"] "foo" Int :> GET
51+
:<|> "capture" :> Capture' '[Description "example description"] "bar" Int :> GET
5252
:<|> "capture-lenient" :> Capture' '[Lenient] "foo" Int :> GET
5353
:<|> "header" :> Header "foo" Int :> GET
5454
:<|> "header-lenient" :> Header' '[Required, Lenient] "bar" Int :> GET

0 commit comments

Comments
 (0)