Skip to content

Conversation

@smokhov
Copy link
Contributor

@smokhov smokhov commented Aug 15, 2025

This PR supersedes PR #1189 (by @Lucas-Mondini) by sync'ing with master, fixing linting errors, adding a test, and updating README. The test works for both request's and response's XML element keys. I've also fixed a typo in the dir name in one of the tests I perused to make a test for this.

I've done a number of instrumentation and testing while doing this, which I have removed from the actual PR, but I am pasting them below if that should be useful for checking:

diff --git a/src/types.ts b/src/types.ts
index dde5f7c..5660ba8 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -91,6 +91,18 @@ export interface IWsdlBaseOptions {
   xmlKey?: string;
   overrideRootElement?: { namespace: string; xmlnsAttributes?: IXmlAttribute[]; };
   overrideElementKey?: object;
+/*
+  overrideElementKey?: {
+    namespace: 'xmlns:TNS',
+    xmlnsAttributes: [{
+      name: 'xmlns:NS2',
+      value: "http://tempuri.org/"
+    }, {
+      name: 'xmlns:NS3',
+      value: "http://sillypets.com/xsd"
+    }]
+  };
+*/
   ignoredNamespaces?: boolean | string[] | { namespaces?: string[]; override?: boolean; };
   ignoreBaseNameSpaces?: boolean;
   /** escape special XML characters in SOAP message (e.g. &, >, < etc), default: true. */
diff --git a/src/wsdl/index.ts b/src/wsdl/index.ts
index c43f023..be88d03 100644
--- a/src/wsdl/index.ts
+++ b/src/wsdl/index.ts
@@ -656,10 +656,34 @@ export class WSDL {
   public objectToXML(obj, name: string, nsPrefix: any, nsURI: string, isFirst?: boolean, xmlnsAttr?, schemaObject?, nsContext?: NamespaceContext) {
     const schema = this.definitions.schemas[nsURI];
 
+/*
+    this.options.overrideElementKey =
+    {
+        address: 'ADRESSE',
+        companyName: 'nomeDeLaCOMPANIE',
+        DummyRequest: 'SillyRequest'
+    };
+
+    console.log("this.options.overrideElementKey");
+    console.log(this.options.overrideElementKey);
+    console.log("Object.keys(this.options.overrideElementKey)");
+    console.log(Object.keys(this.options.overrideElementKey));
+*/
+
     if (this.options.overrideElementKey && Object.keys(this.options.overrideElementKey).length > 0) {
       for (const key in this.options.overrideElementKey) {
         const overrideKey = this.options.overrideElementKey[key];
+///*
+        console.log("KEY!!!!!");
+        console.log(key);
+        console.log("OVERRIDE KEY!!!!!");
+        console.log(overrideKey);
+        console.log("OBJ!!!!!");
+        console.log(obj);
+//*/
         if (obj && obj[key]) {
+          console.log("OBJ[KEY BEFORE]!!!!!");
+          console.log(obj[key]);
           Object.defineProperty(obj, overrideKey,
               Object.getOwnPropertyDescriptor(obj, key));
           delete obj[key];
@@ -667,6 +691,8 @@ export class WSDL {
       }
     }
 
+        console.log("OBJ222222222222222222222!!!!!");
+        console.log(obj);
     let parentNsPrefix = nsPrefix ? nsPrefix.parent : undefined;
     if (typeof parentNsPrefix !== 'undefined') {
       // we got the parentNsPrefix for our array. setting the namespace-variable back to the current namespace string
@@ -736,6 +762,8 @@ export class WSDL {
 
       for (i = 0, n = obj.length; i < n; i++) {
         const item = obj[i];
+          console.log("OBJ[KEY AFTER]!!!!!");
+          console.log(obj[i]);
         const arrayAttr = this.processAttributes(item, nsContext);
         const correctOuterNsPrefix = nonSubNameSpace || parentNsPrefix || ns; // using the parent namespace prefix if given
 
diff --git a/test/request-response-samples-test.js b/test/request-response-samples-test.js
index a55da22..62c5115 100644
--- a/test/request-response-samples-test.js
+++ b/test/request-response-samples-test.js
@@ -119,7 +119,7 @@ tests.forEach(function(test){
   requestJSON = require(requestJSON);
 
   //options is optional
-  if (fs.existsSync(options))options = require(options);
+  if (fs.existsSync(options)) options = require(options);
   else options = {};
 
   //wsdlOptions is optional
@@ -136,8 +136,55 @@ tests.forEach(function(test){
   if(fs.existsSync(attachmentParts))  attachmentParts = require(attachmentParts)
   else attachmentParts = null;
 
+/*
+  wsdlOptions.overrideElementKey = null;
+  //wsdlOptions.overrideElementKey = {}
+  if (requestXML) {
+    if (requestXML.match('SillyRequest')) {
+      wsdlOptions.overrideElementKey = {};
+      requestXML = requestXML.replace(/SillyRequest/g, 'DummyRequest');
+    }
+  }
+*/
+
   generateTest(name, methodName, wsdl, headerJSON, securityJSON, requestXML, requestJSON, responseXML, responseJSON, responseSoapHeaderJSON, wsdlOptions, options, responseHttpHeaders, attachmentParts, false);
   generateTest(name, methodName, wsdl, headerJSON, securityJSON, requestXML, requestJSON, responseXML, responseJSON, responseSoapHeaderJSON, wsdlOptions, options, responseHttpHeaders, attachmentParts, true);
+/*
+  // Generate additional tests where DummyRequest is used for overrideElementKey
+  if (requestXML) {
+    //if (requestXML.match('DummyRequest')) {
+    //if (requestXML.match('address') || requestXML.match(':DummyField1')) {
+    if (requestXML.match('address') || requestXML.match('Order')) {
+      //var overrideWsdlOptions = wsdlOptions;
+      //overrideWsdlOptions.overrideElementKey = { DummyRequest: 'SillyRequest' };
+      //wsdlOptions.overrideElementKey = { DummyRequest: 'SillyRequest' };
+      //wsdlOptions.overrideElementKey = { address: 'adresse' };
+      //wsdlOptions.overrideElementKey = { address: 'adresse', DummyField1: 'DumboField1' };
+      wsdlOptions.overrideElementKey = { address: 'adresse', Order: 'Commande' };
+      options.ignoreBaseNameSpaces = false;
+
+      //const overrideRequestXML = requestXML.replace(/DummyRequest/g, 'SillyRequest');
+      //requestXML = requestXML.replace(/DummyRequest/g, 'SillyRequest');
+      //requestXML = requestXML.replace(/:DummyField/g, ':DumboField');
+      requestXML = requestXML.replace(/:Order/g, ':Commande');
+
+      generateTest(name + ' (overrideElementKey)', methodName, wsdl, headerJSON, securityJSON,
+        //overrideRequestXML, requestJSON, responseXML, responseJSON, responseSoapHeaderJSON,
+        requestXML, requestJSON, responseXML, responseJSON, responseSoapHeaderJSON,
+        wsdlOptions, options, responseHttpHeaders, attachmentParts, false);
+      //wsdlOptions.overrideElementKey = null;
+      //wsdlOptions.overrideElementKey = {};
+      //requestXML = requestXML.replace(/SillyRequest/g, 'DummyRequest');
+    }
+    
+    //if (requestXML.match('SillyRequest')) {
+    //  wsdlOptions.overrideElementKey = {};
+    //  requestXML = requestXML.replace(/SillyRequest/g, 'DummyRequest');
+    //}
+    
+  }
+*/
+
 });
 
 function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requestXML, requestJSON, responseXML, responseJSON, responseSoapHeaderJSON, wsdlOptions, options, responseHttpHeaders, attachmentParts, usePromises){
@@ -150,12 +197,27 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ
   }
 
   suite[name] = function(done){
-    if(requestXML) requestContext.expectedRequest = requestXML;
+    console.log(wsdlOptions);
+
+    if (requestXML) {
+      // Override the expect request's keys to match
+      if (wsdlOptions.overrideElementKey) {
+         requestXML = requestXML.replace(/:Commande/g, ':Order');
+         requestXML = requestXML.replace(/:Nom/g, ':Name');
+      } 
+      requestContext.expectedRequest = requestXML;
+      console.log(requestXML);
+    }
+
     if (responseXML) {
       if (wsdlOptions.parseReponseAttachments) {//all LF to CRLF
           responseXML = responseXML.replace(/\r\n/g, "\n");
           responseXML = responseXML.replace(/\n/g, "\r\n");
       }
+      // Override the expect request's keys to match
+      if (wsdlOptions.overrideElementKey) {
+         responseXML = responseXML.replace(/SillyResponse/g, 'DummyResponse');
+      } 
       requestContext.responseToSend = responseXML;
     }
     requestContext.doneHandler = done;
@@ -171,7 +233,7 @@ function generateTest(name, methodName, wsdlPath, headerJSON, securityJSON, requ
       }
 
       //throw more meaningful error
-      if(typeof client[methodName] !== 'function'){
+      if (typeof client[methodName] !== 'function'){
         throw new Error('method ' + methodName + ' does not exists in wsdl specified in test wsdl: ' + wsdlPath);
       }
 

@w666
Copy link
Collaborator

w666 commented Aug 17, 2025

I will try to allocate time for this next week.

@w666 w666 self-requested a review August 17, 2025 08:43
@w666 w666 merged commit b17b7f3 into vpulim:master Aug 28, 2025
1 check passed
smokhov added a commit to smokhov/node-soap that referenced this pull request Sep 1, 2025
…#1334)

* Update readme

---------

Co-authored-by: Lucas Fellipe Mondini Pereira <lucasfepereira1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants