Search code examples
javajsonjsonpathwiremock

Matching JSON request body based on JSON Path with logical operators in WireMock


In WireMock I am trying to match the request based on the JSON body with path matchers as shown below. But it seems that the logical operators are not supported inside matchesJsonPath expression.

Is this understanding correct? What could be a possible alternative solution?

...
"request": {
        "url": "/mock/score",
        "method": "PUT",
        "bodyPatterns": [
          {
            "matchesJsonPath": "$[?(@.code == '123')]"
          },
          {
            "matchesJsonPath": "$[?(@.amnt < '200000')]"
          }
        ]
      }
...

Solution

  • Your assumption is incorrect. You can use operators inside of the JSONPath filters. You might be running into issues because the value being checked on amnt is a string. The following should work.

    ...
    {
      "matchesJsonPath": "$[?(@.amnt < 20000)]"
    }
    ...
    

    This of course, requires that the value being passed in the body is a number type and not a string itself.

    { 
      "amnt": 20000 // correct format
      "amnt": "20000" // incorrect format
    }
    

    If your amnt value is a string, you may have to write a custom matcher that can parse the body and convert the string value into a number (or the java data type equivalent)