Search code examples
jsonwiremock

How to check if all elements of JSON array have property in request body with Wiremock?


I have a following example JSON body, which will be sent to Wiremock.

{
  "array1": [
    {
      "prop1": "value",
    },
    {
      "prop1": "value",
    },
    {
      "prop1": "value",
    }
  ]
}

I need a matcher which will match if at leas one of prop1 properties will be missing from json. prop1 can be of any value, even null.

Tried this one "expression" : "$.array1[?(@.prop1)]", "absent: true" and it works only if array is missing all prop1 properties.


Solution

  • You should be able to use the matchesJsonPath matcher to acheive this. I think you were nearly there with your jsonPath expression. I tried this one and it seemed to work only where a prop1 element was missing (while allowing prop1 to be null):

    $.array1[?(!@.prop1)]
    

    This is how I put it together in practice. I created one stub with the matchesJsonPath matcher and then a second stub with a lower priority to catch the requests where all the prop1 elements were present:

    {
      "mappings": [
        {
          "request": {
            "url": "/any-missing",
            "bodyPatterns": [
              {
                "matchesJsonPath" : "$.array1[?(!@.prop1)]"
              }
            ]
          },
          "response": {
            "body": "At least 1 prop1 IS missing"
          }
        },
        {
          "priority": 10,
          "request": {
            "url": "/any-missing"
          },
          "response": {
            "body": "All prop1 fields are present"
          }
        }
      ]
    }
    

    I then tested it with a few different requests:

    curl -X GET --location "http://localhost:8080/any-missing" \
        -H "Content-Type: application/json" \
        -d '{
              "array1": [
                {
                  "prop1": "value"
                },
                {
                  "prop1": "value"
                },
                {
                  "prop1": "value"
                }
              ]
            }'
    
    All prop1 fields are present
    
    curl -X GET --location "http://localhost:8080/any-missing" \
        -H "Content-Type: application/json" \
        -d '{
              "array1": [
                {
                  "prop1": "value"
                },
                {
                  "prop1": null
                },
                {
                  "prop1": "value"
                }
              ]
            }'
    
    All prop1 fields are present
    
    curl -X GET --location "http://localhost:8080/any-missing" \
        -H "Content-Type: application/json" \
        -d '{
              "array1": [
                {
                  "foo": "value"
                },
                {
                  "prop1": null
                },
                {
                  "prop1": "value"
                }
              ]
            }'
    
    At least 1 prop1 IS missing