crystal icon indicating copy to clipboard operation
crystal copied to clipboard

YAML Serialization fail if there's a class in a module with the same module name.

Open hugopl opened this issue 3 years ago • 1 comments

The serialization macro code doesn't use the :: code when expanding the type, so the code above fails to compile:

require "yaml"

module Foo
  class Foo
  end
  
  class Serialized
    include YAML::Serializable
    @foo = ""
  end
end

Foo::Serialized.from_yaml("")

Error:

There was a problem expanding macro 'macro_140364593214112'

Code in macro 'included'

 9 | ctx.read_alias(node, {{@type}}) do |obj|
                          ^
Called macro defined in macro 'included'

 9 | ctx.read_alias(node, {{@type}}) do |obj|

Which expanded to:

 > 1 | Foo::Serialized
       ^--------------
Error: undefined constant Foo::Serialized

If the class Foo is commented out it works as expected, the problem does not happen on JSON serialization.

hugopl avatar Jul 17 '22 15:07 hugopl

I just tried it and if we replace \{{@type}} with self, all YAML specs pass. Maybe that's the solution? 🤔

asterite avatar Jul 18 '22 11:07 asterite

It does indeed, I can't think a situation that this change could cause a change in the current API behavior, but I wouldn't be surprised if @HertzDevil appears with a corner case that shows that :sweat_smile:

I added a test case for this and created a PR, locally it passed all std_spec tests

hugopl avatar Sep 28 '22 22:09 hugopl