From b28ac21eebfb8a54c2157e2c59913f61195e8408 Mon Sep 17 00:00:00 2001 From: jsdevel Date: Fri, 4 Sep 2015 17:52:02 -0700 Subject: [PATCH] Allowing namespace overriding for elements. --- Readme.md | 14 ++++- lib/wsdl.js | 53 +++++++++++++------ test/request-response-samples-test.js | 3 +- .../request.json | 3 ++ .../request.xml | 1 + .../response.json | 4 ++ .../response.xml | 1 + .../soap.wsdl | 51 ++++++++++++++++++ 8 files changed, 111 insertions(+), 19 deletions(-) create mode 100644 test/request-response-samples/Message__overriding_namespace_prefix/request.json create mode 100644 test/request-response-samples/Message__overriding_namespace_prefix/request.xml create mode 100644 test/request-response-samples/Message__overriding_namespace_prefix/response.json create mode 100644 test/request-response-samples/Message__overriding_namespace_prefix/response.xml create mode 100644 test/request-response-samples/Message__overriding_namespace_prefix/soap.wsdl diff --git a/Readme.md b/Readme.md index e5903d85b..3a904594b 100644 --- a/Readme.md +++ b/Readme.md @@ -259,6 +259,16 @@ as default request options to the constructor: // result is a javascript object }) ``` +###Overriding the namespace prefix +`node-soap` is still working out some kinks regarding namespaces. If you find that an element is given the wrong namespace prefix in the request body, you can add the prefix to it's name in the containing object. I.E.: + +```javascript + client.MyService.MyPort.MyFunction({'ns1:name': 'value'}, function(err, result) { + // request body sent with ` usage. * - * NB: 'Element' (and subtypes) don't have any prototyped properties: there's - * no need to process a 'hasOwnProperties' call, we should just iterate over the + * NB: 'Element' (and subtypes) don't have any prototyped properties: there's + * no need to process a 'hasOwnProperties' call, we should just iterate over the * keys. */ function extend(base, obj) { @@ -569,12 +569,12 @@ MessageElement.prototype.postProcess = function(definitions) { } else { this.parts[part.$name] = part.$type; } - + if (typeof this.parts[part.$name] === 'object') { this.parts[part.$name].namespace = nsName.namespace; this.parts[part.$name].xmlns = ns; } - + this.children.splice(i--, 1); } } @@ -887,7 +887,7 @@ ElementElement.prototype.description = function(definitions, xmlns) { else { element[name] = elem; } - + if (typeof elem === 'object') { elem.targetNSAlias = type.namespace; elem.targetNamespace = ns; @@ -1303,7 +1303,7 @@ WSDL.prototype.xmlToObject = function(xml) { return; } - + if (topSchema && topSchema[name + '[]']) { if (!topObject[name]) topObject[name] = []; @@ -1323,12 +1323,12 @@ WSDL.prototype.xmlToObject = function(xml) { refs[cur.id].obj = obj; } }; - + p.oncdata = function (text) { text = trim(text); if (!text.length) return; - + if (/<\?xml[\s\S]+\?>/.test(text)) { var top = stack[stack.length - 1]; var value = self.xmlToObject(text); @@ -1463,7 +1463,20 @@ WSDL.prototype.objectToRpcXML = function(name, params, namespace, xmlns) { return parts.join(''); }; -WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsAttr, parameterTypeObject, ancestorXmlns) { +/** + * Convert an object to XML. This is a recursive method as it calls itself. + * + * @param {Object} obj the object to convert. + * @param {String} name the name of the element (if the object being traversed is + * an element). + * @param {String} namespace the namespace prefix of the object I.E. xsd. + * @param {String} xmlns the full namespace of the object I.E. http://w3.org/schema. + * @param {Boolean} isFirst whether or not this is the first item being traversed. + * @param {?} xmlnsAttr + * @param {?} parameterTypeObject + * @param {?} ancestorXmlns + */ +WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, isFirst, xmlnsAttr, parameterTypeObject, ancestorXmlns) { var self = this; var schema = this.definitions.schemas[xmlns]; @@ -1479,7 +1492,7 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsA var prefixNamespace = (namespace || qualified) && namespace !== 'xmlns'; var xmlnsAttrib = ''; - if (xmlns && first) { + if (xmlns && isFirst) { if (prefixNamespace && this.options.ignoredNamespaces.indexOf(namespace) === -1) { // resolve the prefix namespace @@ -1490,18 +1503,19 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsA } var ancXmlns = ancestorXmlns ? ancestorXmlns : new Array(xmlns); - + // explicitly use xmlns attribute if available if (xmlnsAttr) { xmlnsAttrib = xmlnsAttr; } var ns = ''; - if (prefixNamespace && ((qualified || first) || soapHeader) && this.options.ignoredNamespaces.indexOf(namespace) === -1) { + if (prefixNamespace && ((qualified || isFirst) || soapHeader) && this.options.ignoredNamespaces.indexOf(namespace) === -1) { // prefix element ns = namespace.indexOf(":") === -1 ? namespace + ':' : namespace; } + // start building out XML string. if (Array.isArray(obj)) { for (var i = 0, item; item = obj[i]; i++) { var arrayAttr = self.processAttributes(item), @@ -1534,7 +1548,14 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsA var value = ''; var nonSubNameSpace = ''; - if (first) { + + var nameWithNsRegex = /^([^:]+):([^:]+)$/.exec(name); + if (nameWithNsRegex) { + nonSubNameSpace = nameWithNsRegex[1] + ':'; + name = nameWithNsRegex[2]; + } + + if (isFirst) { value = self.objectToXML(child, name, namespace, xmlns, false, null, parameterTypeObject, ancXmlns); } else { @@ -1548,7 +1569,7 @@ WSDL.prototype.objectToXML = function(obj, name, namespace, xmlns, first, xmlnsA if(childParameterTypeObject.$baseNameSpace) { //this element has a base with another namespace (the correct one) ns = childParameterTypeObject.$baseNameSpace + ':'; } - + var childParameterType = childParameterTypeObject.$type || childParameterTypeObject.$ref; var childNamespace = ''; diff --git a/test/request-response-samples-test.js b/test/request-response-samples-test.js index d847f98ae..6ed07592d 100644 --- a/test/request-response-samples-test.js +++ b/test/request-response-samples-test.js @@ -23,7 +23,8 @@ var requestContext = { requestHandler:function(req, res){ var chunks = []; req.on('data', function(chunk){ - chunks.push(chunk); + // ignore eol on sample files. + chunks.push(chunk.toString().replace(/\r?\n$/m, '')); }); req.on('end', function(){ if(!requestContext.expectedRequest)return res.end(requestContext.responseToSend); diff --git a/test/request-response-samples/Message__overriding_namespace_prefix/request.json b/test/request-response-samples/Message__overriding_namespace_prefix/request.json new file mode 100644 index 000000000..138f385fd --- /dev/null +++ b/test/request-response-samples/Message__overriding_namespace_prefix/request.json @@ -0,0 +1,3 @@ +{ + "s1:bstrResourceId": "034b7ea5-8a04-11e3-9710-0050569575d8" +} diff --git a/test/request-response-samples/Message__overriding_namespace_prefix/request.xml b/test/request-response-samples/Message__overriding_namespace_prefix/request.xml new file mode 100644 index 000000000..e11e9cee0 --- /dev/null +++ b/test/request-response-samples/Message__overriding_namespace_prefix/request.xml @@ -0,0 +1 @@ +034b7ea5-8a04-11e3-9710-0050569575d8 \ No newline at end of file diff --git a/test/request-response-samples/Message__overriding_namespace_prefix/response.json b/test/request-response-samples/Message__overriding_namespace_prefix/response.json new file mode 100644 index 000000000..79fdba308 --- /dev/null +++ b/test/request-response-samples/Message__overriding_namespace_prefix/response.json @@ -0,0 +1,4 @@ +{ + "return":"-1", + "bstrError":"" +} diff --git a/test/request-response-samples/Message__overriding_namespace_prefix/response.xml b/test/request-response-samples/Message__overriding_namespace_prefix/response.xml new file mode 100644 index 000000000..17169f0b0 --- /dev/null +++ b/test/request-response-samples/Message__overriding_namespace_prefix/response.xml @@ -0,0 +1 @@ +-1 \ No newline at end of file diff --git a/test/request-response-samples/Message__overriding_namespace_prefix/soap.wsdl b/test/request-response-samples/Message__overriding_namespace_prefix/soap.wsdl new file mode 100644 index 000000000..85deeaa7e --- /dev/null +++ b/test/request-response-samples/Message__overriding_namespace_prefix/soap.wsdl @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +