-
Notifications
You must be signed in to change notification settings - Fork 4.3k
[v2] Update JSON formatter to encode bytes as Base64 strings #9424
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
base: v2
Are you sure you want to change the base?
Conversation
For reviewer, thoughts on adding a regression test that would catch this? I'm thinking maybe in |
if isinstance(obj, datetime.datetime): | ||
return obj.isoformat() | ||
elif isinstance(obj, bytes): | ||
return base64.b64encode(obj).decode("utf-8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reviewer, I chose utf-8 here because this is a JSON encoder, and according to the JSON spec, JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.
else: | ||
return obj | ||
raise TypeError('Encountered unrecognized type in JSON encoder.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reviewer, according to Python documentation for json.dump, JSON encoders should be raising a TypeError
for any type that it does not encode. This will prevent the ambiguous "Circular reference detected" if the issue recurs in the future with a different type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Could you add a test case exercising this?
- Are the other output formats (table, text, yaml) okay?
The reason the issue addressed is unique to JSON is because JSON is our only formatter that does not support binary data. |
Follow-up bugfix for #9388.
Current Problem
When a service sends back a response containing a member with raw bytes, the CLI JSON formatter fails with an error and does not output the complete response. Since CBOR is not a modeled protocol by any service (as of writing), this bugfix should not be a breaking change.
Explanation of problem
When using the JSON wire protocol, a service encodes the raw bytes as base64 (string) to represent it in JSON before sending it back. CLI's JSON formatter perfectly handles strings.
However, when using CBOR as the wire protocol, the service sends the raw bytes, so now it is up to us to encode the bytes as base64 to represent it in JSON format.
Description of changes
Description of tests
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.