Skip to content

Commit

Permalink
fix codec v0.3 double quote encoding (cloudevents#184)
Browse files Browse the repository at this point in the history
Signed-off-by: Diego Marangoni <diegomarangoni@me.com>
Signed-off-by: Scott Nichols <nicholss@google.com>
  • Loading branch information
diegomarangoni authored and Scott Nichols committed Sep 20, 2019
1 parent 7fdfeba commit 221b9e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
26 changes: 19 additions & 7 deletions pkg/cloudevents/transport/http/codec_v03.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,33 @@ func (v CodecV03) toHeaders(ec *cloudevents.EventContextV03) (http.Header, error
for k, v := range ec.Extensions {
// Per spec, map-valued extensions are converted to a list of headers as:
// CE-attrib-key
if mapVal, ok := v.(map[string]interface{}); ok {
switch v.(type) {
case string:
h.Set("ce-"+k, v.(string))

case map[string]interface{}:
mapVal := v.(map[string]interface{})

for subkey, subval := range mapVal {
if subvalstr, ok := v.(string); ok {
h.Set("ce-"+k+"-"+subkey, subvalstr)
continue
}

encoded, err := json.Marshal(subval)
if err != nil {
return nil, err
}
h.Set("ce-"+k+"-"+subkey, string(encoded))
}
continue
}
encoded, err := json.Marshal(v)
if err != nil {
return nil, err

default:
encoded, err := json.Marshal(v)
if err != nil {
return nil, err
}
h.Set("ce-"+k, string(encoded))
}
h.Set("ce-"+k, string(encoded))
}

return h, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudevents/transport/http/codec_v03_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestCodecV03_Encode(t *testing.T) {
"Ce-Source": {"http://example.com/source"},
"Ce-Subject": {"resource"},
"Ce-Schemaurl": {"http://example.com/schema"},
"Ce-Test": {`"extended"`},
"Ce-Test": {"extended"},
"Content-Type": {"application/json"},
},
Body: []byte(`{"hello":"world"}`),
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestCodecV03_Encode(t *testing.T) {
"Ce-Source": {"http://example.com/source"},
"Ce-Subject": {"resource"},
"Ce-Schemaurl": {"http://example.com/schema"},
"Ce-Test": {`"extended"`},
"Ce-Test": {"extended"},
"Ce-Asmap-A": {`"apple"`},
"Ce-Asmap-B": {`"banana"`},
"Ce-Asmap-C": {`{"d":"dog","e":"eel"}`},
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestCodecV03_Encode(t *testing.T) {
"Ce-Source": {"http://example.com/source"},
"Ce-Subject": {"resource"},
"Ce-Schemaurl": {"http://example.com/schema"},
"Ce-Test": {`"extended"`},
"Ce-Test": {"extended"},
"Ce-Asmap-A": {`"apple"`},
"Ce-Asmap-B": {`"banana"`},
"Ce-Asmap-C": {`{"d":"dog","e":"eel"}`},
Expand Down

0 comments on commit 221b9e9

Please sign in to comment.