Search code examples
pact

How to handle Polymorphic endpoints on Pact?


I have an app where I can search for Books and Movies. These 2 entities have different properties, so their JSON structures are different.

I also have a GET /favorites endpoint which should return both Books and Movies.

GET /favorites

{
  "favorites": [
    {
      "type": "book",
      "title": "Foo",
      "author": "John"
    },
    { 
      "type": "movie",
      "name": "Bar",
      "producers": [
        {
          "firstName": "Mary"
        }
      ]
    }
  ]
}

I searched for for docs on this case but I can't find anything. How can I write a Pact contract for this use case?


Solution

  • I would write two separate test cases for this, and use Provider States to differentiate the two payloads.

    For example:

    1. When there are books
    2. When there are movies

    Or something to that effect. See [1] for related background on this.

    [1] https://docs.pact.io/faq#why-is-there-no-support-for-specifying-optional-attributes