Skip to content

Commit 46661e2

Browse files
authored
Correctly reference global JSON/YAML modules (#16161)
1 parent 0be6ee6 commit 46661e2

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

spec/std/json/serializable_spec.cr

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,24 @@ class JSONInitializeOpts
577577
end
578578
end
579579

580-
describe "JSON mapping" do
580+
record Namespaced::JSON::Wrapper, name : String, options : Hash(String, ::JSON::Any::Type)? = nil do
581+
include ::JSON::Serializable
582+
end
583+
584+
describe "JSON::Serializable" do
585+
it "works with classes within `JSON` namespace" do
586+
Namespaced::JSON::Wrapper
587+
.from_json(<<-JSON)
588+
{
589+
"name": "foo",
590+
"options": {
591+
"foo": true
592+
}
593+
}
594+
JSON
595+
.to_json
596+
end
597+
581598
it "works with record" do
582599
JSONAttrPoint.new(1, 2).to_json.should eq "{\"x\":1,\"y\":2}"
583600
JSONAttrPoint.from_json(%({"x": 1, "y": 2})).should eq JSONAttrPoint.new(1, 2)

spec/std/yaml/serializable_spec.cr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,21 @@ class YAMLInitializeOpts
477477
end
478478
end
479479

480+
record Namespaced::YAML::Wrapper, name : String, options : Hash(String, ::YAML::Any::Type)? = nil do
481+
include ::YAML::Serializable
482+
end
483+
480484
describe "YAML::Serializable" do
485+
it "works with classes within `YAML` namespace" do
486+
Namespaced::YAML::Wrapper
487+
.from_yaml(<<-YAML)
488+
name: foo
489+
options:
490+
foo: true
491+
YAML
492+
.to_yaml
493+
end
494+
481495
it "works with record" do
482496
YAMLAttrPoint.new(1, 2).to_yaml.should eq "---\nx: 1\ny: 2\n"
483497
YAMLAttrPoint.from_yaml("---\nx: 1\ny: 2\n").should eq YAMLAttrPoint.new(1, 2)

src/json/serialization.cr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ module JSON
207207
# `%var`'s type must be exact to avoid type inference issues with
208208
# recursively defined serializable types
209209
{% for name, value in properties %}
210-
%var{name} = uninitialized ::Union({{value[:type]}})
210+
%var{name} = uninitialized ::Union(typeof(@{{ name }}))
211211
%found{name} = false
212212
{% end %}
213213

@@ -239,7 +239,7 @@ module JSON
239239
{% if value[:converter] %}
240240
{{value[:converter]}}.from_json(pull)
241241
{% else %}
242-
::Union({{value[:type]}}).new(pull)
242+
::Union(typeof(@{{ name }})).new(pull)
243243
{% end %}
244244
end
245245
%found{name} = true
@@ -365,12 +365,12 @@ module JSON
365365
end
366366

367367
module Unmapped
368-
@[JSON::Field(ignore: true)]
369-
property json_unmapped = Hash(String, JSON::Any).new
368+
@[::JSON::Field(ignore: true)]
369+
property json_unmapped = Hash(String, ::JSON::Any).new
370370

371371
protected def on_unknown_json_attribute(pull, key, key_location)
372372
json_unmapped[key] = begin
373-
JSON::Any.new(pull)
373+
::JSON::Any.new(pull)
374374
rescue exc : ::JSON::ParseException
375375
raise ::JSON::SerializableError.new(exc.message, self.class.to_s, key, *key_location, exc)
376376
end

src/yaml/serialization.cr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ module YAML
188188
end
189189
end
190190

191-
def initialize(*, __context_for_yaml_serializable ctx : YAML::ParseContext, __node_for_yaml_serializable node : ::YAML::Nodes::Node)
191+
def initialize(*, __context_for_yaml_serializable ctx : ::YAML::ParseContext, __node_for_yaml_serializable node : ::YAML::Nodes::Node)
192192
{% verbatim do %}
193193
{% begin %}
194194
{% properties = {} of Nil => Nil %}
@@ -212,14 +212,14 @@ module YAML
212212
# `%var`'s type must be exact to avoid type inference issues with
213213
# recursively defined serializable types
214214
{% for name, value in properties %}
215-
%var{name} = uninitialized ::Union({{value[:type]}})
215+
%var{name} = uninitialized ::Union(typeof(@{{ name }}))
216216
%found{name} = false
217217
{% end %}
218218

219219
case node
220-
when YAML::Nodes::Mapping
221-
YAML::Schema::Core.each(node) do |key_node, value_node|
222-
unless key_node.is_a?(YAML::Nodes::Scalar)
220+
when ::YAML::Nodes::Mapping
221+
::YAML::Schema::Core.each(node) do |key_node, value_node|
222+
unless key_node.is_a?(::YAML::Nodes::Scalar)
223223
key_node.raise "Expected scalar as key for mapping"
224224
end
225225

@@ -230,7 +230,7 @@ module YAML
230230
when {{value[:key]}}
231231
begin
232232
{% if value[:has_default] || value[:nilable] %}
233-
if YAML::Schema::Core.parse_null?(value_node)
233+
if ::YAML::Schema::Core.parse_null?(value_node)
234234
{% if value[:nilable] %}
235235
%var{name} = nil
236236
%found{name} = true
@@ -243,7 +243,7 @@ module YAML
243243
{% if value[:converter] %}
244244
{{value[:converter]}}.from_yaml(ctx, value_node)
245245
{% else %}
246-
::Union({{value[:type]}}).new(ctx, value_node)
246+
::Union(typeof(@{{ name }})).new(ctx, value_node)
247247
{% end %}
248248
%found{name} = true
249249
end
@@ -252,7 +252,7 @@ module YAML
252252
on_unknown_yaml_attribute(ctx, key, key_node, value_node)
253253
end
254254
end
255-
when YAML::Nodes::Scalar
255+
when ::YAML::Nodes::Scalar
256256
if node.value.empty? && node.style.plain? && !node.tag
257257
# We consider an empty scalar as an empty mapping
258258
else
@@ -353,11 +353,11 @@ module YAML
353353
end
354354

355355
module Unmapped
356-
@[YAML::Field(ignore: true)]
357-
property yaml_unmapped = Hash(String, YAML::Any).new
356+
@[::YAML::Field(ignore: true)]
357+
property yaml_unmapped = Hash(String, ::YAML::Any).new
358358

359359
protected def on_unknown_yaml_attribute(ctx, key, key_node, value_node)
360-
yaml_unmapped[key] = YAML::Any.new(ctx, value_node)
360+
yaml_unmapped[key] = ::YAML::Any.new(ctx, value_node)
361361
end
362362

363363
protected def on_to_yaml(yaml)

0 commit comments

Comments
 (0)