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.
This commit is contained in:
Samuel Gélineau 2021-06-08 14:28:19 -04:00 committed by GitHub
parent da8e64b534
commit 0cb2d603c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 4 deletions

View file

@ -52,11 +52,11 @@
``` ```
## GET /capture/:foo ## GET /capture/:bar
### Captures: ### Captures:
- *foo*: Capture foo Int - *bar*: example description
### Response: ### Response:

View file

@ -862,7 +862,7 @@ instance HasDocs EmptyAPI where
-- | @"books" :> 'Capture' "isbn" Text@ will appear as -- | @"books" :> 'Capture' "isbn" Text@ will appear as
-- @/books/:isbn@ in the docs. -- @/books/:isbn@ in the docs.
instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs api) instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs api)
=> HasDocs (Capture' mods sym a :> api) where => HasDocs (Capture' '[] sym a :> api) where
docsFor Proxy (endpoint, action) = docsFor Proxy (endpoint, action) =
docsFor subApiP (endpoint', action') docsFor subApiP (endpoint', action')
@ -874,6 +874,28 @@ instance (KnownSymbol sym, ToCapture (Capture sym a), HasDocs api)
endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint
symP = Proxy :: Proxy sym symP = Proxy :: Proxy sym
instance (KnownSymbol descr, KnownSymbol sym, HasDocs api)
=> HasDocs (Capture' (Description descr ': mods) sym a :> api) where
docsFor Proxy (endpoint, action) =
docsFor subApiP (endpoint', action')
where subApiP = Proxy :: Proxy api
docCapture = DocCapture (symbolVal symP) (symbolVal descrP)
action' = over captures (|> docCapture) action
endpoint' = over path (\p -> p ++ [":" ++ symbolVal symP]) endpoint
descrP = Proxy :: Proxy descr
symP = Proxy :: Proxy sym
instance {-# OVERLAPPABLE #-} HasDocs (Capture' mods sym a :> api)
=> HasDocs (Capture' (mod ': mods) sym a :> api) where
docsFor Proxy =
docsFor apiP
where apiP = Proxy :: Proxy (Capture' mods sym a :> api)
-- | @"books" :> 'CaptureAll' "isbn" Text@ will appear as -- | @"books" :> 'CaptureAll' "isbn" Text@ will appear as
-- @/books/:isbn@ in the docs. -- @/books/:isbn@ in the docs.

View file

@ -48,7 +48,7 @@ comprehensiveAPIWithoutStreaming = Proxy
type ComprehensiveAPIWithoutStreamingOrRaw' endpoint = type ComprehensiveAPIWithoutStreamingOrRaw' endpoint =
GET GET
:<|> "get-int" :> Get '[JSON] Int :<|> "get-int" :> Get '[JSON] Int
:<|> "capture" :> Capture' '[Description "example description"] "foo" Int :> GET :<|> "capture" :> Capture' '[Description "example description"] "bar" Int :> GET
:<|> "capture-lenient" :> Capture' '[Lenient] "foo" Int :> GET :<|> "capture-lenient" :> Capture' '[Lenient] "foo" Int :> GET
:<|> "header" :> Header "foo" Int :> GET :<|> "header" :> Header "foo" Int :> GET
:<|> "header-lenient" :> Header' '[Required, Lenient] "bar" Int :> GET :<|> "header-lenient" :> Header' '[Required, Lenient] "bar" Int :> GET