如何在 pact-jvm 中用异构数组表示响应

How to represent responses with heterogenous arrays in pact-jvm

我无法弄清楚如何在契约合同中用数组中结构不同的对象来表示数组。

来自pact-spec-v3

It would also be required to define whether the matchers should be combined with logical AND (all matchers must match) or OR (at least one matcher must match). AND should be the default, but there are cases where an OR makes sense

这可以用于 'OR' 两种不同类型的对象吗?

这是我试图通过 pact-jvm DSL 建模的响应,suggestions 数组包含两个不同类型的对象,“1”和“3”,具有不同的模式 -

{
  "suggestions": [
    {
      "display_name": "Potato",
      "type": 1,
      "keyword": "Potato",
      "category_l1": {
        "icon_image_url": "XXXXX",
        "id": 1489,
        "name": "Potato"
      }
    },
    {
      "type": 3,
      "suggestion": {
        "display_name": "New Potato (Aloo)",
        "name": "New Potato"
      }
    }
  ]
}

我目前拥有的代码:

private DslPart getBody() {
    return new PactDslJsonBody()
            .eachLike("suggestions", 1)
                    .stringType("display_name")
                    .integerType("type")
                    .stringType("keyword")
                    .object("category_l1")
                        .stringType("icon_image_url")
                        .stringType("name")
                        .integerType("id")
                    .closeObject()
                    .closeObject()
            .closeArray();
}

目前使用 Pact 建模并不容易,它可以假设每个项目都与提供的示例相似。有关讨论,请参阅 https://github.com/pact-foundation/pact-specification/issues/38