From 204014578019d24c458550bad8fdb544b95190b3 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 23 Mar 2021 19:52:59 +0100 Subject: 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. --- spec/util_datamapper_spec.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'spec/util_datamapper_spec.lua') diff --git a/spec/util_datamapper_spec.lua b/spec/util_datamapper_spec.lua index 37b3e3b6..8a4eacfa 100644 --- a/spec/util_datamapper_spec.lua +++ b/spec/util_datamapper_spec.lua @@ -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 @@ describe("util.datampper", function() 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() -- cgit v1.2.3