Skip to content

Commit a86e9b3

Browse files
authored
Set JSON::SerializableError#attribute when appropriate (#16158)
1 parent cebc2b0 commit a86e9b3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

spec/std/json/serializable_spec.cr

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ class JSONAttrWithQueryAttributes
326326
getter? bar_present : Bool
327327
end
328328

329+
class JSONAttrWithKeyQueryAttribute
330+
include JSON::Serializable
331+
332+
@[JSON::Field(key: "is_foo")]
333+
property? foo : Bool
334+
end
335+
329336
module JSONAttrModule
330337
property moo : Int32 = 10
331338
end
@@ -664,6 +671,7 @@ describe "JSON::Serializable" do
664671
JSON
665672
end
666673
ex.location.should eq({4, 3})
674+
ex.attribute.should eq "foo"
667675
end
668676

669677
it "should parse extra fields (JSONAttrPersonExtraFields with on_unknown_json_attribute)" do
@@ -684,12 +692,13 @@ describe "JSON::Serializable" do
684692
it "raises if non-nilable attribute is nil" do
685693
error_message = <<-'MSG'
686694
Missing JSON attribute: name
687-
parsing JSONAttrPerson at line 1, column 1
695+
parsing JSONAttrPerson#name at line 1, column 1
688696
MSG
689697
ex = expect_raises ::JSON::SerializableError, error_message do
690698
JSONAttrPerson.from_json(%({"age": 30}))
691699
end
692700
ex.location.should eq({1, 1})
701+
ex.attribute.should eq "name"
693702
end
694703

695704
it "raises if not an object" do
@@ -719,6 +728,7 @@ describe "JSON::Serializable" do
719728
JSON
720729
end
721730
ex.location.should eq({3, 10})
731+
ex.attribute.should eq "age"
722732
end
723733

724734
it "doesn't emit null by default when doing to_json" do
@@ -1115,12 +1125,25 @@ describe "JSON::Serializable" do
11151125
it "raises if non-nilable attribute is nil" do
11161126
error_message = <<-'MSG'
11171127
Missing JSON attribute: foo
1118-
parsing JSONAttrWithQueryAttributes at line 1, column 1
1128+
parsing JSONAttrWithQueryAttributes#foo at line 1, column 1
11191129
MSG
11201130
ex = expect_raises ::JSON::SerializableError, error_message do
11211131
JSONAttrWithQueryAttributes.from_json(%({"is_bar": true}))
11221132
end
11231133
ex.location.should eq({1, 1})
1134+
ex.attribute.should eq "foo"
1135+
end
1136+
1137+
it "raises with key as attribute if non-nilable attribute is nil" do
1138+
error_message = <<-'MSG'
1139+
Missing JSON attribute: is_foo
1140+
parsing JSONAttrWithKeyQueryAttribute#is_foo at line 1, column 1
1141+
MSG
1142+
ex = expect_raises ::JSON::SerializableError, error_message do
1143+
JSONAttrWithKeyQueryAttribute.from_json(%({}))
1144+
end
1145+
ex.location.should eq({1, 1})
1146+
ex.attribute.should eq "is_foo"
11241147
end
11251148
end
11261149

src/json/serialization.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ module JSON
257257
@{{name}} = %var{name}
258258
else
259259
{% unless value[:has_default] || value[:nilable] %}
260-
raise ::JSON::SerializableError.new("Missing JSON attribute: {{value[:key].id}}", self.class.to_s, nil, *%location, nil)
260+
raise ::JSON::SerializableError.new("Missing JSON attribute: {{value[:key].id}}", self.class.to_s, {{value[:key]}}, *%location, nil)
261261
{% end %}
262262
end
263263

@@ -359,7 +359,7 @@ module JSON
359359

360360
module Strict
361361
protected def on_unknown_json_attribute(pull, key, key_location)
362-
raise ::JSON::SerializableError.new("Unknown JSON attribute: #{key}", self.class.to_s, nil, *key_location, nil)
362+
raise ::JSON::SerializableError.new("Unknown JSON attribute: #{key}", self.class.to_s, key, *key_location, nil)
363363
end
364364
end
365365

0 commit comments

Comments
 (0)