crystal
crystal copied to clipboard
YAML Serialization fail if there's a class in a module with the same module name.
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.
I just tried it and if we replace \{{@type}} with self, all YAML specs pass. Maybe that's the solution? 🤔
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