This is a general question about matching, but app...
# pact-jvm
e
This is a general question about matching, but applies to Pact-JVM. How do I specify/match an empty JSON array. We have a response that looks like:
Copy code
{
  "destinations": [],
  "cities": [],
  "airports": [
    {
      "code": "MSY",
      "name": "Louis Armstrong New Orleans International Airport"
    }
  ]
}
I want to match that "destinations" and "cities" are indeed empty lists. I am trying to use
maxArrayLike("destinations", 0, example)
but I get an error on the Consumer test complaining that I either cannot leave that field empty, or that the example of 1 doesn't match the expected size of 0. Slack Conversation
r
There is an empty matcher. Need to look up how that works.
I think it requires V4 Pacts, though.
e
Cool thanks! I did find that I was missing the "numberOfExamples" parameter in my builder, which solved the issue codewise.
r
Oh, it's a not empty matcher.
You can just provide an empty array for the attribute, but maxArrayLike will also work.
e
Thanks for the response, I appreciate it! Oh, I wonder if this is a problem with the Java library then. Of course what I want (that you are pointing out) is
Copy code
cities: [ ]
destinations: [ ]
in the pact file, which is completely reasonable. But I'm not clear how to specify that in the Java DSL code (your statement: "just provide an empty array for the attribute"). Using
maxArrayLike
does result in that output, but I would love a way that makes more sense. Bing tells me that it is straightforward to match an array that has 0 or more values, but I specifically want 0.
r
It is a Java problem, with JS you just give an empty array. You could just try using an empty
PactDslJsonArray
(it should work in theory), but you have a working example.
Something like
Copy code
body.array("cities").closeArray()
e
Oh, that is much cleaner. I was under the impression that
array("cities")
would match an array of any size. That is much cleaner than the
maxArrayLike
syntax. Thanks!!
r
If you are up for a PR,
Copy code
body.emptyArray("cities")
would be even better