Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
brendanmaguire opened this issue Oct 25, 2023 · 4 comments · Fixed by #229
Closed

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

brendanmaguire opened this issue Oct 25, 2023 · 4 comments · Fixed by #229
Labels
bug Something isn't working

Comments

@brendanmaguire
Copy link

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

  • Platform: Mac
  • Python Version: 3.11.5
  • cloudevents Python SDK version: 1.10.0
@xSAVIKx xSAVIKx added the bug Something isn't working label Oct 26, 2023
@xSAVIKx
Copy link
Member

xSAVIKx commented Oct 26, 2023

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
Copy link
Author

Hey @xSAVIKx . The Pydantic version is 2.4.2.

xSAVIKx added a commit that referenced this issue Oct 29, 2023
Replicates bug test data from the #228

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
xSAVIKx added a commit that referenced this issue Oct 29, 2023
Pydantic v2 .__dict__ has different behavior from what Pydantic v1 had and is not giving us `extra` fields anymore. On the other hand the iterator over the event gives us extras as well

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
xSAVIKx added a commit that referenced this issue Oct 30, 2023
* Add custom extension attribute to the test set.

Replicates bug test data from the #228

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* use modern `super` syntax

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Fix `black` language version

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Fixes #228

Pydantic v2 .__dict__ has different behavior from what Pydantic v1 had and is not giving us `extra` fields anymore. On the other hand the iterator over the event gives us extras as well

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Add missing EOF

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Add Pydantic fix to the changelog

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Add links to the changelog

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Bump version

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

* Update Black and MyPy versions

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>

---------

Signed-off-by: Yurii Serhiichuk <savik.ne@gmail.com>
@xSAVIKx
Copy link
Member

xSAVIKx commented Oct 30, 2023

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

@brendanmaguire
Copy link
Author

@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 ! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants