From: Alexis Laferrière Date: Sat, 15 Oct 2016 00:58:55 +0000 (-0400) Subject: nitrestful & lib: use static type to limit and as heuristic at deserialization X-Git-Url: http://nitlanguage.org nitrestful & lib: use static type to limit and as heuristic at deserialization Signed-off-by: Alexis Laferrière --- diff --git a/lib/nitcorn/restful.nit b/lib/nitcorn/restful.nit index abaaf0d..af3fe1d 100644 --- a/lib/nitcorn/restful.nit +++ b/lib/nitcorn/restful.nit @@ -62,18 +62,18 @@ class RestfulAction redef fun answer(request, truncated_uri) do return new HttpResponse(400) - # Service to deserialize arguments from JSON + # Deserialize `val` from JSON for a parameter typed by `static_type` # # Accepts `nullable String` for convenience, but returns `null` when `val == null`. # # This method is called by the code generated by `nitrestful`. # It can be specialized to customize its behavior. - protected fun deserialize_arg(val: nullable String): nullable Object + protected fun deserialize_arg(val: nullable String, static_type: String): nullable Object do if val == null then return null var deserializer = new JsonDeserializer(val) - var obj = deserializer.deserialize + var obj = deserializer.deserialize(static_type) if deserializer.errors.not_empty then print_error deserializer.errors.join("\n") diff --git a/src/nitrestful.nit b/src/nitrestful.nit index cf590ce..2303974 100644 --- a/src/nitrestful.nit +++ b/src/nitrestful.nit @@ -131,7 +131,7 @@ redef class MType else # Deserialize everything else template.add """ - var out_{{{arg_name}}} = deserialize_arg(in_{{{arg_name}}}) + var out_{{{arg_name}}} = deserialize_arg(in_{{{arg_name}}}, "{{{self.name}}}") """ end end @@ -263,7 +263,11 @@ redef class {{{mclass}}} """ var mtype = param.mtype - mtype.gen_arg_convert(t, param.name) + var bound_mtype = mclassdef.bound_mtype + var resolved_mtype = mtype.resolve_for(bound_mtype, bound_mtype, mclassdef.mmodule, true) + var resolved_type_name = resolved_mtype.name + + resolved_mtype.gen_arg_convert(t, param.name) var arg = "out_{param.name}" args.add arg