Changeset

11476:83e127eb91f9

util.datamapper: Deal with locally built stanzas missing xmlns So the problem is that xmlns is not inherited when building a stanza, and then :get_child(n, ns) with an explicit namespace does not find that such child tags. E.g. local t = st.stanza("foo", { xmlns = "urn:example:bar" }) :text_tag("hello", "world"); assert(t:get_child("hello", "urn:example:bar"), "This fails"); Meanwhile, during parsing (util.xmppstream or util.xml) child tags do get the parents xmlns when not overriding them. Thus, in the above example, if the stanza is passed trough `t = util.xml.parse(tostring(t))` then the assert succeeds. This change makes it so that it leaves out the namespace argument to :get_child when it is the same as the current/parent namespace, which behaves the same for both built and parsed stanzas.
author Kim Alvefur <zash@zash.se>
date Tue, 23 Mar 2021 19:52:59 +0100
parents 11475:9bd36e871f05
children 11477:c90ef8745779
files spec/util_datamapper_spec.lua teal-src/util/datamapper.tl util/datamapper.lua
diffstat 3 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_datamapper_spec.lua	Mon Mar 22 22:24:39 2021 +0100
+++ b/spec/util_datamapper_spec.lua	Tue Mar 23 19:52:59 2021 +0100
@@ -1,7 +1,9 @@
+local st
 local xml
 local map
 
 setup(function()
+	st = require "util.stanza";
 	xml = require "util.xml";
 	map = require "util.datamapper";
 end);
@@ -160,6 +162,32 @@
 			assert.same(disco, map.parse(disco_schema, disco_info));
 		end);
 
+		it("deals with locally built stanzas", function()
+			-- FIXME this could also be argued to be a util.stanza problem
+			local ver_schema = {
+				type = "object";
+				xml = {name = "iq"};
+				properties = {
+					type = {type = "string"; xml = {attribute = true}};
+					id = {type = "string"; xml = {attribute = true}};
+					version = {
+						type = "object";
+						xml = {name = "query"; namespace = "jabber:iq:version"};
+						properties = {name = "string"; version = "string"; os = "string"};
+					};
+				};
+			};
+			local ver_st = st.iq({type = "result"; id = "v1"})
+				:query("jabber:iq:version")
+					:text_tag("name", "Prosody")
+					:text_tag("version", "trunk")
+					:text_tag("os", "Lua 5.3")
+				:reset();
+
+			local data = {type = "result"; id = "v1"; version = {name = "Prosody"; version = "trunk"; os = "Lua 5.3"}}
+			assert.same(data, map.parse(ver_schema, ver_st));
+		end);
+
 	end);
 
 	describe("unparse", function()
--- a/teal-src/util/datamapper.tl	Mon Mar 22 22:24:39 2021 +0100
+++ b/teal-src/util/datamapper.tl	Tue Mar 23 19:52:59 2021 +0100
@@ -61,7 +61,7 @@
 	local proptype : json_type_name = "string"
 	local value_where : value_goes = propname and "in_text_tag" or "in_text"
 	local name = propname
-	local namespace = current_ns
+	local namespace : string
 	local prefix : string
 	local single_attribute : string
 	local enums : { any }
@@ -82,7 +82,7 @@
 			if xml.name then
 				name = xml.name
 			end
-			if xml.namespace then
+			if xml.namespace and xml.namespace ~= current_ns then
 				namespace = xml.namespace
 			end
 			if xml.prefix then
@@ -137,7 +137,7 @@
 		local attr = name
 		if prefix then
 			attr = prefix .. ':' .. name
-		elseif namespace ~= s.attr.xmlns then
+		elseif namespace and namespace ~= s.attr.xmlns then
 			attr = namespace .. "\1" .. name
 		end
 		return s.attr[attr]
@@ -250,7 +250,7 @@
 		local attr = name
 		if prefix then
 			attr = prefix .. ':' .. name
-		elseif namespace ~= current_ns then
+		elseif namespace and namespace ~= current_ns then
 			attr = namespace .. "\1" .. name
 		end
 
@@ -261,7 +261,7 @@
 		assert(single_attribute)
 		local propattr : { string : string } = {}
 
-		if namespace ~= current_ns then
+		if namespace and namespace ~= current_ns then
 			propattr.xmlns = namespace
 		end
 
--- a/util/datamapper.lua	Mon Mar 22 22:24:39 2021 +0100
+++ b/util/datamapper.lua	Tue Mar 23 19:52:59 2021 +0100
@@ -29,7 +29,7 @@
 	local proptype = "string"
 	local value_where = propname and "in_text_tag" or "in_text"
 	local name = propname
-	local namespace = current_ns
+	local namespace
 	local prefix
 	local single_attribute
 	local enums
@@ -50,7 +50,7 @@
 			if xml.name then
 				name = xml.name
 			end
-			if xml.namespace then
+			if xml.namespace and xml.namespace ~= current_ns then
 				namespace = xml.namespace
 			end
 			if xml.prefix then
@@ -105,7 +105,7 @@
 		local attr = name
 		if prefix then
 			attr = prefix .. ":" .. name
-		elseif namespace ~= s.attr.xmlns then
+		elseif namespace and namespace ~= s.attr.xmlns then
 			attr = namespace .. "\1" .. name
 		end
 		return s.attr[attr]
@@ -218,7 +218,7 @@
 		local attr = name
 		if prefix then
 			attr = prefix .. ":" .. name
-		elseif namespace ~= current_ns then
+		elseif namespace and namespace ~= current_ns then
 			attr = namespace .. "\1" .. name
 		end
 
@@ -229,7 +229,7 @@
 		assert(single_attribute)
 		local propattr = {}
 
-		if namespace ~= current_ns then
+		if namespace and namespace ~= current_ns then
 			propattr.xmlns = namespace
 		end