servant-mock icon indicating copy to clipboard operation
servant-mock copied to clipboard

Example in Haddock docs for Servant.Mock no longer builds without error

Open phlummox opened this issue 4 years ago • 0 comments

The Haddock documentation for Servant.Mock gives example code similar to what's found in example/main.hs, but with the most recent version of the package (0.8.7), the code in the Haddock documentation no longer compiles.

The module-level haddock documentation suggests the following should compile:

--  we need imports and User definition and instance -- we'll use the one from example/main.hs

import           Data.Aeson
import           GHC.Generics
import           Network.Wai.Handler.Warp
import           Servant
import           Servant.Mock
import           Test.QuickCheck.Arbitrary

newtype User = User { username :: String }
  deriving (Eq, Show, Arbitrary, Generic)

instance ToJSON User

-- paste in the example from the Haddock documentation:

type API = "user" :> Get '[JSON] User

myAPI :: Proxy API
myAPI = Proxy

main :: IO ()
main = Network.Wai.Handler.Warp.run 8080 $
  serve myAPI (mock myAPI Proxy)

However, in the arguments to serve, it seems a reasonably recent GHC (8.6.5) can't deduce the type of the second Proxy (whereas it could for servant-mock 0.8.5).

I used the Docker image for GHC 8.6.5 (running docker -D run --rm -it haskell:8.6.5 bash), and pasted the above code into example/main.hs, and within the container ran:

$ stack unpack servant-mock-0.8.7
$ cd servant-mock-0.8.7
$ cabal v2-update
$ cabal v2-build --dependencies-only all
$ cabal v2-build all

I get the following compile error:

[1 of 1] Compiling Main             ( example/main.hs, /root/servant-mock-0.8.7/dist-newstyle/build/x86_64-linux/ghc-8.6.5/servant-mock-0.8.7/x/mock-app/build/mock-app/mock-app-tmp/Main.o )

example/main.hs:33:16: error:
    * Ambiguous type variable `context0' arising from a use of `mock'
      prevents the constraint `(HasContextEntry
                                  (context0 .++ DefaultErrorFormatters)
                                  ErrorFormatters)' from being solved.
      Probable fix: use a type annotation to specify what `context0' should be.
      These potential instances exist:
        two instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    * In the second argument of `serve', namely `(mock myAPI Proxy)'
      In the second argument of `($)', namely
        `serve myAPI (mock myAPI Proxy)'
      In the expression: run 8080 $ serve myAPI (mock myAPI Proxy)
   |
33 |   serve myAPI (mock myAPI Proxy)

I assume this is due to some change in servant-mock and the other servant packages, since the example seems to build with no issues using servant-mock 0.8.5 and the same version of GHC.

The example/main.hs code shows the fix - add a type declaration to the Proxy, thus: (Proxy :: Proxy '[])). But it seems worthwhile updating the Haddock documentation to reflect this.

(I'm not using contexts in my own project, so I don't know if that is an unduly restrictive signature and something looser could be used.)

phlummox avatar Sep 14 '21 08:09 phlummox