Skip to content

messageBody mangles json-ish inputs #1627

@aviv-amdocs

Description

@aviv-amdocs

I'm trying to use Spring Cloud Contract to test Messaging (Spring Cloud Stream over Kafka).
The message I need to send is JSON encoded, and one of its fields is a JSON encoded string, like this:

    {
        "type": "x",
        "payload": "{\"a\": 4}"
    }

Using Spring Cloud Contract Maven Plugin, versions 2.2.6.RELEASE or 3.0.1.

This is the contract file - node body is provided as a String:

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

Contract.make{
	input {
		messageBody('   {"type": "x","payload": "{\\"a\\": 4}"}')
	}
}

and this is the generated test class (trimmed for focus):

public class Sunny_dayTest extends AsyncBaseClass {
	@Inject ContractVerifierMessaging contractVerifierMessaging;
	@Inject ContractVerifierObjectMapper contractVerifierObjectMapper;

	@Test
	public void validate_notifyChangeUpdate() throws Exception {
		// given:
			ContractVerifierMessage inputMessage = contractVerifierMessaging.create(
					"{\"type\":\"x\",\"payload\":{\"a\":4}}"
						, headers()
			);

		// when:;
		}
	}

The message in the Java file is altered from my input; Here it is printed and formatted:

{
  "type": "x",
  "payload": {
    "a": 4
  }
}

(Note that value of payload is now an object, not a string!)

It appears that the value of messageBody is being recursively parsed and reassembled.

Some other values are also modified:

messageBody(' [][]]') becomes "[]" (leading whitespace removed, as well as all content that can't be parsed as json).
And, hilariously, messageBody(file('message.json')) translates into ...create("new String(fileToBytes(this, \"notifyChangeUpdate_request_message.json\")), ...)"

Is there a way to provide an arbitrary string to messageBody and have it used as the message?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions