Warm tip: This article is reproduced from serverfault.com, please click

jsonpath-Wiremock 匹配 JsonPath 检查忽略顺序的数组值

(jsonpath - Wiremock matchesJsonPath checking array values ignoring the order)

发布于 2021-01-27 12:49:17

我正在尝试改进代码以防止错误。我有几个请求可以以这些形式出现,1:

 {
   "idUser": "1234",,
   "ids": ["3", "1"]
}

或 2:

{
  "idUser": "1234",,
  "ids": ["1", "3"]
}

我有这个 Json 匹配:

{
  "request": {
    "urlPath": "mypath/rest/",
    "bodyPatterns": [
      {
        "matchesJsonPath": {
          "expression": "$..ids[0]",
          "contains": "1"
        }
      },
      {
        "matchesJsonPath": {
          "expression": "$..ids[1]",
          "contains": "3"
        }
      }
    ]
  },
  "response": {
  }
}

如何在忽略 id 顺序的正则表达式中创建“包含”。我试过了 :

 "matchesJsonPath": "$[?(@..ids =~ /[1-3]+/]"
    
    {
      "matchesJsonPath": {
         "expression": "$..ids",
         "contains": "1"
       }
    },
    {
       "matchesJsonPath": {
         "expression": "$..ids",
         "contains": "3"
       }
    }

** 在情况 1 中它不起作用,在情况 2 中它会起作用。

非常感谢!

Questioner
abeld89
Viewed
0
agoff 2021-01-27 23:09:40

我认为问题出在你的表达上,但我不是 jsonPath 专家。我认为你不需要使用该expression/includes符号。这样的事情应该工作:

[{
    "matchesJsonPath": "$.ids[?(@ == '1')]"
},
{
    "matchesJsonPath": "$.ids[?(@ == '3')]"
}]

我对此进行了测试,如果你有"ids": ["1", "3"]"ids": ["3", "1"],它将返回匹配项。如果你只有"ids": ["1"]"ids": ["3"],则不会返回匹配项。