From 8dc6d9a7d4527bc69f76239d5c1f06764941164e Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 16 Jun 2015 10:53:13 +0200 Subject: [PATCH 1/2] servant-docs: Fix docsWith. When adding extra info using using docsWith, the responses vanished from the output. This was due to combineAction being left-biased, and docsWith combining the extra info with the enpoint (in that order). Flipping combineAction solves this. --- servant-docs/src/Servant/Docs/Internal.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/servant-docs/src/Servant/Docs/Internal.hs b/servant-docs/src/Servant/Docs/Internal.hs index c1e5b724..09bf8b40 100644 --- a/servant-docs/src/Servant/Docs/Internal.hs +++ b/servant-docs/src/Servant/Docs/Internal.hs @@ -338,7 +338,7 @@ extraInfo p action = docsWith :: HasDocs layout => [DocIntro] -> ExtraInfo layout -> Proxy layout -> API docsWith intros (ExtraInfo endpoints) p = docs p & apiIntros <>~ intros - & apiEndpoints %~ HM.unionWith combineAction endpoints + & apiEndpoints %~ HM.unionWith (flip combineAction) endpoints -- | Generate the docs for a given API that implements 'HasDocs' with with any From 529139eb0395b1dff5d3beac17821f3d5451b7f5 Mon Sep 17 00:00:00 2001 From: Philipp Kant Date: Tue, 16 Jun 2015 22:38:14 +0200 Subject: [PATCH 2/2] Added test for docsWith. Make sure that no information is lost when providing additional information via docsWith. With the current left-biased implementation of combineAction, this can happen if the function arguments are in the wrong order. --- servant-docs/servant-docs.cabal | 1 + servant-docs/test/Servant/DocsSpec.hs | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/servant-docs/servant-docs.cabal b/servant-docs/servant-docs.cabal index b992b662..a18ee1d1 100644 --- a/servant-docs/servant-docs.cabal +++ b/servant-docs/servant-docs.cabal @@ -70,6 +70,7 @@ test-suite spec base , aeson , hspec + , lens , servant , servant-docs , string-conversions diff --git a/servant-docs/test/Servant/DocsSpec.hs b/servant-docs/test/Servant/DocsSpec.hs index 8e6cb201..9da66448 100644 --- a/servant-docs/test/Servant/DocsSpec.hs +++ b/servant-docs/test/Servant/DocsSpec.hs @@ -7,7 +7,9 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} module Servant.DocsSpec where +import Control.Lens import Data.Aeson +import Data.Monoid import Data.Proxy import Data.String.Conversions (cs) import GHC.Generics @@ -21,7 +23,27 @@ spec = describe "Servant.Docs" $ do describe "markdown" $ do let md = markdown (docs (Proxy :: Proxy TestApi1)) + tests md + describe "markdown with extra info" $ do + let + extra = extraInfo + (Proxy :: Proxy (Get '[JSON, PlainText] Int)) + (defAction & notes <>~ [DocNote "Get an Integer" ["get an integer in Json or plain text"]]) + <> + extraInfo + (Proxy :: Proxy (ReqBody '[JSON] String :> Post '[JSON] Datatype1)) + (defAction & notes <>~ [DocNote "Post data" ["Posts some Json data"]]) + md = markdown (docsWith [] extra (Proxy :: Proxy TestApi1)) + tests md + it "contains the extra info provided" $ do + md `shouldContain` "Get an Integer" + md `shouldContain` "Post data" + md `shouldContain` "get an integer in Json or plain text" + md `shouldContain` "Posts some Json data" + + where + tests md = do it "mentions supported content-types" $ do md `shouldContain` "application/json" md `shouldContain` "text/plain;charset=utf-8" @@ -34,10 +56,11 @@ spec = describe "Servant.Docs" $ do md `shouldContain` "POST" md `shouldContain` "GET" - it "contains response samples" $ do + it "contains response samples" $ md `shouldContain` "{\"dt1field1\":\"field 1\",\"dt1field2\":13}" - it "contains request body samples" $ do + it "contains request body samples" $ md `shouldContain` "17" + -- * APIs data Datatype1 = Datatype1 { dt1field1 :: String