最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jan 17 at 19:40 Alexey GavrilikAlexey Gavrilik 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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[?([email protected])]

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[?([email protected])]"
          }
        ]
      },
      "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
发布评论

评论列表(0)

  1. 暂无评论