Search code examples
pactspring-cloud-contract

Spring Cloud Contract generates Pacts with empty body when the body is a list


I'm trying to generate pacts from spring cloud contracts as shown at the documentation. It works just find when the response body root is a json, however when I'm trying to generate a pact that return an array of jsons it generates an empty body. I´ve tried using groovy dsl with String format """[{}...]""" and using DslProperty [value()...]. Here are my contracts:

With string format

Contract.make {
description "should return a list of dummy object with dummy value. Generates pact with empty json"
request {
    method GET()
    url("/dummy")
}
response {
    body("""[{"value": "Hi! I'm a dummy object ;)"}]""")
    headers {
        contentType applicationJson()
    }
    status 200
}}

With DslProperty

Contract.make {
description "should return a list of dummy object with dummy value. Generates pact with empty body list"
request {
    method GET()
    url("/dummy")
}
response {
    body([value(value: "Hi! I'm a dummy object ;)")])
    headers {
        contentType applicationJson()
    }
    status 200
}}

And that's the file generate at target/pacts

{
"provider": {
    "name": "Provider"
},
"consumer": {
    "name": "Consumer"
},
"interactions": [
    {
        "description": "should return a list of dummy object with dummy value. Generates pact with empty body list",
        "request": {
            "method": "GET",
            "path": "/dummy"
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": [

            ],
            "matchingRules": {
                "header": {
                    "Content-Type": {
                        "matchers": [
                            {
                                "match": "regex",
                                "regex": "application/json.*"
                            }
                        ],
                        "combine": "AND"
                    }
                }
            }
        }
    },
    {
        "description": "should return a list of dummy object with dummy value. Generates pact with empty json",
        "request": {
            "method": "GET",
            "path": "/dummy"
        },
        "response": {
            "status": 200,
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {

            },
            "matchingRules": {
                "header": {
                    "Content-Type": {
                        "matchers": [
                            {
                                "match": "regex",
                                "regex": "application/json.*"
                            }
                        ],
                        "combine": "AND"
                    }
                }
            }
        }
    }
],
"metadata": {
    "pactSpecification": {
        "version": "3.0.0"
    },
    "pact-jvm": {
        "version": "3.5.23"
    }
}}

I'm using the following versions

    <spring-cloud.version>Hoxton.BUILD-SNAPSHOT</spring-cloud.version>
    <spring-cloud-contract.version>2.0.1.RELEASE</spring-cloud-contract.version>
    <pact-jvm-provider-maven.version>3.5.23</pact-jvm-provider-maven.version>

and that's my plugin configuration

<!-- SCC to pact see https://cloud.spring.io/spring-cloud-contract/reference/html/howto.html#how-to-generate-pact-from-scc-->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <id>convert-dsl-to-pact</id>
                    <phase>process-test-classes</phase>
                    <configuration>
                        <classpathScope>test</classpathScope>
                        <mainClass>
                            org.springframework.cloud.contract.verifier.util.ToFileContractsTransformer
                        </mainClass>
                        <arguments>
                            <argument>
                                org.springframework.cloud.contract.verifier.spec.pact.PactContractConverter
                            </argument>
                            <argument>${project.basedir}/target/pacts</argument>
                            <argument>
                                ${project.basedir}/src/test/resources/contracts
                            </argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

While debugging throgh the plugin I've seen that what is happening respectively is: - When declaring body as “”” [{...}] “”” Pact converter assumes that the body is an String instance so it goes through traverse method at org.springframework.cloud.contract.verifier.spec.pact.BodyConverter. And since it starts with [ it is not parsed.

org.springframework.cloud.contract.verifier.spec.pact.BodyConverter

private static DslPart traverse(Object value, DslPart parent, Closure dslPropertyValueExtractor) {
    ...
    if (v instanceof String) {
        v = v.trim()
        if (v.startsWith("{") && v.endsWith("}")) {
            try {
                v = jsonSlurper.parseText(v as String)
            }
            catch (JsonException ex) { /*it wasn't a JSON string after all...*/
            }
        }
    }
    ...

On the other hand, when going through the plugin code using the DslProperty I have an object like [DslProperty{clientValue=DslProperty}]. The first DslProperty is being extracted but since the content is another DslProperty and there’s no recursive extraction I end up with an empty body because v isn’t an instance of the Gstring, String, Number, Map, Collection. So I get an empty body again.

org.springframework.cloud.contract.verifier.spec.pact.BodyConverter

    private static void processCollection(Collection values, PactDslJsonArray jsonArray, Closure dslPropertyValueExtractor) {
    values.forEach({
        Object v = it
        if (v instanceof DslProperty) {
            v = dslPropertyValueExtractor(v)
        }
        if (v instanceof GString) {
            v = ContentUtils.extractValue(v, dslPropertyValueExtractor)
        }
        if (v == null) {
            jsonArray.nullValue()
        }
        else if (v instanceof String) {
            jsonArray.string(v)
        }
        else if (v instanceof Number) {
            jsonArray.number(v)
        }
        else if (v instanceof Map) {
            PactDslJsonBody current = jsonArray.object()
            traverse(v, current, dslPropertyValueExtractor)
            current.closeObject()
        }
        else if (v instanceof Collection) {
            PactDslJsonArray current = jsonArray.array()
            traverse(v, current, dslPropertyValueExtractor)
            current.closeArray()
        }
    })
}

I've published an example at https://github.com/brjt23/contract-to-pact/tree/master in case more info about how I did build the project is required.

Is there something I'm doing wrong on defining my groovy contract files? I guess I missunderstood something about how the response body should be defined.


Solution

  • You need to create an array of groovy objects in your body like this:

    body([ [value: "Object1"], [value: "Object2"] ])

    This way spring cloud contracts will generate the correct code needed for your contracts.