Skip to content

Commit

Permalink
Merge pull request #709 from vpulim/allowing-namespace-overriding
Browse files Browse the repository at this point in the history
Adding samples for overriding element namespaces with object keys.
  • Loading branch information
jsdevel committed Sep 9, 2015
2 parents 0145d9f + b28ac21 commit f68efd8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
14 changes: 12 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<ns1:name`, regardless of what the namespace should have been.
}, {timeout: 5000})
```


#### Options (optional)
- Accepts any option that the request module accepts, see [here.](https://github.com/mikeal/request)
- For example, you could set a timeout of 5 seconds on the request like this:
Expand All @@ -283,9 +293,9 @@ as default request options to the constructor:
### Client Events
Client instances emit the following events:

* request - Emitted before a request is sent. The event handler receives the
* request - Emitted before a request is sent. The event handler receives the
entire Soap request (Envelope) including headers.
* message - Emitted before a request is sent. The event handler receives the
* message - Emitted before a request is sent. The event handler receives the
Soap body contents. Useful if you don't want to log /store Soap headers.
* soapError - Emitted when an erroneous response is received.
Useful if you want to globally log errors.
Expand Down
53 changes: 37 additions & 16 deletions lib/wsdl.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ function trim(text) {
}

/**
* What we want is to copy properties from one object to another one and avoid
* properties overriding. This way we ensure the 'inheritance' of
* What we want is to copy properties from one object to another one and avoid
* properties overriding. This way we ensure the 'inheritance' of
* <xsd:extension base=...> 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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1303,7 +1303,7 @@ WSDL.prototype.xmlToObject = function(xml) {
return;
}


if (topSchema && topSchema[name + '[]']) {
if (!topObject[name])
topObject[name] = [];
Expand All @@ -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);
Expand Down Expand Up @@ -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];

Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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 {

Expand All @@ -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 = '';
Expand Down
3 changes: 2 additions & 1 deletion test/request-response-samples-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"s1:bstrResourceId": "034b7ea5-8a04-11e3-9710-0050569575d8"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s0="urn:MessageService" xmlns:s1="urn:MessageService2" xmlns:atls="http://tempuri.org/vc/atl/server/"><soap:Body><s0:Message><s1:bstrResourceId>034b7ea5-8a04-11e3-9710-0050569575d8</s1:bstrResourceId></s0:Message></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"return":"-1",
"bstrError":""
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><snp:Message xmlns:snp="urn:MessageService"><return>-1</return><bstrError></bstrError></snp:Message></soap:Body></soap:Envelope>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0"?>
<!-- ATL Server generated Web Service Description -->
<definitions
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:s0="urn:MessageService"
xmlns:s1="urn:MessageService2"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:atls="http://tempuri.org/vc/atl/server/"
targetNamespace="urn:MessageService"
xmlns="http://schemas.xmlsoap.org/wsdl/"
>
<types>
<s:schema targetNamespace="urn:MessageService" attributeFormDefault="qualified" elementFormDefault="qualified">
<s:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
</s:schema>
</types>
<message name="MessageIn">
<part name="bstrResourceID" type="s:string"/>
</message>
<message name="MessageOut">
<part name="return" type="s:string"/>
<part name="bstrError" type="s:string"/>
</message>
<portType name="MessageServiceSoap">
<operation name="Message">
<input message="s0:MessageIn"/>
<output message="s0:MessageOut"/>
</operation>
</portType>
<binding name="MessageServiceSoap" type="s0:MessageServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="Message">
<soap:operation soapAction="#Message" style="rpc"/>
<input>
<soap:body use="encoded" namespace="urn:MessageService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:MessageService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="MessageService">
<port name="MessageServiceSoap" binding="s0:MessageServiceSoap">
<soap:address location="http://localhost/Message/Message.dll?Handler=Default"/>
</port>
</service>
</definitions>

0 comments on commit f68efd8

Please sign in to comment.