About
The package is the client library for the mock server. The source code is generated by the openapi3-code-generator, specifically its patched version.
Usage
module Main
import MockServer as Mock
import Network.HTTP.Client
conf :: Mock.Configuration
conf = Configuration
{ configBaseURL = "http://localhost"
, configSecurityScheme = id
, configIncludeUserAgent = False
, configApplicationName = "ping-app" }
expectation :: Mock.Expectations
expectation = Mock.ExpectationsExpectation $ Mock.mkExpectation
{ expectationHttpRequest = Just request
, expectationHttpResponse = Just response }
where
request = Mock.mkHttpRequest
{ httpRequestMethod = Just $ StringOrJsonSchemaText "POST"
, httpRequestPath = Just $ StringOrJsonSchemaText "/ping" }
response = Mock.mkHttpResponse
{ httpResponseBody = Just $ BodyWithContentTypeRawStringResponseBody "pong"
, httpResponseStatusCode = Just 200 }
-- | Puts the expectation which will response with "pong" body to the "POST /ping" request
main :: IO ()
main = do
void $ Mock.runWithConfiguration conf $ Mock.put_mockserver_expectation expectation
Maintainance
Files
The codegen dir
regen.shis the simple script to regenerate the whole package.shell.nixa shell in which theregen.shcalls theopenapi3-code-generatorto regenerate the codemock-server-openapi.yamlnot the original specification of themock-serveras you might think. The thing is that theopenapi3-code-generatordoes not support all the features of the OpenAPI3 standard correctly, so it is hand-postprocessed. It was originally downloaded from here and fixed.
Other
srcandmock-server-client.cabalcontain the generted code, but not exactly. Some of the parts of the generted code was not very pleasant, so it was fixed too.
What will happen if you just call the regen.sh?
It will build the openapi3-code-generator in nix and regenerate the whole
Haskell source code. You will see that the code was changed, because as
mentioned earlier the original code was slightly modified, so watch your commits.
How to update the package to the new version of the mock-server spec?
- Commit or discard all changes
git checkout update-openapi-spec- Replace the
codegen/mock-server-openapi.yamlwith a newer version git commit -m "updated mock-server-openapi.yaml to version $NEWVERSION"git checkout -b "update-$NEWVERSION" mastergit merge update-openapi-spec- Resolve conflicts if any
./codegen/regen.shgit symbolic-ref HEAD refs/heads/update-generated-code && git resetswitching to the branchupdate-generated-codewithout checking out the working-treegit commit -m "update the generted code for version $NEWVERSION"git checkout "update-$NEWVERSION"git merge update-generated-code- Resolve conflicts if any
git push origin "update-$NEWVERSION"- Create PR, wait for CI, review, merge into the master
The algorithm is kind of weird, but for a reason.
The branch update-openapi-spec is the branch tracking only original
codegen/mock-server-openapi.yaml changes, meaning changes made by authors of the
mock-server. So, when you merge changes of this branch into the master you literally merge the
changes made by authors of the mock-server with our manual changes made to please the
openapi3-code-generator.
The bratch update-generated-code is for tracking "changes" made by the code generator when it is
feed by the new OpenAPI spec file. So, when you merge this branch into the master you literally
merge the "code regeneration patch" with changes made by hand.
The master branch is for manual changes only. The changes made to keep things working and the
package usable. You can always see the manual changes just by git diff update-generated-code master or git diff update-openapi-spec master