带有 hamcrest hasSize() 的 JSONPath

JSONPath with hamcrest hasSize()

当使用 @assertThat(hasSize(x))@ 形式的 hasSize 匹配器和一个 returns 数组的 jsonpath 表达式时,将不会考虑重复值。示例:

{
        "a":[
        {
        "value1":"ab",
        "value2": 2
        },
        {
        "value1":"ab",
        "value2":2
        }
        ]
}

如果我执行 payload().validate("$..value1", "@assertThat(hasSize(2))@") 它将导致错误,因为 hasSize 期望大小为 1 的集合而不是2. 这是匹配器期望的行为吗?解决方法是使用表达式“$.a[?(@.value1)]”。

总结

这是 Citrus 中的错误。正如我的评论中提到的,我已经找到了这个错误,请参阅 GitHub issue

我也修复了bug,开了一个GitHub Pull Request。但是,主要的 Citrus 维护者可能需要几周的时间才能审核我的更改,因为他目前正在从事不同的项目。

我确信修复将包含在下一个 Citrus 维护版本中 2.7.3

备注

仅当某些值是重复条目时才会出现此错误,因为 code fragment 使用 Set,根据定义不能包含重复条目。

示例(从 GitHub 期复制)

{
  "test_array": [
    {
      "key_with_identical_values": "identical_value",
      "key_with_unique_values": "unique_value"
    },
    {
      "key_with_identical_values": "identical_value",
      "key_with_unique_values": "different_unique_value"
    }
  ]
}

鉴于 Citrus 验证如下:

<receive endpoint="testServer">
  <message type="json">
    <validate>
      <json-path expression="$..key_with_unique_values" value="@assertThat(hasSize(2))@"/>
      <json-path expression="$..key_with_identical_values" value="@assertThat(hasSize(2))@"/>
    </validate>
  </message>
</receive>

即使两个表达式的 JSONPath 数组大小为 2["unique_value", "different_unique_value"]["identical_value", "identical_value"]),表达式 $..key_with_identical_values 的验证也会失败并显示消息:

Expected: a collection with size <2>
     but: collection size was <1>