Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to generate dynamic array in response #2112

Closed
lality90 opened this issue May 14, 2024 · 3 comments
Closed

Unable to generate dynamic array in response #2112

lality90 opened this issue May 14, 2024 · 3 comments

Comments

@lality90
Copy link

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
Copy link
Contributor

marcingrzejszczak commented Jun 27, 2024


                    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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants