Skip to content

Commit b17b7f3

Browse files
Add option overrideElementKey to change the key of the wsdl (#1334)
* Update readme --------- Co-authored-by: Lucas Fellipe Mondini Pereira <[email protected]>
1 parent ed0b827 commit b17b7f3

File tree

18 files changed

+194
-5
lines changed

18 files changed

+194
-5
lines changed

Readme.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This module lets you connect to web services using SOAP. It also provides a ser
1010
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1111
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1212

13-
- [Features:](#features)
13+
- [Features](#features)
1414
- [Install](#install)
1515
- [Support](#support)
1616
- [Module](#module)
@@ -62,6 +62,7 @@ This module lets you connect to web services using SOAP. It also provides a ser
6262
- [Overriding imports relative paths](#overriding-imports-relative-paths)
6363
- [Overriding import locations](#overriding-import-locations)
6464
- [Specifying the exact namespace definition of the root element](#specifying-the-exact-namespace-definition-of-the-root-element)
65+
- [Overriding element key specification in XML](#overriding-element-key-specification-in-xml)
6566
- [Custom Deserializer](#custom-deserializer)
6667
- [Changing the tag formats to use self-closing (empty element) tags](#changing-the-tag-formats-to-use-self-closing-empty-element-tags)
6768
- [Handling "ignored" namespaces](#handling-ignored-namespaces)
@@ -72,7 +73,7 @@ This module lets you connect to web services using SOAP. It also provides a ser
7273

7374
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
7475

75-
## Features:
76+
## Features
7677

7778
* Very simple API
7879
* Handles both RPC and Document schema types
@@ -1334,6 +1335,24 @@ var wsdlOptions = {
13341335

13351336
To see it in practice, have a look at the sample files in: [test/request-response-samples/addPets__force_namespaces](https://github.com/vpulim/node-soap/tree/master/test/request-response-samples/addPets__force_namespaces)
13361337

1338+
### Overriding element key specification in XML
1339+
1340+
In very rare cases ([external implementation isn't matching exactly the WSDL spec?](https://github.com/vpulim/node-soap/pull/1189)),
1341+
you may want to override element XML keys in requests and/or responses.
1342+
1343+
You can specify the key definitions by setting the `overrideElementKey` key in the `wsdlOptions` like so:
1344+
```javascript
1345+
var wsdlOptions = {
1346+
overrideElementKey: {
1347+
Nom: 'Name',
1348+
Commande: 'Order',
1349+
SillyResponse: 'DummyResponse'
1350+
};
1351+
};
1352+
```
1353+
1354+
Test sample files covering this are in [test/request-response-samples/Dummy__ref_element_should_have_correct_namespace_with_overrideElementKey](https://github.com/vpulim/node-soap/tree/master/test/request-response-samples/Dummy__ref_element_should_have_correct_namespace_with_overrideElementKey)
1355+
13371356
### Custom Deserializer
13381357

13391358
Sometimes it's useful to handle deserialization in your code instead of letting node-soap do it.

src/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ export class Client extends EventEmitter {
189189
}
190190
}
191191
}
192+
193+
if (options.overrideElementKey !== undefined) {
194+
this.wsdl.options.overrideElementKey = options.overrideElementKey;
195+
}
192196
if (options.overrideRootElement !== undefined) {
193197
this.wsdl.options.overrideRootElement = options.overrideRootElement;
194198
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export interface IWsdlBaseOptions {
9090
valueKey?: string;
9191
xmlKey?: string;
9292
overrideRootElement?: { namespace: string; xmlnsAttributes?: IXmlAttribute[]; };
93+
overrideElementKey?: object;
9394
ignoredNamespaces?: boolean | string[] | { namespaces?: string[]; override?: boolean; };
9495
ignoreBaseNameSpaces?: boolean;
9596
/** escape special XML characters in SOAP message (e.g. &, >, < etc), default: true. */

src/wsdl/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,17 @@ export class WSDL {
656656
public objectToXML(obj, name: string, nsPrefix: any, nsURI: string, isFirst?: boolean, xmlnsAttr?, schemaObject?, nsContext?: NamespaceContext) {
657657
const schema = this.definitions.schemas[nsURI];
658658

659+
if (this.options.overrideElementKey && Object.keys(this.options.overrideElementKey).length > 0) {
660+
for (const key in this.options.overrideElementKey) {
661+
const overrideKey = this.options.overrideElementKey[key];
662+
if (obj && obj[key]) {
663+
Object.defineProperty(obj, overrideKey,
664+
Object.getOwnPropertyDescriptor(obj, key));
665+
delete obj[key];
666+
}
667+
}
668+
}
669+
659670
let parentNsPrefix = nsPrefix ? nsPrefix.parent : undefined;
660671
if (typeof parentNsPrefix !== 'undefined') {
661672
// we got the parentNsPrefix for our array. setting the namespace-variable back to the current namespace string
@@ -1193,6 +1204,9 @@ export class WSDL {
11931204
this.options.forceSoap12Headers = options.forceSoap12Headers;
11941205
this.options.customDeserializer = options.customDeserializer;
11951206

1207+
if (options.overrideElementKey !== undefined) {
1208+
this.options.overrideElementKey = options.overrideElementKey;
1209+
}
11961210
if (options.overrideRootElement !== undefined) {
11971211
this.options.overrideRootElement = options.overrideRootElement;
11981212
}

test/request-response-samples-test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ tests.forEach(function(test){
119119
requestJSON = require(requestJSON);
120120

121121
//options is optional
122-
if (fs.existsSync(options))options = require(options);
122+
if (fs.existsSync(options)) options = require(options);
123123
else options = {};
124124

125125
//wsdlOptions is optional
@@ -150,12 +150,24 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ
150150
}
151151

152152
suite[name] = function(done){
153-
if(requestXML) requestContext.expectedRequest = requestXML;
153+
if (requestXML) {
154+
// Override the expect request's keys to match
155+
if (wsdlOptions.overrideElementKey) {
156+
requestXML = requestXML.replace(/:Commande/g, ':Order');
157+
requestXML = requestXML.replace(/:Nom/g, ':Name');
158+
}
159+
requestContext.expectedRequest = requestXML;
160+
}
161+
154162
if (responseXML) {
155163
if (wsdlOptions.parseReponseAttachments) {//all LF to CRLF
156164
responseXML = responseXML.replace(/\r\n/g, "\n");
157165
responseXML = responseXML.replace(/\n/g, "\r\n");
158166
}
167+
// Override the expect request's keys to match
168+
if (wsdlOptions.overrideElementKey) {
169+
responseXML = responseXML.replace(/SillyResponse/g, 'DummyResponse');
170+
}
159171
requestContext.responseToSend = responseXML;
160172
}
161173
requestContext.doneHandler = done;
@@ -171,7 +183,7 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ
171183
}
172184

173185
//throw more meaningful error
174-
if(typeof client[methodName] !== 'function'){
186+
if (typeof client[methodName] !== 'function'){
175187
throw new Error('method ' + methodName + ' does not exists in wsdl specified in test wsdl: ' + wsdlPath);
176188
}
177189

0 commit comments

Comments
 (0)