Skip to content

DelegatingByTypeSerializer does not support null payload messages #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dimalesfar opened this issue Mar 8, 2022 · 1 comment
Closed

Comments

@dimalesfar
Copy link

dimalesfar commented Mar 8, 2022

In what version(s) of Spring for Apache Kafka are you seeing this issue?
2.8.3

Describe the bug

DelegatingByTypeSerializer does not support null payload messages (Tombstone)

To Reproduce

@Bean
DefaultKafkaProducerFactoryCustomizer kafkaProducerFactoryCustomizer() {
    return producerFactory -> producerFactory.setValueSerializer((Serializer) new DelegatingByTypeSerializer (
            Map.of(
                   byte[].class, new ByteArraySerializer(),
                    AvroEvent.class, new KafkaAvroSerializer(),
                    XmlEvent.class, new CustumXmlSerializer())));
}

kafkaTemplate.send (key="abcd", value=null)

Expected behavior

Messages with value = null could be sent via KafkaTemplate

Actual behavior

Caused by: java.lang.NullPointerException: null
at org.springframework.kafka.support.serializer.DelegatingByTypeSerializer.findDelegate(DelegatingByTypeSerializer.java:114) ~[spring-kafka-2.8.3.jar:2.8.3]
at org.springframework.kafka.support.serializer.DelegatingByTypeSerializer.serialize(DelegatingByTypeSerializer.java:99) ~[spring-kafka-2.8.3.jar:2.8.3]
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:929) ~[kafka-clients-3.0.0.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:889) ~[kafka-clients-3.0.0.jar:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:993) ~[spring-kafka-2.8.3.jar:2.8.3]
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:649) ~[spring-kafka-2.8.3.jar:2.8.3]
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:429) ~[spring-kafka-2.8.

Workarround

public class CustomDelegatingByTypeSerializer extends DelegatingByTypeSerializer {

public CustomDelegatingByTypeSerializer(Map<Class<?>, Serializer> delegates) {
    super(delegates);
}

@Override
protected Serializer findDelegate(Object data, Map<Class<?>, Serializer> delegates) {
    if (data == null) {
        return new ByteArraySerializer();
    }
    return super.findDelegate(data, delegates);
}

}

Solution / proposition
All spring-kafka Serializer support messages with null values, this is why it's good if DelegatingByTypeSerializer can also handle it.

@garyrussell
Copy link
Contributor

Thanks for reporting; duplicate of #2154

PR: #2160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants