Skip to content

DelegatingByTypeSerializer does not work for Tombstones #2154

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
dspruth opened this issue Mar 8, 2022 · 1 comment · Fixed by #2160
Closed

DelegatingByTypeSerializer does not work for Tombstones #2154

dspruth opened this issue Mar 8, 2022 · 1 comment · Fixed by #2160

Comments

@dspruth
Copy link

dspruth commented Mar 8, 2022

Expected Behavior

DelegatingByTypeSerializer should not crash when receiving Tombstone (null value)
I should be able to configure a Serializer for Tombstones based on KafkaNull

@Bean
public ProducerFactory<Object, Object> producerFactory(KafkaProperties config) {
    return new DefaultKafkaProducerFactory<>(config.buildProducerProperties(),
            null,
            new MyDelegatingByTypeSerializer(Map.of(
                    String.class, new StringSerializer(),
                    byte[].class, new ByteArraySerializer(),
                    KafkaNull.class, new ByteArraySerializer())));
}

Current Behavior

DelegatingByTypeSerializer throws NullpointerException on Tombstone message due to

Serializer<?> delegate = delegates.get(data.getClass()); //data is null for null message

Context

I'm using tombstone messages to remove entries from compacted topics and trigger deletion in consumer services. When starting to use DelegatingByTypeSerializer producing the Tombstones lead to errors.
A possible fix could be:

if (data == null) {
    delegate = this.delegates.get(KafkaNull.class);
} else {
    delegate = this.delegates.get(data.getClass());
}
@garyrussell
Copy link
Contributor

garyrussell commented Mar 8, 2022

Thanks for reporting; I've changed this to a bug.

KafkaNull never appears in a ProducerRecord; it is an artifact for converting to/from spring-messaging Message<?>, which doesn't support null payloads; null values should simply be returned as null.

garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Mar 8, 2022
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Mar 8, 2022
Resolves spring-projects#2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Mar 8, 2022
Resolves spring-projects#2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**
garyrussell added a commit to garyrussell/spring-kafka that referenced this issue Mar 8, 2022
Resolves spring-projects#2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**
artembilan pushed a commit that referenced this issue Mar 8, 2022
Resolves #2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**
artembilan pushed a commit that referenced this issue Mar 8, 2022
Resolves #2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTypeSerializer.java
artembilan pushed a commit that referenced this issue Mar 8, 2022
Resolves #2154

Check for null and return.

**cherry-pick to 2.8.x, 2.7.x (partial)**

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTypeSerializer.java

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTopicSerializer.java
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingByTypeSerializer.java
#	spring-kafka/src/main/java/org/springframework/kafka/support/serializer/DelegatingSerializer.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants