Skip to content
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

Addressed issue in Kafka-pubsub for avro null messages #3531

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions common/component/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package kafka

import (
"bytes"
"context"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -263,7 +264,9 @@ func getSchemaSubject(topic string) string {
}

func (k *Kafka) DeserializeValue(message *sarama.ConsumerMessage, config SubscriptionHandlerConfig) ([]byte, error) {
// Null Data is valid and a tombstone record. It shouldn't be serialized
// Null Data is valid and a tombstone record.
// It shouldn't be going through schema validation and decoding
// Instead directly convert to JSON `null`
if message.Value == nil {
return []byte("null"), nil
}
Expand Down Expand Up @@ -354,8 +357,9 @@ func (k *Kafka) getSchemaRegistyClient() (srclient.ISchemaRegistryClient, error)
}

func (k *Kafka) SerializeValue(topic string, data []byte, metadata map[string]string) ([]byte, error) {
// Null Data is valid and a tombstone record. It shouldn't be serialized
if data == nil {
// Null Data is valid and a tombstone record.
// It should be converted to NULL and not go through schema validation & encoding
if bytes.Equal(data, []byte("null")) || data == nil {
return nil, nil
}

Expand Down
9 changes: 8 additions & 1 deletion common/component/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,14 @@ func TestSerializeValueCachingDisabled(t *testing.T) {
require.NoError(t, err)
})

t.Run("value published null, no error", func(t *testing.T) {
t.Run("value published 'null', no error", func(t *testing.T) {
act, err := k.SerializeValue("my-topic", []byte("null"), map[string]string{"valueSchemaType": "Avro"})

require.Nil(t, act)
require.NoError(t, err)
})

t.Run("value published nil, no error", func(t *testing.T) {
act, err := k.SerializeValue("my-topic", nil, map[string]string{"valueSchemaType": "Avro"})

require.Nil(t, act)
Expand Down
Loading