ruby / ruby/psych

Cannot unmarshal into nameless class

Open
#538 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
597
Forks
223
Avg merge
11h 23m
Merged PRs (30d)
3

Description

Currently this library relies on class names in order to instantiate objects while unmarshaling, even if you have explicitly registered a tag via add_tag. Consequently, nameless classes do not properly survive a round trip.

require 'yaml'

w = Class.new do
    def initialize(foo)
        @foo = foo
    end
    def foo
        @foo
    end
    def encode_with(c)
        c['bar'] = @foo
    end
    def init_with(c)
        initialize(c['bar'])
        self
    end
end
YAML.add_tag('!sample', w)

x = w.new('abc')
y = x.to_yaml # "--- !sample\nbar: abc\n"
z = YAML.load(y) # {"bar"=>"abc"}
z.foo # NoMethodError (undefined method `foo' for {"bar"=>"abc"}:Hash)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the YAML.add_tag and YAML.load paths shown in the reproduction, then trace how the registered tag is resolved when the class has no name. Re-run the provided example as a regression case; done means loading the YAML returns the registered object and z.foo returns "abc".

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.