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

[Ruby] allow encode json options to be an object that responds to to_hash #9513

Merged
merged 2 commits into from Mar 8, 2022

Conversation

lukad
Copy link
Contributor

@lukad lukad commented Feb 16, 2022

fixes #9500

@lukad lukad changed the title allow encode json options to be an object that responds to to_hash [Ruby] allow encode json options to be an object that responds to to_hash Feb 16, 2022
@haberman
Copy link
Member

What is an example of how this arises in real code?

What is a json::Ext::Generator::State object, and why is your code passing that in instead of Hash?

@lukad
Copy link
Contributor Author

lukad commented Feb 23, 2022

We ran into this in a Rails application that serves a JSON api and calls out to a GRPC Service. The JSON response is supposed to include the protobuf message.

msg = protobuf_message # returned from a GRPC service
{
  msg: protobuf_message,
  # and other fields
}.to_json

json::Ext::Generator::State holds Ruby's json generator options (like identation or space). This will always be created when calling to_json on a Hash and it will be passed down as opts to all subsequent to_json calls on the Hash's values values values as opts.

It is not safe for protobuf to assume that the opts passed to a message's to_json are always a hash.

Links:

@@ -870,6 +870,9 @@ def test_protobuf_encode_decode_json_helpers

decoded_msg = Google::Protobuf.decode_json(proto_module::TestMessage, encoded_msg)
assert_equal proto_module::TestMessage.decode_json(m.to_json), decoded_msg

assert_equal [m].to_json, Google::Protobuf.encode_json([m])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I am missing something, but this test doesn't seem to exercise the new code. We need a test that calls encode_json with a second parameter that response to to_h but is not itself a Hash.

Copy link
Contributor Author

@lukad lukad Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[m].to_json will do exactly that. Ruby's #to_json creates a json::Ext::Generator::State which is not a Hash. That state object will then be passed to m.to_json which in turn passes the state to encode_json.

Withouth my changes the assert fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An ArgumentError: Expected hash arguments is raised without my changes.

Error: test_protobuf_encode_decode_json_helpers(BasicTest::MessageContainerTest): ArgumentError: Expected hash arguments.
/Users/luka/code/protobuf/ruby/lib/google/protobuf/message_exts.rb:44:in `encode_json'
/Users/luka/code/protobuf/ruby/lib/google/protobuf/message_exts.rb:44:in `to_json'
/Users/luka/code/protobuf/ruby/tests/common_tests.rb:874:in `to_json'
/Users/luka/code/protobuf/ruby/tests/common_tests.rb:874:in `test_protobuf_encode_decode_json_helpers'
     871:     decoded_msg = Google::Protobuf.decode_json(proto_module::TestMessage, encoded_msg)
     872:     assert_equal proto_module::TestMessage.decode_json(m.to_json), decoded_msg
     873:
  => 874:     assert_equal [m].to_json, Google::Protobuf.encode_json([m])
     875:     assert_equal proto_module::TestMessage.decode_json([m.to_json].first), decoded_msg
     876:   end
     877:

@haberman haberman merged commit 24a0659 into protocolbuffers:master Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling to_json on a Hash that contains a protobuf message value raises an ArgumentError
4 participants