Search code examples
springspring-cloudspring-cloud-contract

Contract with request body having a nested array


I have this contract

org.springframework.cloud.contract.spec.Contract.make {
    request {
        method 'POST'
        url ('/v1/foo') {
            body([
                    fooList: [[
                        id: anyUuid()
                    ]]
            ])
        }
        headers {
            contentType applicationJson()
        }
    }
    response {
        status OK()
    }
}

when I test it with Postman like bellow it worked as expected

{
    "fooList": [
        { "id": "dd2b602a-5052-4203-9e8c-3f1dfb49b860" },
        { "id": "dd2b602a-5052-4203-9e8c-3f1dfb49b861" }
    ]
}

but when I try to make it fail and change just one id like bellow it works too !!

{
    "fooList": [
        { "id": 1 },
        { "id": "dd2b602a-5052-4203-9e8c-3f1dfb49b861" }
    ]
}

it failed just when all id are incorrect !!

{
    "fooList": [
        { "id": 1 },
        { "id": 1 }
    ]
}

like this

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
POST                                                       | POST
/v1/processOdr                                             | /v1/processOdr
                                                           |
Content-Type [matches] : application/json.*                | Content-Type: application/json
                                                           |
$.['fooList'][*][?(@.['id'] =~                             | {                                                   <<<<< Body does not match
/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]  |     "fooList": [
{12}/)]                                                    |         {
                                                           |             "id": 1
                                                           |         },
                                                           |         {
                                                           |             "id": 1
                                                           |         }
                                                           |     ]
                                                           | }
                                                           |
-----------------------------------------------------------------------------------------------------------------------

How to handle this scenario with an request body having a nested array ?


Solution

  • The produced jsonpath is such that it searches for any element that matches the regex. That means that there is at least one. If you need all of them to match you can use the array size check via the spring.cloud.contract.verifier.assert.size property. Below you can find an entry in the doc about this

    The support for verifying the size of JSON arrays is experimental. If you want to turn it on, set the value of the following system property to true: spring.cloud.contract.verifier.assert.size. By default, this feature is set to false. You can also set the assertJsonSize property in the plugin configuration.