aetherknight / recursive-open-struct

OpenStruct subclass that returns nested hash attributes as RecursiveOpenStructs
Other
276 stars 54 forks source link

Can't serialize ROS as YAML #69

Open aramgre opened 3 years ago

aramgre commented 3 years ago

Ruby 2.7.1 recursive-open-struct 1.1.3 YAML 3.1.0

Deserializing a ROS from YAML does not work. I think this is because YAML/Psych calls respond_to_missing? before the object is fully loaded, see similar issue in stripe-ruby gem https://github.com/stripe/stripe-ruby/commit/8bb6acfca9b20cc028410bc66e2ba0491fba3c7e

irb(main):001:0> require 'recursive-open-struct'
=> true
irb(main):002:0> require 'yaml'
=> true
irb(main):003:0> ros = RecursiveOpenStruct.new({x: 1})
irb(main):004:0> yaml = YAML.dump(ros)
irb(main):005:0> YAML.load(yaml)
Traceback (most recent call last):
       16: from (irb):5
       15: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych.rb:279:in `load'
       14: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/nodes/node.rb:50:in `to_ruby'
       13: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'
       12: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'
       11: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'
       10: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:313:in `visit_Psych_Nodes_Document'
        9: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:32:in `accept'
        8: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/visitor.rb:6:in `accept'
        7: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/visitor.rb:16:in `visit'
        6: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:208:in `visit_Psych_Nodes_Mapping'
        5: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:394:in `revive'
        4: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:401:in `init_with'
        3: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/2.7.0/psych/visitors/to_ruby.rb:401:in `respond_to?'
        2: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/recursive-open-struct-1.1.3/lib/recursive_open_struct.rb:105:in `respond_to_missing?'
        1: from /Users/aram/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/recursive-open-struct-1.1.3/lib/recursive_open_struct.rb:182:in `_get_key_from_table_'
NoMethodError (undefined method `has_key?' for nil:NilClass)