Search code examples
wiremock

Wiremock startup fails with error "is not a valid match operation"


I have added wiremock dependency in a spring boot app and started adding json configurations to match my incoming requests so that I can return mocked responses. However upon adding the follwoing request_matcher, the app fails to boot

{
  "priority": 1,
  "request": {
    "urlPath": "/someapi/searchTimeslot",
    "method": "GET",
    "queryParameters": {
      "latest": {
        "or": [
          {
            "absent": true
          },
          {
            "doesNotMatch": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
          }
        ]
      }
    }
  },
  "response": {
    "status": 422,
    "jsonBody": [{"code": 422, "detail": "Invalid Parameter Value", "field": "latest", "message": "An allowed parameter was assigned an invalid value"}],
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

what I want is the request to be matched when a query param named "latest" is either absent or does not match a timestamp pattern.

this is exactly as stated in the documentation

but the spring boot app fails to start with the following error

get-timeslot-422-invalid-latest-date.json:\n{\"or\":[{\"absent\":true},{\"doesNotMatch\":\"^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}Z$\"}]} is not a valid match operation please excuse the escaped sequences and quotation marks. Its just how log formatting is done in this app.

spring boot version : 2.7.10 wiremock dependency :

<dependency>
  <groupId>com.github.tomakehurst</groupId>
  <artifactId>wiremock-jre8</artifactId>
  <version>2.27.2</version>
</dependency>

Any help is greatly appreciated


Solution

  • So, it looks like this may be a WireMock bug with the logical OR operator. If you reverse the order of your objects in the OR array, the app should start up.

    ...
    "or": [
      {
        "doesNotMatch": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$"
      },
      {
        "absent": true
      }
    ]
    ...
    

    The above loaded for me (WireMock 2.35.0), but I was encountering the same error as you were when the absent: true object was first.