diff --git a/pkg/transformers/json/transformer.go b/pkg/transformers/json/transformer.go index 0421ebf20e..f15f60383c 100644 --- a/pkg/transformers/json/transformer.go +++ b/pkg/transformers/json/transformer.go @@ -44,6 +44,9 @@ func transformer(msg messaging.Message) (interface{}, error) { Channel: msg.Channel, Subtopic: msg.Subtopic, } + if ret.Subtopic == "" { + return nil, errors.Wrap(ErrTransform, errUnknownFormat) + } subs := strings.Split(ret.Subtopic, ".") if len(subs) == 0 { return nil, errors.Wrap(ErrTransform, errUnknownFormat) diff --git a/pkg/transformers/json/transformer_test.go b/pkg/transformers/json/transformer_test.go index 809c284724..89e39f2ce4 100644 --- a/pkg/transformers/json/transformer_test.go +++ b/pkg/transformers/json/transformer_test.go @@ -56,6 +56,9 @@ func TestTransformJSON(t *testing.T) { Format: msg.Subtopic, } + invalidFmt := msg + invalidFmt.Subtopic = "" + listJSON := json.Messages{ Data: []json.Message{ { @@ -100,6 +103,12 @@ func TestTransformJSON(t *testing.T) { json: jsonMsg, err: nil, }, + { + desc: "test transform JSON with an invalid subtopic", + msg: invalidFmt, + json: nil, + err: json.ErrTransform, + }, { desc: "test transform JSON array", msg: listMsg,