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 26, 2021
1 parent 926b682 commit 0adfaf9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/wsdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
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 0adfaf9

Please sign in to comment.