Pact JVM closeArray

问题描述 投票:1回答:1

我在pact-jvm-consumer中遇到了closeArray的问题。

鉴于Json喜欢这样,“DslPart imeiResults = new PactDslJsonBody()”将如何构建。

{ 
   "Car": {
     "Price": 123,     
     "Features": [
         "rain sensor",
         "cruise control"
     ],
     "Id": "6500"
   }
}

我试过这样的:

    DslPart etaResults = new PactDslJsonBody()
           .object("Car")
                .integerType("Price",123)
                .array("Features")
                    .stringValue("rain sensor")
                    .stringValue("cruise control")
                .closeArray()
                .stringValue("Id","6500")
            .closeObject()
            .asBody();

但这不起作用,例如.closeArray()不会返回PactDslJsonBody而是返回DslPart,所以在.closeArray()之后你永远不会有任何东西?我不明白,有人能以正确的方式展示如何做到这一点的代码吗?

pact pact-jvm
1个回答
0
投票

我猜你在stringValue工作后你的closeArray不起作用?

遗憾的是,当使用array函数创建一个数组时,它实际上是creates a new PactDslJsonArray and when closing it, there's no way for that class to know what the parent is, hence it just returns the common superclass of DslPart,这可能会引起一些混乱。需要做的是使用DslPart函数将PactDslJsonBody投回到asBody。所以,你的例子应该是这样的:

DslPart etaResults = new PactDslJsonBody()
   .object("Car")
        .integerType("Price",123)
        .array("Features")
            .stringValue("rain sensor")
            .stringValue("cruise control")
        .closeArray()
        .asBody()
        .stringValue("Id","6500")
    .closeObject();

现在,我们知道这是令人困惑的,hence why we started working on a new DSL using Java 8's Lambda functions试图让体验更好。希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.