Search code examples
pactpact-jvm

Matching only types for a list of items


I created this project to show my problem a bit more interactive.
I have an endpoint that returns a object containing a list of objects. I do not want to compare the actual values of my pact, I just want to match the type.

My Pact looks like this:

  @Pact(consumer = "Consumer", provider = "Provider")
  fun multi(builder: PactBuilder): V4Pact {
    return builder
        .expectsToReceiveHttpInteraction("multi") { httpBuilder ->
          httpBuilder
              .withRequest { httpRequestBuilder ->
                httpRequestBuilder
                    .path("/multi")
                    .method("GET")
              }
              .willRespondWith { httpResponseBuilder ->
                httpResponseBuilder
                    .status(200)
                    .body(PactDslJsonBody()
                        .`object`("myData", PactDslJsonBody()
                            .eachLike("myData", 2)
                            .stringType("foo", "foo")
                            .`object`("bar", PactDslJsonBody()
                                .stringType("bar", "bar")
                            )
                        )
                    )
              }
        }
        .toPact()

Unfortunately it actually matches not only the size of the example list, but also the value of the fields.
Both of which is not wanted.
As the single return works fine I assume it has something to do with the .eachLike("myData", 2).
Any idea what I am doing wrong and how I can change it to get my desired outcome?

I already tried to ask in the Pact Slack workspace, but unfortunately nobody could help me there :(


Solution

  • I still do not know what the problem with the current implementation is. But I can easier manage it when switching to the LambdaDsl:

    .body(
      newJsonBody { o: LambdaDslJsonBody ->
        o.eachLike("myData", 2
            ) { d: LambdaDslObject ->
              d
                  .stringType("foo", "foo")
                  .`object`("bar") { bar: LambdaDslObject ->
                    bar
                        .stringType("bar", "bar")
                  }
            }
      }.build()
    )