serialization: don't throw missing attribute error if there is a default value
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 30 Aug 2016 02:32:44 +0000 (22:32 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 30 Aug 2016 13:45:23 +0000 (09:45 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/json/serialization.nit
src/frontend/serialization_phase.nit

index 8a80240..ac29a62 100644 (file)
@@ -302,7 +302,7 @@ class JsonDeserializer
                var current = path.last
 
                if not current.keys.has(name) then
-                       errors.add new Error("Deserialization Error: JSON object has not attribute '{name}'.")
+                       # Let the generated code / caller of `deserialize_attribute` raise the missing attribute error
                        deserialize_attribute_missing = true
                        return null
                end
index 7fc1c31..c054558 100644 (file)
@@ -312,9 +312,18 @@ do
                                code.add """
        self.{{{name}}} = v.deserialize_attribute("{{{attribute.serialize_name}}}", "{{{type_name}}}")
 """
-                       else code.add """
+                       else
+                               code.add """
        var {{{name}}} = v.deserialize_attribute("{{{attribute.serialize_name}}}", "{{{type_name}}}")
        if v.deserialize_attribute_missing then
+"""
+                               # What to do when an attribute is missing?
+                               if attribute.has_value then
+                                       # Leave it to the default value
+                               else code.add """
+               v.errors.add new Error("Deserialization Error: attribute `{class_name}::{{{name}}}` missing from JSON object")"""
+
+                               code.add """
        else if not {{{name}}} isa {{{type_name}}} then
                v.errors.add new AttributeTypeError(self, "{{{attribute.serialize_name}}}", {{{name}}}, "{{{type_name}}}")
                if v.keep_going == false then return
@@ -322,6 +331,7 @@ do
                self.{{{name}}} = {{{name}}}
        end
 """
+                       end
                end
 
                code.add "end"