Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add option to remove element-by-element namespacing of json arrays #994

Merged
merged 1 commit into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ The `options` argument allows you to customize the client with the following pro
- disableCache: don't cache WSDL files, request them every time.
- overridePromiseSuffix: if your wsdl operations contains names with Async suffix, you will need to override the default promise suffix to a custom one, default: `Async`.
- normalizeNames: if your wsdl operations contains names with non identifier characters (`[^a-z$_0-9]`), replace them with `_`. Note: if using this option, clients using wsdls with two operations like `soap:method` and `soap-method` will be overwritten. Then, use bracket notation instead (`client['soap:method']()`).
- namespaceArrayElements: provides support for nonstandard array semantics. If true, JSON arrays of the form `{list: [{elem: 1}, {elem: 2}]}` are marshalled into xml as `<list><elem>1</elem></list> <list><elem>2</elem></list>`. If false, marshalls into `<list> <elem>1</elem> <elem>2</elem> </list>`. Default: `true`.

Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-alive'}` in SOAP headers to avoid truncation of longer chunked responses.

Expand Down
14 changes: 12 additions & 2 deletions lib/wsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,12 @@ WSDL.prototype._initializeOptions = function (options) {
}
this.options.handleNilAsNull = !!options.handleNilAsNull;

if (options.namespaceArrayElements !== undefined) {
this.options.namespaceArrayElements = options.namespaceArrayElements;
} else {
this.options.namespaceArrayElements = true;
}

// Allow any request headers to keep passing through
this.options.wsdl_headers = options.wsdl_headers;
this.options.wsdl_options = options.wsdl_options;
Expand Down Expand Up @@ -1718,9 +1724,13 @@ WSDL.prototype.objectToXML = function(obj, name, nsPrefix, nsURI, isFirst, xmlns
parts.push(openingTagParts.join(''));
} else {
openingTagParts.push('>');
parts.push(openingTagParts.join(''));
if(self.options.namespaceArrayElements || i === 0) {
parts.push(openingTagParts.join(''));
}
parts.push(body);
parts.push(['</', appendColon(correctOuterNsPrefix), name, '>'].join(''));
if(self.options.namespaceArrayElements || i === n-1) {
parts.push(['</', appendColon(correctOuterNsPrefix), name, '>'].join(''));
}
}
}
} else if (typeof obj === 'object') {
Expand Down
4 changes: 3 additions & 1 deletion test/client-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('SOAP Client', function() {
'namespace': 'tns'
},
'overridePromiseSuffix': 'Test',
'request': 'customRequest'
'request': 'customRequest',
'namespaceArrayElements': true
};

it('should set WSDL options to those specified in createClient', function(done) {
Expand All @@ -27,6 +28,7 @@ describe('SOAP Client', function() {
assert.ok(client.wsdl.options.overrideRootElement.namespace === 'tns');
assert.ok(typeof client.MyOperationTest === 'function');
assert.ok(client.wsdl.options.request, "customRequest");
assert.ok(client.wsdl.options.namespaceArrayElements === true);
done();
});
});
Expand Down
31 changes: 31 additions & 0 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,37 @@ var fs = require('fs'),
});
});

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) {
assert.ok(client);
var pathToArrayContainer = 'TimesheetV201511Mobile.TimesheetV201511MobileSoap.AddTimesheet.input.input.PeriodList';
var arrayParameter = _.get(client.describe(), pathToArrayContainer)['PeriodType[]'];
assert.ok(arrayParameter);
client.AddTimesheet({input: {PeriodList: {PeriodType: [{PeriodId: '1'}, {PeriodId: '2'}]}}}, function() {
var sentInputContent = client.lastRequest.substring(client.lastRequest.indexOf('<input>') + '<input>'.length, client.lastRequest.indexOf('</input>'));
assert.equal(sentInputContent, '<PeriodList><PeriodType><PeriodId>1</PeriodId><PeriodId>2</PeriodId></PeriodType></PeriodList>');
done();
});
});
});

it('shall generate correct payload for methods with array parameter when individual array elements are namespaced', function (done) {
// this is the default behavior for array element namespacing
soap.createClient(__dirname + '/wsdl/list_parameter.wsdl', {disableCache: true, namespaceArrayElements: true}, function(err, client) {
assert.ok(client);
assert.ok(client.wsdl.options.namespaceArrayElements === true);
var pathToArrayContainer = 'TimesheetV201511Mobile.TimesheetV201511MobileSoap.AddTimesheet.input.input.PeriodList';
var arrayParameter = _.get(client.describe(), pathToArrayContainer)['PeriodType[]'];
assert.ok(arrayParameter);
client.AddTimesheet({input: {PeriodList: {PeriodType: [{PeriodId: '1'}, {PeriodId: '2'}]}}}, function() {
var sentInputContent = client.lastRequest.substring(client.lastRequest.indexOf('<input>') + '<input>'.length, client.lastRequest.indexOf('</input>'));
assert.equal(sentInputContent, '<PeriodList><PeriodType><PeriodId>1</PeriodId></PeriodType><PeriodType><PeriodId>2</PeriodId></PeriodType></PeriodList>');
done();
});
});
});

it('shall generate correct payload for recursively-defined types', function (done) {
soap.createClient(__dirname + '/wsdl/recursive2.wsdl', function (err, client) {
if (err) {
Expand Down