Skip to content

fix(json): serialize non-dict data as JSON in binary mode - #312

Open
RaphaelFakhri wants to merge 1 commit into
cloudevents:mainfrom
RaphaelFakhri:fix/json-write-data-non-dict
Open

RaphaelFakhri wants to merge 1 commit into
cloudevents:mainfrom
RaphaelFakhri:fix/json-write-data-non-dict

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Related Issue: #311

Type of change:

  • Bug fix (non-breaking change which fixes an issue)

Description:

JSONFormat.write_data only JSON-encodes dict data. Other JSON values, such as lists and booleans, fall through to str(data). Binary content mode then sends Python reprs like ['a', 'b'] or True as the body, even with datacontenttype: application/json. This affects the HTTP, Kafka, AMQP and RabbitMQ bindings.

This change applies the existing JSON handling to every value that isn't None, str or bytes. It keeps the current behavior otherwise:

Tests: test_write_data_non_dict_json_value covers a list, a boolean and a float with no content type, application/json and application/octet-stream. Without the fix, the list and boolean cases fail.


Pre-submission checklist:

  • I have read the CONTRIBUTING.md file.
  • I have signed off my commits using git commit --signoff.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the documentation (README.md, CHANGELOG.md, etc.) as necessary.
  • I have run pre-commit and tox and all checks pass.
  • This pull request is ready to be reviewed.

Signed-off-by: Raphael Fakhri <153192858+RaphaelFakhri@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant