cloudevents / sdk-python

Python SDK for CloudEvents
https://pypi.org/p/cloudevents/
Apache License 2.0
266 stars 53 forks source link

Encoded JSON data is not set correctly inside data field #185

Closed sasha-tkachev closed 1 year ago

sasha-tkachev commented 2 years ago

Expected Behavior

JSON format spec section [#3.1.1] states

If the datacontenttype declares the data to contain JSON-formatted content, a JSON serializer MUST translate the data value to a JSON value, and use the member name data to store it inside the JSON representation. The data value MUST be stored directly as a JSON value, rather than as an encoded JSON document represented as a string.

from cloudevents.http import CloudEvent, to_json
import json
json.loads(
to_json(
CloudEvent(
attributes={"type": "my-type", "source": "my-source"},
data='{"a": "b"}',
)
)
)["data"] =={"a": "b"}

Actual Behavior

When an encoded json value is passed into the cloud even, when to_json is invoked the resulting json object has an encoded json object in the data field and not a JSON representation as required.

from cloudevents.http import CloudEvent, to_json
import json
json.loads(
     to_json(
         CloudEvent(
             attributes={"type": "my-type", "source": "my-source"},
             data='{"a": "b"}',
         )
     )
 )["data"] == '{"a": "b"}' 

Specifications

#3.1.1

xSAVIKx commented 2 years ago

Hey @sasha-tkachev. Thx for opening the issue.

Although, I'm not sure this is actually an issue. Can you please verify how Event behaves? CloudEvent class as noted is a Python-friendly wrapper and thus is not obliged to follow the spec and may have different behavior.

There's also an open issue in the spec itself that I believe is related to the behavior you're showcasing: https://github.com/cloudevents/spec/issues/558.

sasha-tkachev commented 1 year ago

Ok