spring-cloud / spring-cloud-contract

Support for Consumer Driven Contracts in Spring
https://cloud.spring.io/spring-cloud-contract
Apache License 2.0
713 stars 435 forks source link

Unable to generate dynamic array in response #2112

Closed lality90 closed 1 week ago

lality90 commented 2 months ago

I have a scenario where if a request has 1 element in the array then return 1 object in response and similarly if the request has n elements respond with n objects, But I am unable to create such a response.

Contract package contracts

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    name("test-multiple-array")
    inProgress()
    request {
        method(POST())
        urlPath('/create')
        headers {
            header('Content-Type', 'application/json')
        }

        bodyMatchers {
            jsonPath('$.id', byEquality())
            jsonPath('$.address', byType {
                minOccurrence(1)
                maxOccurrence(5)
            })
        }

        body([
                id                  : '1',
                address      : [
                        '123 Office',
                        '234 Home'
                ]
        ])
    }
    response {
        status(OK())
        headers {
            contentType applicationJson()
        }

        body([results: [
                {
                    def results = []

                    fromRequest().body('address').each { add ->
                        def resultObject = [
                                processStatus: 'CREATED',
                                address: "${add}"
                        ]
                        results.add(resultObject)
                    }
                    return results
                }()
        ]])
        bodyMatchers {
            jsonPath('$.results', byType())
        }
    }
}

//Expected Response

{
    "results": [
        {
            "processStatus": "CREATED",
            "address": "123 Office"
        },
        {
            "processStatus": "CREATED",
            "address": "234 Home"
        }
    ]
}

//Actual Response

{
    "results": [
        {
            "processStatus": "CREATED",
            "address": [
                "123 Office",
                "234 Home"
            ]
        }
    ]
}
marcingrzejszczak commented 3 weeks ago

                    fromRequest().body('address').each { add ->
                        def resultObject = [
                                processStatus: 'CREATED',
                                address: "${add}"
                        ]
                        results.add(resultObject)
                    }
                    return results
                }()

That was never intended to work like this. fromRequest() will be transformed to WireMock's json path reference. I didn't even know you can do some each(...) etc. :P

spring-cloud-issues commented 2 weeks ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues commented 1 week ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.