如何指定根级别数组,使其具有最小尺寸,并且每个对象匹配模式。

huangapple go评论74阅读模式
英文:

How to specify root level array, of minimal size and each object matching pattern

问题

我在 Pact 中创建 DSL 时遇到问题,该 DSL 将接受最小大小的数组,其中每个元素都与给定的对象模式相匹配。当 JSON 对象内部有数组时,我没有问题,然后我可以简单地调用:

DslPart body = new PactDslJsonBody()
        .minArrayLike("users")
            .id()
            .stringType("name")
        .closeObject()
        .closeArray();

但是如果数组是根对象怎么办?我尝试了以下方式:

new PactDslJsonArray()
        .minArrayLike("users")
            .id()
            .stringType("name")
        .closeObject()
        .closeArray();

但是模式与请求不符(它似乎期望数组内有一个数组,因为它抛出反序列化异常,说 '[' 字符是意外的)。

英文:

I have problems creating a DSL in pact, which will accept array of minimal size where each element matches given object pattern. I have no problems when I have an array inside a JSON object, then I can simply call:

DslPart body = new PactDslJsonBody()
        .minArrayLike("users")
            .id()
            .stringType("name")
        .closeObject()
        .closeArray();

But how can I do it in case Array is the root object? I tried following:

new PactDslJsonArray()
        .minArrayLike("users")
            .id()
            .stringType("name")
        .closeObject()
        .closeArray();

But the pattern does not comply with request (it looks like it expects an array inside an array, since it throws deserialization exception saying, that '[' character was unexpected).

答案1

得分: 1

我在阅读文档时一定是瞎了眼,因为文档明确说明可以使用 PactDslJsonArray 的静态接口来完成:

PactDslJsonArray.minArrayLike()
    .date("clearedDate", "mm/dd/yyyy", date)
    .stringType("status", "STATUS")
    .decimalType("amount", 100.0)
.closeObject()
英文:

I must have been blind while reading documentation, which clearly states that one can do it using static interface of PactDslJsonArray:

PactDslJsonArray.minArrayLike()
    .date("clearedDate", "mm/dd/yyyy", date)
    .stringType("status", "STATUS")
    .decimalType("amount", 100.0)
.closeObject()

huangapple
  • 本文由 发表于 2020年10月8日 19:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64261721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定