2

我需要为一个看起来像这样的请求编写一个存根:

[
    { "todo_id": 1 },
    { "todo_id": 2 }
]

请求中的 todo 对象的数量可能会有所不同。

我的回复目前如下所示:

"response": {
    "status": 200,
    "body": "[ {{#each (jsonPath request.body '$') as |todo|}}
                   { \"todo_id\": {{todo.todo_id}} },
               {{/each}}
             ]"
}

请注意,我将正文隔开以使其更具可读性,在实际存根中,它都在一行上。

所以我的问题是我的 todo 对象后面需要逗号,以防请求中传递了多个对象。然而,这也给最后一个对象留下了逗号,所以如果发送了上述请求,这将是响应:

[
    { "todo_id": 1 },
    { "todo_id": 2 },
]

最后一个逗号使.json()Python 应用程序中的方法失败,该应用程序需要从该 WireMock 存根中读取响应。

关于如何摆脱最后一个逗号的任何想法?我在想可能有一个eq逗号周围的条件并检查当前todo变量是否相同,{{jsonPath request.body '$.[-1]'}}但这样写:

{{#eq todo {{jsonPath request.body '$.[-1]'}} }}

也没有工作。

任何有关如何摆脱最后一个逗号的建议将不胜感激。谢谢 :)

4

1 回答 1

2

在 Google Groups 上得到了答案。

可以@last在每个循环中使用来检测您何时在最后一项上,例如:

{{#each (jsonPath request.body '$.things') as |thing|}}
  {{#if @last}}
    { "thing": {{{thing}}} }
  {{else}}
    { "thing": {{{thing}}} },
  {{/if}}
{{/each}}
于 2020-08-08T15:47:46.490 回答