From 650fcc22a726deb1137234c09f17e3a872853c98 Mon Sep 17 00:00:00 2001 From: Rishikesh Darandale Date: Tue, 19 Feb 2019 12:07:59 +0530 Subject: [PATCH] fix(wsdl): array namespace override with colon(:) Currently, the colon override(:) for array attribute is not preserved and thus by default the parent namespace or override provided namespace is used for array attribute. To handle this scenario/edge case, colon(:) is preserved for array attribute as well. `:array` will not have any namespace as normal attribute. --- src/wsdl/index.ts | 21 +++++- test/client-test.js | 58 +++++++++++++-- test/wsdl/array_namespace_override.wsdl | 98 +++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 9 deletions(-) create mode 100644 test/wsdl/array_namespace_override.wsdl diff --git a/src/wsdl/index.ts b/src/wsdl/index.ts index 8faf34389..ffb9fb5e3 100644 --- a/src/wsdl/index.ts +++ b/src/wsdl/index.ts @@ -689,10 +689,14 @@ export class WSDL { // start building out XML string. if (Array.isArray(obj)) { var nonSubNameSpace = ''; + var emptyNonSubNameSpaceForArray = false; var nameWithNsRegex = /^([^:]+):([^:]+)$/.exec(name); if (nameWithNsRegex) { nonSubNameSpace = nameWithNsRegex[1]; name = nameWithNsRegex[2]; + } else if (name[0] === ':') { + emptyNonSubNameSpaceForArray = true; + name = name.substr(1); } for (i = 0, n = obj.length; i < n; i++) { @@ -702,7 +706,10 @@ export class WSDL { var body = self.objectToXML(item, name, nsPrefix, nsURI, false, null, schemaObject, nsContext); - var openingTagParts = ['<', appendColon(correctOuterNsPrefix), name, arrayAttr, xmlnsAttrib]; + var openingTagParts = ['<', name, arrayAttr, xmlnsAttrib]; + if (!emptyNonSubNameSpaceForArray) { + openingTagParts = ['<', appendColon(correctOuterNsPrefix), name, arrayAttr, xmlnsAttrib]; + } if (body === '' && self.options.useEmptyTag) { // Use empty (self-closing) tags if no contents @@ -715,7 +722,11 @@ export class WSDL { } parts.push(body); if (self.options.namespaceArrayElements || i === n - 1) { - parts.push([''].join('')); + if (emptyNonSubNameSpaceForArray) { + parts.push([''].join('')); + } else { + parts.push([''].join('')); + } } } } @@ -874,7 +885,11 @@ export class WSDL { obj[self.options.attributesKey].xsi_type.xmlns, false, null, null, nsContext); } else { if (Array.isArray(child)) { - name = nonSubNameSpace + name; + if(emptyNonSubNameSpace) { + name = ':' + name; + } else { + name = nonSubNameSpace + name; + } } value = self.objectToXML(child, name, nsPrefix, nsURI, false, null, null, nsContext); diff --git a/test/client-test.js b/test/client-test.js index 2d5a22575..609e99e0d 100644 --- a/test/client-test.js +++ b/test/client-test.js @@ -94,12 +94,12 @@ var fs = require('fs'), }); }); - + it('should allow passing in XML strings', function (done) { soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', _.assign({ envelopeKey: 'soapenv' }, meta.options), function (err, client) { assert.ok(client); assert.ifError(err); - + var xmlStr = '\n\t\n\t\t404 - Not Found\n\t\n\t\n\t\t

404 - Not Found

\n\t\t\n\t\n'; client.MyOperation({_xml: xmlStr}, function (err, result, raw, soapHeader) { assert.ok(err); @@ -210,7 +210,7 @@ var fs = require('fs'), }, 'https://127.0.0.1:443'); }); - + it('should have xml request modified', function (done) { soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function(err, client) { assert.ok(client); @@ -230,7 +230,7 @@ var fs = require('fs'), ); }, baseUrl); }); - + it('should have the correct extra header in the request', function (done) { soap.createClient(__dirname + '/wsdl/default_namespace.wsdl', meta.options, function (err, client) { assert.ok(client); @@ -905,6 +905,52 @@ var fs = require('fs'), }); }); + it('shall generate correct payload for methods with array parameter with colon override', function (done) { + soap.createClient(__dirname + '/wsdl/array_namespace_override.wsdl', function(err, client) { + assert.ok(client); + var pathToArrayContainer = 'SampleArrayServiceImplService.SampleArrayServiceImplPort.createWebOrder.input.order'; + var arrayParameter = _.get(client.describe(), pathToArrayContainer)['orderDetails[]']; + assert.ok(arrayParameter); + const input = { + ':clientId': 'test', + ':order': { + ':orderDetails': { + ':unitNo': 1234, + ':items':[{ ':itemDesc': 'item1'}, { ':itemDesc': 'item2'}] + }, + }, + }; + client.createWebOrder(input, function() { + var sentInputContent = client.lastRequest.substring(client.lastRequest.indexOf(''), client.lastRequest.lastIndexOf('') + ''.length); + assert.equal(sentInputContent, 'item1item2'); + done(); + }); + }); + }); + + it('shall generate correct payload for methods with array parameter with parent namespace', function (done) { + soap.createClient(__dirname + '/wsdl/array_namespace_override.wsdl', function(err, client) { + assert.ok(client); + var pathToArrayContainer = 'SampleArrayServiceImplService.SampleArrayServiceImplPort.createWebOrder.input.order'; + var arrayParameter = _.get(client.describe(), pathToArrayContainer)['orderDetails[]']; + assert.ok(arrayParameter); + const input = { + ':clientId': 'test', + ':order': { + 'orderDetails': { + ':unitNo': 1234, + 'items':[{ ':itemDesc': 'item1'}, { ':itemDesc': 'item2'}] + }, + }, + }; + client.createWebOrder(input, function() { + var sentInputContent = client.lastRequest.substring(client.lastRequest.indexOf(''), client.lastRequest.lastIndexOf('') + ''.length); + assert.equal(sentInputContent, 'item1item2'); + done(); + }); + }); + }); + it('shall generate correct payload for methods with array parameter when individual array elements are not namespaced', function (done) { // used for servers that cannot aggregate individually namespaced array elements soap.createClient(__dirname + '/wsdl/list_parameter.wsdl', {disableCache: true, namespaceArrayElements: false}, function(err, client) { @@ -1001,7 +1047,7 @@ var fs = require('fs'), }); }); - + describe('Client created with createClientAsync', function () { it('should error on invalid host', function (done) { soap.createClientAsync('http://localhost:1', meta.options) @@ -1057,7 +1103,7 @@ var fs = require('fs'), done(); }); }); - + it('should allow customization of request for http client', function (done) { var myRequest = function () { }; diff --git a/test/wsdl/array_namespace_override.wsdl b/test/wsdl/array_namespace_override.wsdl new file mode 100644 index 000000000..c986a9baa --- /dev/null +++ b/test/wsdl/array_namespace_override.wsdl @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +