haskell-servant / haskell-servant/servant-mock

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

Open
#29 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
19
Forks
10
PR merge metrics
No merged PRs in 30d

Description

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.)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the module-level Haddock in src/Servant/Mock.hs and compare its example with the working version in example/main.hs. Update the documentation example to include the explicit Proxy type annotation, then build the example with the package dependencies to confirm it compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.