Skip to content

Commit 65b45e4

Browse files
authored
Merge pull request #1164 from alank-ps/main
fix: consumeData(and consumeDataAsBytes) should consider structued mode JSON …
2 parents 506a8fe + bb544e0 commit 65b45e4

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

v2/event/content_type.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ const (
1414
ApplicationCloudEventsBatchJSON = "application/cloudevents-batch+json"
1515
)
1616

17+
// isJSON returns true if the content type is a JSON type.
18+
func isJSON(contentType string) bool {
19+
switch contentType {
20+
case ApplicationJSON, TextJSON, ApplicationCloudEventsJSON, ApplicationCloudEventsBatchJSON:
21+
return true
22+
case "":
23+
return true // Empty content type assumes json
24+
default:
25+
return false
26+
}
27+
}
28+
1729
// StringOfApplicationJSON returns a string pointer to "application/json"
1830
func StringOfApplicationJSON() *string {
1931
a := ApplicationJSON

v2/event/event_marshal.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,8 @@ func WriteJson(in *Event, writer io.Writer) error {
150150
mediaType = strings.TrimSpace(strings.ToLower(contentType[0:i]))
151151
}
152152

153-
jsonMediaTypes := map[string]bool{
154-
"": true,
155-
ApplicationJSON: true,
156-
TextJSON: true,
157-
ApplicationCloudEventsJSON: true,
158-
ApplicationCloudEventsBatchJSON: true,
159-
}
160-
// If isJson and no encoding to base64, we don't need to perform additional steps
161-
if jsonMediaTypes[mediaType] && !isBase64 {
153+
// If IsJSON and no encoding to base64, we don't need to perform additional steps
154+
if isJSON(mediaType) && !isBase64 {
162155
stream.WriteObjectField("data")
163156
_, err := stream.Write(in.DataEncoded)
164157
if err != nil {

v2/event/event_unmarshal.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ func consumeDataAsBytes(e *Event, isBase64 bool, b []byte) error {
365365
}
366366

367367
mt, _ := e.Context.GetDataMediaType()
368-
// Empty content type assumes json
369-
if mt != "" && mt != ApplicationJSON && mt != TextJSON {
368+
if !isJSON(mt) {
370369
// If not json, then data is encoded as string
371370
iter := jsoniter.ParseBytes(jsoniter.ConfigFastest, b)
372371
src := iter.ReadString() // handles escaping
@@ -402,7 +401,7 @@ func consumeData(e *Event, isBase64 bool, iter *jsoniter.Iterator) error {
402401
}
403402

404403
mt, _ := e.Context.GetDataMediaType()
405-
if mt != ApplicationJSON && mt != TextJSON {
404+
if !isJSON(mt) {
406405
// If not json, then data is encoded as string
407406
src := iter.ReadString() // handles escaping
408407
e.DataEncoded = []byte(src)

v2/event/event_unmarshal_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,94 @@ func TestUnmarshal(t *testing.T) {
198198
}),
199199
},
200200
},
201+
"structured mode data v1.0": {
202+
body: mustJsonMarshal(t, map[string]interface{}{
203+
"specversion": "1.0",
204+
"datacontenttype": "application/cloudevents+json",
205+
"data": map[string]interface{}{
206+
"a": 42,
207+
"b": "testing",
208+
},
209+
"id": "ABC-123",
210+
"time": now.Format(time.RFC3339Nano),
211+
"type": "com.example.test",
212+
"exbool": true,
213+
"exint": 42,
214+
"exstring": "exstring",
215+
"exbinary": "AAECAw==",
216+
"exurl": "http://example.com/source",
217+
"extime": now.Format(time.RFC3339Nano),
218+
"dataschema": "http://example.com/schema",
219+
"source": "http://example.com/source",
220+
}),
221+
want: &event.Event{
222+
Context: event.EventContextV1{
223+
Type: "com.example.test",
224+
Source: *sourceV1,
225+
DataSchema: schemaV1,
226+
ID: "ABC-123",
227+
Time: &now,
228+
DataContentType: event.StringOfApplicationCloudEventsJSON(),
229+
Extensions: map[string]interface{}{
230+
"exbool": true, // Boolean should be preserved
231+
"exint": int32(42),
232+
"exstring": "exstring",
233+
// Since byte, url and time are encoded as string, the unmarshal should just convert them to string
234+
"exbinary": "AAECAw==",
235+
"exurl": "http://example.com/source",
236+
"extime": now.Format(time.RFC3339Nano),
237+
},
238+
}.AsV1(),
239+
DataEncoded: mustJsonMarshal(t, DataExample{
240+
AnInt: 42,
241+
AString: "testing",
242+
}),
243+
},
244+
},
245+
"structured mode batch data v1.0": {
246+
body: mustJsonMarshal(t, map[string]interface{}{
247+
"specversion": "1.0",
248+
"datacontenttype": "application/cloudevents-batch+json",
249+
"data": map[string]interface{}{
250+
"a": 42,
251+
"b": "testing",
252+
},
253+
"id": "ABC-123",
254+
"time": now.Format(time.RFC3339Nano),
255+
"type": "com.example.test",
256+
"exbool": true,
257+
"exint": 42,
258+
"exstring": "exstring",
259+
"exbinary": "AAECAw==",
260+
"exurl": "http://example.com/source",
261+
"extime": now.Format(time.RFC3339Nano),
262+
"dataschema": "http://example.com/schema",
263+
"source": "http://example.com/source",
264+
}),
265+
want: &event.Event{
266+
Context: event.EventContextV1{
267+
Type: "com.example.test",
268+
Source: *sourceV1,
269+
DataSchema: schemaV1,
270+
ID: "ABC-123",
271+
Time: &now,
272+
DataContentType: event.StringOfApplicationCloudEventsBatchJSON(),
273+
Extensions: map[string]interface{}{
274+
"exbool": true, // Boolean should be preserved
275+
"exint": int32(42),
276+
"exstring": "exstring",
277+
// Since byte, url and time are encoded as string, the unmarshal should just convert them to string
278+
"exbinary": "AAECAw==",
279+
"exurl": "http://example.com/source",
280+
"extime": now.Format(time.RFC3339Nano),
281+
},
282+
}.AsV1(),
283+
DataEncoded: mustJsonMarshal(t, DataExample{
284+
AnInt: 42,
285+
AString: "testing",
286+
}),
287+
},
288+
},
201289
"string data v1.0": {
202290
body: mustJsonMarshal(t, map[string]interface{}{
203291
"specversion": "1.0",

0 commit comments

Comments
 (0)