Search code examples
wiremockwiremock-standalone

In wiremock mapping json BodyPatterns only the last comparison is happening


I am using wiremock-jre8-standalone-2.27.0 jar to mock an API. My mapping json looks like:

 {
  "request": {
    "url": "/sampleUrl",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath" : {
          "expression": "$[0].fruit",
          "contains": "apple"
        },
        "matchesJsonPath" : {
          "expression": "$[0].quantity",
          "contains": "1221"
        },
        "matchesJsonPath" : {
          "expression": "$[1].fruit",
          "contains": "banana"
        },
        "matchesJsonPath" : {
          "expression": "$[2].quantity",
          "contains": "2784"
        }
      }
    ]
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "bodyFileName": "prices.json",
    "delayDistribution": {
      "type": "uniform",
      "lower": 200000,
      "upper": 500000
    }

As it could be seen there are 4 matchesJsonPath inside bodyPatterns but only the last matchesJsonPath ($[2].quantity == 2784) is compared every time. Is I change the remaining things in the request body such as it fails the first three matchesJsonPath and send the request through Postman I still get the response. Is there a way to make Wiremock check all the conditions?


Solution

  • The issue is with your bodyPatterns array. Each match needs to be its own JSON object within the array. You currently have the matchers all within one object.

    "bodyPatterns": [
        {
            "matchesJsonPath" : {
              "expression": "$[0].fruit",
              "contains": "apple"
            }
        },
        {
            "matchesJsonPath" : {
              "expression": "$[0].quantity",
              "contains": "1221"
            }    
        },
        {
            "matchesJsonPath" : {
              "expression": "$[1].fruit",
              "contains": "banana"
            }    
        },
        {
            "matchesJsonPath" : {
              "expression": "$[2].quantity",
              "contains": "2784"
            }    
        }
    ]