Can I write Pact contract for an API response of the type, a list of objects which may have either an empty list or a populated list

Viewed 17

This seems to be a common issue with Pact and I cannot find a solution and it seems unresolvable, but I just thought I'd check on StackOverflow.

My API response returns an array of objects each of which has a children attribute (a list of child objects in my case veryone the same shape) .

{
   "type": "parent",
   children: [] // or [{"name": "Buzz"}..... ]
}

If the parent object doesn't have any children the list is empty else it has child objects. So the response contains both types of object.

Pact matcher EachLike has a minimum of 1, meaning, (as I understand it), the list of objects must all have either an attribute for a empty list or list of length > 0. So I cannot use that matcher to resolve this.

I cannot write two contracts for this as the API response contains parent objects with and without children. Both contracts would be wrong.

Can I accomodate either parent object has empty children list or populated children list ?

1 Answers

It seems you cannot do this with Pact contract testing . The Pact.io team consider this to be an example of an optional feild which isn't supported here is a link to the explanation if you're interested https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes. But Pact do have Bidirectional testing as one of the Pact team explained "you still utilise your existing pact contracts generated on the consumer side, and for the provider you upload an oas spec, plus verification results to show that the provider implements the specification" Here is a link to the relevant page https://docs.pactflow.io/docs/bi-directional-contract-testing/

Related