Skip to content

ContractVerifierObjectMapper fails for Avro-generated objects #2402

@etrandafir93

Description

@etrandafir93

Describe the bug
The ContractVerifierObjectMapper maps the actual intercepted message to JSON, before performing the relevant assertions on it.

However, if the message is an Avro-generated object, mapping it to JSON will fail due to some of the avro-specific fields in that record (schema, specificData, classSchema, conversion)

tools.jackson.databind.DatabindException: Not an array: {"type":"record","name":"FooAvro","namespace":"org.springframework.cloud.contract.verifier.messaging.internal","doc":"Dummy Avro object for testing purposes","fields":[{"name":"fooAvro","type":{"type":"string","avro.java.string":"String"},"doc":"foo field"}]}
 at [No location information] (through reference chain: org.springframework.cloud.contract.verifier.messaging.internal.FooAvro["schema"]->org.apache.avro.Schema$RecordSchema["elementType"])
	at tools.jackson.databind.DatabindException.wrapWithPath(DatabindException.java:132)
	at tools.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:275)
		[ ... ]
	at org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierObjectMapper.writeValueAsString(ContractVerifierObjectMapper.java:77)
	at org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierObjectMapperSpec.should convert an Avro-generated object into a json representation(ContractVerifierObjectMapperSpec.groovy:39)
Caused by: org.apache.avro.AvroRuntimeException: Not an array: {"type":"record","name":"FooAvro","namespace":"org.springframework.cloud.contract.verifier.messaging.internal","doc":"Dummy Avro object for testing purposes","fields":[{"name":"fooAvro","type":{"type":"string","avro.java.string":"String"},"doc":"foo field"}]}
	at org.apache.avro.Schema.getElementType(Schema.java:374)
		[ ... ]

Sample
I have reproduced the issue here.
(For simplicity, I have just copied and pasted an Avro-generated class)

In my project, I have configured the ContractVerifierObjectMapper's JsonMapper to ignore these problematic fields, and it solved the issue.

Would it be fair to assume that, if Avro is in the classpath, we should ignore these 4 fields while converting to JSON?
If so, we can encapsulate this bit of logic inside the ContractVerifierObjectMapper

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions