Search code examples
wiremock

Combining multiple bodyPattern with logical 'OR' in wiremock


I had a question if its possible to combine multiple body patterns with logical 'OR' operation in wiremock Currently bodyPatterns is an array that support multiple patterns but they are combined with logical 'AND' by default

for example,

{
  "request": {
    "urlPath": "/somePath",
    "method": "POST",
    "bodyPatterns": [
      {
        "matchesJsonPath": "[?(!(@.product.group))]",
        "matchesJsonPath": "[?(!(@.appointment.id))]"
      }
    ]
  },
  
  "response": {
    "status": 400,
    "jsonBody": [
      {
        "code":400,
        "detail":"Missing Required Parameter",
        "field":"product.group, title, id, contact.name, contact.number",
        "message":"The request is missing a required parameter."
      }
    ],
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

this will match if both product.group and appointment.id is missing from the request. what configuration change should I do to match this request id either product.group or appointment.id is missing I just dont want to have a separate config for each validation parameter


Solution

  • WireMock has an explicit Logical OR operator. Simplying adding that "or" field between bodyPatterns and the matchesJsonPath objects should suffice. Logical AND and OR were added in WireMock 2.28.0.

    {
      "request": {
        "urlPath": "/somePath",
        "method": "POST",
        "bodyPatterns": [
          {
            "or": [
              {
                "matchesJsonPath": "[?(!(@.product.group))]"
              },
              {
                "matchesJsonPath": "[?(!(@.appointment.id))]"
              }
            ]
          }
        ]
      },
      "response": {
        "status": 400,
        "jsonBody": [
          {
            "code": 400,
            "detail": "Missing Required Parameter",
            "field": "product.group, title, id, contact.name, contact.number",
            "message": "The request is missing a required parameter."
          }
        ],
        "headers": {
          "Content-Type": "application/json"
        }
      }
    }