Spring-cloud-contract: Contracts: Using dynamic expressions in body -> conumser(...) produces wrong results

Created on 9 May 2018  路  7Comments  路  Source: spring-cloud/spring-cloud-contract

I have a contract with misplaced consumer/producer dynamic expressions in the body part. This is wrong however, according to the docs using $(consumer(regex(uuid())), producer(....)) should work correctly.

package contracts.person.msg

org.springframework.cloud.contract.spec.Contract.make {
    description 'should produce valid customer created event'
    label 'person.changed.event.create'
    input {
        triggeredBy('createNewPerson()')
    }
    outputMessage {
        sentTo 'personEventsTopic'
        headers {
            [
                    header('contentType': 'application/json'),
                    header('type': 'person'),
                    header('eventType': 'PersonChangedEvent'),
                    header('customerId': $(consumer('443682c6-e92d-477c-a847-6fb7e6a4c907'), producer(regex(uuid()))))
            ]
        }
        body([
                "type"      : 'CREATED',
                "personId"  : $(consumer(regex(uuid())), producer('0fd552ba-8043-42da-ab97-4fc77e1057c9')),
                "userId"    : $(consumer(optional(regex(uuid()))), producer('f043ccf1-0b72-423b-ad32-4ef123718897')),
                "firstName" : $(regex(nonEmpty())),
                "middleName": $(optional(regex(nonEmpty()))),
                "lastName"  : $(regex(nonEmpty())),
                "version"   : $(consumer(regex(number())), producer(0l))
        ])
    }
}

However the following JSON is produced for the Message

{

    "type":"CREATED",
    "personId":{
        "cursor":60
    },
    "userId":{
        "value":{
            "cursor":60
        }
    },
    "firstName":{
        "cursor":7
    },
    "middleName":{
        "value":{
            "cursor":7
        }
    },
    "lastName":{
        "cursor":7
    },
    "version":{
        "cursor":16
    }

}

I have tested this on 1.1.4 and 1.2.4, both produced similar output.

bug

Most helpful comment

@marekdominiak - I fixed it but remember that you have mixed consumer with producer.

org.springframework.cloud.contract.spec.Contract.make {
    description 'issue #650'
    label 'trigger'
    input {
        triggeredBy('createNewPerson()')
    }
    outputMessage {
        sentTo 'personEventsTopic'
        headers {
            [
                    header('contentType': 'application/json'),
                    header('type': 'person'),
                    header('eventType': 'PersonChangedEvent'),
                    header('customerId': $(producer(regex(uuid()))))
            ]
        }
        body([
                "type"      : 'CREATED',
                "personId"  : $(producer(regex(uuid())), consumer('0fd552ba-8043-42da-ab97-4fc77e1057c9')),
                "userId"    : $(producer(optional(regex(uuid()))), consumer('f043ccf1-0b72-423b-ad32-4ef123718897')),
                "firstName" : $(regex(nonEmpty())),
                "middleName": $(optional(regex(nonEmpty()))),
                "lastName"  : $(regex(nonEmpty())),
                "version"   : $(producer(regex(number())), consumer(0l))
        ])
    }
}

All 7 comments

@marekdominiak Can you provide us with a sample application?
I tested your contract with 1.2.4.RELEASE and was not able to reproduce this bug.

@marekdominiak - I fixed it but remember that you have mixed consumer with producer.

org.springframework.cloud.contract.spec.Contract.make {
    description 'issue #650'
    label 'trigger'
    input {
        triggeredBy('createNewPerson()')
    }
    outputMessage {
        sentTo 'personEventsTopic'
        headers {
            [
                    header('contentType': 'application/json'),
                    header('type': 'person'),
                    header('eventType': 'PersonChangedEvent'),
                    header('customerId': $(producer(regex(uuid()))))
            ]
        }
        body([
                "type"      : 'CREATED',
                "personId"  : $(producer(regex(uuid())), consumer('0fd552ba-8043-42da-ab97-4fc77e1057c9')),
                "userId"    : $(producer(optional(regex(uuid()))), consumer('f043ccf1-0b72-423b-ad32-4ef123718897')),
                "firstName" : $(regex(nonEmpty())),
                "middleName": $(optional(regex(nonEmpty()))),
                "lastName"  : $(regex(nonEmpty())),
                "version"   : $(producer(regex(number())), consumer(0l))
        ])
    }
}

@marcingrzejszczak Thanks, I fixed my contract before filling the bug :)

Awesome. Can you check out the snapshots and verify if things work fine now?

And thanks for fast fix (which we probably won't use after we fixed our contracts ;)

I will check that on Monday, at this moment AFTK of my work computer.

Quite late, but this is working now in 1.2.5.

Was this page helpful?
0 / 5 - 0 ratings