From 0adfaf975c3e08ddd5d0b83235b2497f4fa34532 Mon Sep 17 00:00:00 2001 From: Michael Newman Date: Thu, 26 Aug 2021 18:05:08 -0400 Subject: [PATCH] xsi:type currently requires a namespace, resulting in undefined if no XMLNS is defined. Making attributes working without namespace or xmlns definitions. --- src/wsdl/index.ts | 50 ++++++++++++++++++++++----------------------- test/client-test.js | 34 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/wsdl/index.ts b/src/wsdl/index.ts index 885b904b5..772bca25a 100644 --- a/src/wsdl/index.ts +++ b/src/wsdl/index.ts @@ -952,34 +952,32 @@ export class WSDL { child = []; } - const attrObj = child[this.options.attributesKey]; - if (attrObj && attrObj.xsi_type) { - const xsiType = attrObj.xsi_type; - - let prefix = xsiType.prefix || xsiType.namespace; - // Generate a new namespace for complex extension if one not provided - if (!prefix) { - prefix = nsContext.registerNamespace(xsiType.xmlns); - } else { - nsContext.declareNamespace(prefix, xsiType.xmlns); - } - xsiType.prefix = prefix; - } - - if (attrObj) { - for (const attrKey in attrObj) { - // handle complex extension separately - if (attrKey === 'xsi_type') { - const attrValue = attrObj[attrKey]; - attr += ' xsi:type="' + attrValue.prefix + ':' + attrValue.type + '"'; - attr += ' xmlns:' + attrValue.prefix + '="' + attrValue.xmlns + '"'; - - continue; - } else { - attr += ' ' + attrKey + '="' + xmlEscape(attrObj[attrKey]) + '"'; + const attrObj = child[this.options.attributesKey] || {}; + + Object.keys(attrObj).forEach((k) => { + const v = attrObj[k]; + if (k === 'xsi_type') { + let prefix = v.prefix || v.namespace; + if (v.xmlns) { + if (!prefix) { + prefix = nsContext.registerNamespace(v.xmlns); + } else { + // Generate a new namespace for complex extension if one not provided + nsContext.declareNamespace(prefix, v.xmlns); + } + } + let name = v.type; + if (prefix) { + name = `${prefix}:${name}`; + } + attr += ` xsi:type="${name}"`; + if (v.xmlns) { + attr += ` xmlns:${prefix}="${v.xmlns}"`; } + } else { + attr += ` ${k}="${xmlEscape(v)}"`; } - } + }); return attr; } diff --git a/test/client-test.js b/test/client-test.js index b75e06a6e..ec6202c97 100644 --- a/test/client-test.js +++ b/test/client-test.js @@ -753,6 +753,40 @@ var fs = require('fs'), }); }, baseUrl); }); + + it("should handle xsi:type without xmlns", function (done) { + soap.createClient( + __dirname + "/wsdl/default_namespace.wsdl", + meta.options, + function (err, client) { + assert.ok(client); + + var data = { + element: { + attributes: { + xsi_type: { + type: "Ty", + }, + }, + $value: "Hello World", + }, + }; + + var message = + 'Hello World'; + + client.MyOperation(data, function (err, result) { + assert.ok(client.lastRequest); + assert.ok(client.lastMessage); + assert.ok(client.lastEndpoint); + console.log(client.lastMessage) + assert.strictEqual(client.lastMessage, message); + done(); + }); + }, + baseUrl + ); + }); }); describe('Follow even non-standard redirects', function () {