Skip to content
Open
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
8 changes: 5 additions & 3 deletions binding/format/protobuf/v2/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ func ToProto(e *event.Event) (*pb.CloudEvent, error) {
}
container.Attributes[name] = attr
}
container.Data = &pb.CloudEvent_BinaryData{
BinaryData: e.Data(),
if e.Data() != nil {
container.Data = &pb.CloudEvent_BinaryData{
BinaryData: e.Data(),
}
}
if e.DataContentType() == ContentTypeProtobuf {
if e.Data() != nil && e.DataContentType() == ContentTypeProtobuf {
anymsg := &anypb.Any{
TypeUrl: e.DataSchema(),
Value: e.Data(),
Expand Down
31 changes: 31 additions & 0 deletions binding/format/protobuf/v2/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package format_test

import (
"encoding/json"
"net/url"
"reflect"
"testing"
Expand Down Expand Up @@ -94,6 +95,36 @@ func TestProtobufFormatWithProtobufCodec(t *testing.T) {
require.True(payload2.GetCeBoolean())
}

func TestProtobufFormatWithoutDataDoesNotCreateInvalidJSON(t *testing.T) {
require := require.New(t)

e := event.New()
e.SetID("id")
e.SetSource("source")
e.SetType("type")
e.SetDataContentType(event.ApplicationJSON)

b, err := format.Protobuf.Marshal(&e)
require.NoError(err)

var e2 event.Event
require.NoError(format.Protobuf.Unmarshal(b, &e2))
require.Nil(e2.DataEncoded)

jsonData, err := e2.MarshalJSON()
require.NoError(err)
require.JSONEq(`{
"specversion":"1.0",
"id":"id",
"source":"source",
"type":"type",
"datacontenttype":"application/json"
}`, string(jsonData))

var out event.Event
require.NoError(json.Unmarshal(jsonData, &out))
}

func TestFromProto(t *testing.T) {
tests := []struct {
name string
Expand Down