Search code examples
arraysjsonpathwiremock-standalone

Why is Wiremock Standalone rejecting my valid JSONPath expression?


Background

I am using wiremock-jre8-standalone-2.35.0.jar

I want it to return a 200 response if the incoming request's array contains any values:

{
    "field1": "data1",
    "array": [
        {...},
        {...},
         ...
    ],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}

I want it to return a 400 response if the incoming requests' array is empty:

{
    "field1": "data1",
    "array": [],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}

Wiremock should match the incoming request against the "request": {...} from the below code:

{
  "id": "...",
  "request": {
    "urlPattern": "...",
    "method": "POST",
    "headers": {...},
      "bodyPatterns": [
        {
          "matchesJsonPath": "$[?(@.length < 1)]"
        }
      ]
    }
  },
  "response": {
    "status": 400,
    "bodyFileName": "...",
    "headers": {...}
  },
  "uuid": "..."
}

Problem

Wiremock is rejecting my JSONPath expression in the bodyPatterns array:

[{"matchesJsonPath":"$[?(@.length < 1)]"}] is not a valid match operation

Yet it seems that the expression is valid according to https://jsonpath.com/ :

JSONPath
---
$[?(@.length < 1)]


Inputs
---
{
    "field1": "data1",
    "array": [],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}


Evaluation Results
---
[
  []
]

...What gives?


Solution

  • This is what gives:

    JSON Path

    Deems a match if the attribute value is valid JSON and matches the JSON Path expression supplied. A JSON body will be considered to match a path expression if the expression returns either a non-null single value (string, integer etc.), or a non-empty object or array.

    My JSON Path expression returns an empty array, so it cannot be considered to match the path expression.