Skip to content

Commit

Permalink
xsi:type currently requires a namespace, resulting in undefined if no…
Browse files Browse the repository at this point in the history
… XMLNS is defined. Making attributes working without namespace or xmlns definitions.
  • Loading branch information
newmaniese committed Aug 30, 2021
1 parent 926b682 commit 87d5065
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/wsdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,34 +952,37 @@ export class WSDL {
child = [];
}

const attrObj = child[this.options.attributesKey];
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);
if (xsiType.xmlns) {
// 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;
}
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]) + '"';
Object.keys(attrObj).forEach((k) => {
const v = attrObj[k];
if (k === "xsi_type") {
let name = v.type;
if (v.prefix) {
name = `${v.prefix}:${name}`;
}
attr += ` xsi:type="${name}"`;
if (v.xmlns) {
attr += ` xmlns:${v.prefix}="${v.xmlns}"`;
}
} else {
attr += ` ${k}="${xmlEscape(v)}"`;
}
}
});

return attr;
}
Expand Down
34 changes: 34 additions & 0 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
'<Request xmlns="http://www.example.com/v1"><element xsi:type="Ty">Hello World</element></Request>';

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 () {
Expand Down

0 comments on commit 87d5065

Please sign in to comment.