Skip to content

Commit

Permalink
Fix JSON Transformer empty format handling (#1429)
Browse files Browse the repository at this point in the history
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
  • Loading branch information
dborovcanin committed Jun 22, 2021
1 parent d3e34b1 commit 0a89f1d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/transformers/json/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions pkg/transformers/json/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func TestTransformJSON(t *testing.T) {
Format: msg.Subtopic,
}

invalidFmt := msg
invalidFmt.Subtopic = ""

listJSON := json.Messages{
Data: []json.Message{
{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 0a89f1d

Please sign in to comment.