WireMock.Net: Loading mapping models with `JsonMatcher` is not working correctly

Describe the bug

Loading mapping models with JsonMatcher for Body matcher from a file is not working correctly.

Steps to reproduce:

  1. Create mapping with JsonMatcher for the body and save it to file as follow
var requestBuilder = Request.Create()
	.WithPath("/api/v1.0/accounts/")
	.WithHeader("Content-Type", "application/json; charset=utf-8")
	.WithBody(new JsonMatcher(new { AccountIds = new[] { 1, 2, 3} }))
	.UsingPost();

var respondWithAProvider = wiremockServer.Given(requestBuilder, saveToFile: true);

var responseProvider = Response.Create()
	.WithStatusCode(200)
	.WithHeader("Content-Type", "application/json; charset=utf-8")
	.WithBody(JsonConvert.SerializeObject(new[] { 1, 2, 3 }));

wiremockServer.RespondWith(responseProvider);
  1. As a result I get a file with the following content
{
  "Guid": "7d1c7dcc-eba1-4c3a-bc82-0a444fe9fcd9",
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/api/v1.0/accounts/",
          "IgnoreCase": false
        }
      ]
    },
    "Methods": [
      "POST"
    ],
    "Headers": [
      {
        "Name": "Content-Type",
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "application/json; charset=utf-8",
            "IgnoreCase": true
          }
        ]
      }
    ],
    "Body": {
      "Matcher": {
        "Name": "JsonMatcher",
        "Pattern": {
          "AccountIds": [1, 2, 3]
        },
        "IgnoreCase": false
      }
    }
  },
  "Response": {
    "StatusCode": 200,
    "BodyDestination": "SameAsSource",
    "Body": "[1,2,3]",
    "Headers": {
      "Content-Type": "application/json; charset=utf-8"
    }
  }
}
  1. Load mapping from file and feed it to the WireMock server
var content = File.ReadAllText(@"__admin\mappings\7d1c7dcc-eba1-4c3a-bc82-0a444fe9fcd9.json");
wiremockServer.WithMapping(content);

Expected behavior:

This is how requestBuilder looks like before saving mapping to a file image

This is how requestBuilder looks like after loading from a file

image

The expectation is that the JsonMatcher works in the same way when loaded from a file and when created from the code. Currently, the pattern is always set to null when loading from a file.

Other notes

This is probably caused by this line where every pattern is cast to string (non-string values will result with a null) https://github.com/WireMock-Net/WireMock.Net/blob/7abf56eafaf9d57aff1234bf7c0ad0f408bb18d6/src/WireMock.Net/Serialization/MatcherMapper.cs#L39

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19

Most upvoted comments

Hello @RiccoFerraro , I’ll release a new version with this fix tonight and for now close this issue.

In case you have another problem please create a new issue.

@RiccoFerraro Thanks about your remark about the Http VERB. I did create a new Issue for this, and I’ll take a look how to fix this.

@StefH Sorry I haven’t had a chance yet. I will try to take a look at this sometime this week. I’ll post here when I’ve tried it.

Hi @StefH Will do. I’ll get back to you on if the issue is in that alpha build

Hello @RiccoFerraro,

The 1.3.0 version does not have any preview fixed code for this issue, in order to test that you need MyGet version WireMock.Net.1.2.18-ci-13754.nupkg. Or if you want to debug, you can use the PR : https://github.com/WireMock-Net/WireMock.Net/pull/505

And can you create a full working example please?

Ok, I will try to prepare a test or demo app that reproduces that problem.