Skip to content

Commit 0922135

Browse files
committed
need to json.unmarshal data_base64
Signed-off-by: Doug Davis <[email protected]>
1 parent d834326 commit 0922135

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

v2/event/event_unmarshal.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package event
77

88
import (
99
"encoding/base64"
10+
"encoding/json"
1011
"errors"
1112
"fmt"
1213
"io"
@@ -385,9 +386,14 @@ func consumeData(e *Event, isBase64 bool, iter *jsoniter.Iterator) error {
385386
e.DataBase64 = true
386387

387388
// Allocate payload byte buffer
388-
base64Encoded := iter.ReadStringAsSlice()
389-
e.DataEncoded = make([]byte, base64.StdEncoding.DecodedLen(len(base64Encoded)))
390-
length, err := base64.StdEncoding.Decode(e.DataEncoded, base64Encoded)
389+
base64Encoded := iter.ReadString()
390+
var base64DeJSON string
391+
err := json.Unmarshal([]byte(`"`+base64Encoded+`"`), &base64DeJSON)
392+
if err != nil {
393+
return err
394+
}
395+
e.DataEncoded = make([]byte, base64.StdEncoding.DecodedLen(len(base64DeJSON)))
396+
length, err := base64.StdEncoding.Decode(e.DataEncoded, []byte(base64DeJSON))
391397
if err != nil {
392398
return err
393399
}

v2/event/event_unmarshal_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ func TestUnmarshal(t *testing.T) {
298298
DataBase64: true,
299299
},
300300
},
301+
"base64 json encoded data v1.0 - escaped json string": {
302+
body: []byte(`{
303+
"specversion": "1.0",
304+
"datacontenttype": "text/plain",
305+
"data_base64": "\\u002B\\u002B\\u002B\\u002B",
306+
"id": "ABC-123",
307+
"time": "` + now.Format(time.RFC3339Nano) + `",
308+
"type": "com.example.test",
309+
"dataschema": "http://example.com/schema",
310+
"source": "http://example.com/source"
311+
}`),
312+
want: &event.Event{
313+
Context: event.EventContextV1{
314+
Type: "com.example.test",
315+
Source: *sourceV1,
316+
DataSchema: schemaV1,
317+
ID: "ABC-123",
318+
Time: &now,
319+
DataContentType: event.StringOfTextPlain(),
320+
}.AsV1(),
321+
// base64 decode of "++++"
322+
DataEncoded: []byte{0xfb, 0xef, 0xbe},
323+
DataBase64: true,
324+
},
325+
},
301326
"base64 xml encoded data v1.0": {
302327
body: mustJsonMarshal(t, map[string]interface{}{
303328
"specversion": "1.0",

0 commit comments

Comments
 (0)