protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

`to_h` ignores the default value of enum

Open ganmacs opened this issue 4 months ago • 0 comments

What version of protobuf and what language are you using?

Version: commit b8d356701 Language: Ruby

What operating system (Linux, Windows, ...) and version?

macOS Sonoma 14.6.1

What runtime / compiler are you using (e.g., python version or gcc version)

$ ruby -v                                                                                                                                         
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
$ bundler -v                 
Bundler version 2.4.22

What did you do?

I added some test cases to basic.rb. and run rake test command.

https://github.com/ganmacs/protobuf/commit/4facc4872bb24400f0b8fabecc8d7bf0dcc70e4a

m = MapMessage.new(
  :map_string_int32 => {"a" => 1, "b" => 2},
  :map_string_msg => {"a" => TestMessage2.new(:foo => 1),
                      "b" => TestMessage2.new(:foo => 2)},
  :map_string_enum => {"b" => :Default}
)
expected_result = {
  :map_string_int32 => {"a" => 1, "b" => 2},
  :map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
  :map_string_enum => {"b" => :Default}
}
assert_equal expected_result, m.to_h # pass
m = Enumer.new(
  :optional_enum => :Default,
  :repeated_enum => [:A, :C]
)
expected_result = {
  :optional_enum => :Default,
  :repeated_enum => [:A, :C]
}
assert_equal expected_result,  m.to_h # error

What did you expect to see

Enumer.new(:optional_enum => :Default, :repeated_enum => [:A, :C]).to_h returns { :optional_enum => :Default, :repeated_enum => [:A, :C] }

What did you see instead?

Enumer.new(:optional_enum => :Default, :repeated_enum => [:A, :C]).to_h returns { :repeated_enum => [:A, :C] }

$ rake test

...

Failure: test_to_h(BasicTest::MessageContainerTest)
/Users/ganmacs/src/github.com/protocolbuffers/protobuf/ruby/tests/basic.rb:522:in `test_to_h'
     519:         :optional_enum => :Default,
     520:         :repeated_enum => [:A, :C]
     521:       }
  => 522:       assert_equal expected_result,  m.to_h # error
     523:     end
     524:
     525:
<{:optional_enum=>:Default, :repeated_enum=>[:A, :C]}> expected but was
<{:repeated_enum=>[:A, :C]}>

diff:
? {:optional_enum=>:Default, :repeated_enum=>[:A, :C]}

Anything else we should know about your project / environment

The protobuf definitions are

  • TestEnum https://github.com/protocolbuffers/protobuf/blob/b8d356701a788951d92a1b4f04881fb539ca62d7/ruby/tests/basic_test.proto#L103-L109
  • MapMessage https://github.com/protocolbuffers/protobuf/blob/b8d356701a788951d92a1b4f04881fb539ca62d7/ruby/tests/basic_test.proto#L138-L142
  • Enumer https://github.com/protocolbuffers/protobuf/blob/b8d356701a788951d92a1b4f04881fb539ca62d7/ruby/tests/basic_test.proto#L234-L242

ganmacs avatar Oct 19 '24 14:10 ganmacs