Skip to content

StubRunnerExecutor fails for Avro objects #2404

@etrandafir93

Description

@etrandafir93

Describe the bug
StubRunnerExecutor::sendMessage converts the message to a JSON string before delegating to the MessageVerifierSender that should publish it:

void sendMessage(Contract groovyDsl) {
    // ...	

    Object payload = null;
    if (body != null && body.getClientValue() instanceof FromFileProperty) {
        FromFileProperty fromFile = (FromFileProperty) body.getClientValue();
        if (fromFile.isByte()) {
            payload = fromFile.asBytes();
        }
        else {
            payload = fromFile.asString();
        }
    }
    else {
        payload = JsonOutput
            .toJson(BodyExtractor.extractClientValueFromBody(body == null ? null : body.getClientValue()));
            // here! Even if "body" is an Avro object, it will be converted to json
    }

    this.messageVerifierSender.send(payload, headers == null ? null : headers.asStubSideMap(),
        outputMessage.getSentTo().getClientValue(), contract);
}

The MessageVerifierSender uses generic types, and it doesn't mention that the message comes as a JSON String.

When implementing the MessageVerifierSender::send, we'll most likely use an API for externalizing the message (eg: KafkaTemplate for Kafka). In this case, KafkaTemplate already encapsulates the logic for serializing the payload (as per the configured KafkaProperties).

In other words, the KafkaTemplate from MessageVerifierSender::send tries to serialize the payload as Avro, but it fails to do so (or ends up with a weird result) because the object passed to the MessageVerifierSender is a JSON string.


Ugly workaround:
When implementing the MessageVerifierSender, I had to deserialize the JSON back to my Avro object, and (only then) let KafkaTemplate do the serialization.

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