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

Inconsistent JSON conversion for proto message with repeated field #256

Open
hrsht opened this issue Apr 15, 2015 · 0 comments
Open

Inconsistent JSON conversion for proto message with repeated field #256

hrsht opened this issue Apr 15, 2015 · 0 comments

Comments

@hrsht
Copy link

hrsht commented Apr 15, 2015

If a proto message has a repeated field and that field is an empty array, the JSON serialization of the proto is inconsistent. Here is an example

class SomeArray < ::Protobuf::Message
  repeated :int32, :arr, 1
end

[2] pry(main)> some_arr = SomeArray.new
=> #<SomeArray arr=[]>
[3] pry(main)> some_arr.to_json
=> "{\"arr\":[]}"
[4] pry(main)> some_arr.arr = []
=> []
[5] pry(main)> some_arr.to_json
=> "{}"
[6] pry(main)> some_arr.arr
=> []
[7] pry(main)> some_arr.to_json
=> "{\"arr\":[]}"

This inconsistency is because setting an empty array to the repeated field deletes the key for that field from the internal message hash (named values). While referencing the field sets the default value of the field back in the internal values hash, which is an empty array.

IMO, when setting the field to an empty array, it should not delete the key from the internal values hash. However, I am open to any other suggestions.

Though, irrespective of the solution, the .to_json (and .to_hash) calls should return consistent results.

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

No branches or pull requests

1 participant