@@ -326,6 +326,13 @@ class JSONAttrWithQueryAttributes
326326 getter? bar_present : Bool
327327end
328328
329+ class JSONAttrWithKeyQueryAttribute
330+ include JSON ::Serializable
331+
332+ @[JSON ::Field (key: " is_foo" )]
333+ property? foo : Bool
334+ end
335+
329336module JSONAttrModule
330337 property moo : Int32 = 10
331338end
@@ -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
0 commit comments