【问题标题】:Why is Wiremock Standalone rejecting my valid JSONPath expression?为什么 Wiremock Standalone 拒绝我的有效 JSONPath 表达式?
【发布时间】:2023-02-15 09:05:17
【问题描述】:

背景

我正在使用wiremock-jre8-standalone-2.35.0.jar

如果传入请求的数组包含任何值,我希望它返回 200 响应:

{
    "field1": "data1",
    "array": [
        {...},
        {...},
         ...
    ],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}

如果传入请求的数组为空,我希望它返回 400 响应:

{
    "field1": "data1",
    "array": [],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}

Wiremock 应将传入请求与来自以下代码的 "request": {...} 匹配:

{
  "id": "...",
  "request": {
    "urlPattern": "...",
    "method": "POST",
    "headers": {...},
      "bodyPatterns": [
        {
          "matchesJsonPath": "$[?(@.length < 1)]"
        }
      ]
    }
  },
  "response": {
    "status": 400,
    "bodyFileName": "...",
    "headers": {...}
  },
  "uuid": "..."
}

问题

Wiremock 在 bodyPatterns 数组中拒绝我的 JSONPath 表达式:

[{"matchesJsonPath":"$[?(@.length < 1)]"}] is not a valid match operation

然而,根据https://jsonpath.com/,该表达式似乎是有效的:

JSONPath
---
$[?(@.length < 1)]


Inputs
---
{
    "field1": "data1",
    "array": [],
    "field2": "data2",
    "field3": "data3",
    "field4": "data4",
    "field5": "data5"
}


Evaluation Results
---
[
  []
]

...是什么赋予了?

【问题讨论】:

    标签: arrays jsonpath wiremock-standalone


    【解决方案1】:

    这就是:

    JSON 路径

    如果属性值是有效的 JSON 并且与提供的 JSON 路径表达式匹配,则认为匹配。如果表达式返回非空单个值(字符串、整数等)或非空对象或数组.

    我的 JSON 路径表达式返回一个空数组,因此不能认为它匹配路径表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多