cloudevents / sdk-python

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

to_json on a pydantic.CloudEvent does not include extension values #228

Closed brendanmaguire closed 11 months ago

brendanmaguire commented 11 months ago

Expected Behavior

When running the following:

from cloudevents.conversion import to_json
from cloudevents.http import CloudEvent as HttpCloudEvent
from cloudevents.pydantic import CloudEvent as PydanticCloudEvent

data = {"data-key": "val"}
attributes = {
    "type": "com.example.string",
    "source": "https://example.com/event-producer",
    "extension-key": "test-value",
}

pydantic_event = PydanticCloudEvent(attributes, data)
http_event = HttpCloudEvent(attributes, data)

print(f'Pydantic Event JSON:\n{to_json(pydantic_event)}\n')
print(f'HTTP Event JSON:\n{to_json(http_event)}\n')

the following should be output:

Pydantic Event JSON:
b'{"specversion": "1.0", "id": "fb848780-bf68-48db-8497-efbedacc495f", "source": "https://example.com/event-producer", "type": "com.example.string", "time": "2023-10-25T13:46:20.207850+00:00", "data": {"data-key": "val"}, "extension-key": "test-value"}}'

HTTP Event JSON:
b'{"specversion": "1.0", "id": "49c6a9a8-c640-428f-ad1a-dce111008cdf", "source": "https://example.com/event-producer", "type": "com.example.string", "time": "2023-10-25T13:46:20.208740+00:00", "data": {"data-key": "val"}, "extension-key": "test-value"}'

Actual Behavior

This is the output:

Pydantic Event JSON:
b'{"specversion": "1.0", "id": "fb848780-bf68-48db-8497-efbedacc495f", "source": "https://example.com/event-producer", "type": "com.example.string", "time": "2023-10-25T13:46:20.207850+00:00", "data": {"data-key": "val"}}'

HTTP Event JSON:
b'{"specversion": "1.0", "id": "49c6a9a8-c640-428f-ad1a-dce111008cdf", "source": "https://example.com/event-producer", "type": "com.example.string", "time": "2023-10-25T13:46:20.208740+00:00", "data": {"data-key": "val"}, "extension-key": "test-value"}'

Note the missing extension-key value from the Pydantic Event JSON.

Steps to Reproduce the Problem

  1. Run the above code block

Specifications

xSAVIKx commented 11 months ago

Hey @brendanmaguire, thx for opening this issue. I'll try to take a look at it closer to the weekend, but would appreciate a PR if you're up to opening one.

Can you also mention the Pydantic version you're using?

brendanmaguire commented 11 months ago

Hey @xSAVIKx . The Pydantic version is 2.4.2.

xSAVIKx commented 11 months ago

@brendanmaguire the version 1.10.1 is now released. Please try it out.

brendanmaguire commented 11 months ago

@brendanmaguire the version 1.10.1 is now released. Please try it out.

Tested and it works as expected. Thanks for the fast turnaround @xSAVIKx ! 🙂