Generate swagger yaml file from WSDL

This tutorial shows how to generate swagger yaml file from the WSDL file using node.js. This uses the following node modules to generate the yaml file from the WSDL file

  • apiconnect-wsdl

  • js-yaml

  • file-system

Steps

1) Install node.js and npm

If you do not have node.js and npm installed, install Node.js and npm on your development environment. Refer to the post for the installation instructions

2) Install the following Node modules

You need to install all the below modules in the same directory. I will use c:\kalliphant directory in my examples below as a working directory.
Before going to next step change directory to your working directory

c:>
c:> cd c:\kalliphant
c:\kalliphant>
c:\kalliphant>

2.1) Install apiconnect-wsdl module

This is a public module to parse the WSDL. We will use this to parse the WSDL files and get all the services defined in the WSDLs.
Please refer to apiconnect-wsdl documentation for more detail.

To install this module, run the following command at the command prompt in your working directory

c:\kalliphant>
c:\kalliphant>
c:\kalliphant> npm install apiconnect-wsdl
     : 
     :
c:\kalliphant>

You may get some warning during install, ignore them.

2.2) Install js-yaml module

This is a public module, this takes javascript serialized object as input and produces the document (and vice versa). Please refer to js-yaml documentation for more detail.

To install this module, run the following command at the command prompt in your working directory

c:\kalliphant>
c:\kalliphant>
c:\kalliphant> npm install js-yaml
     : 
     :
c:\kalliphant>

You may get some warning during install, ignore them.

2.3) Install file-system module

This is a public module for file operations. This is mainly to print the yaml output to a file instead of standard output. Please refer to file-system documentation for more detail.

To install this module, run the following command at the windows command prompt in your working directory

c:\kalliphant>
c:\kalliphant>
c:\kalliphant> npm install file-system
     : 
     :
c:\kalliphant>

You may get some warning during install, ignore them.

3) Test Modules install

To check if you have installed all the above modules correctly, run the below "npm list --depth=0" command, it should show all the installed modules as below.

C:\kalliphant>
C:\kalliphant> npm list --depth=0
C:\kalliphant
+-- apiconnect-wsdl@1.3.22
+-- file-system@2.2.2
`-- js-yaml@3.8.1

C:\kalliphant>

4) WSDL to Swagger YAML javascript

Following javascript reads the WSDL passed and generate the yaml file using the modules installed above. Create the below script in your working directory where you installed above npm modules.

Below example (at line 5) is using the URL that is pointing to a WSDL it can be a
* standalone WSDL file or
* ZIP file containing a complete set of WSDLs and dependent XSDs or
* URL that can point to either a WSDL or ZIP file.


var apiconnWsdl = require("apiconnect-wsdl");
var yaml 	    = require("js-yaml");
var fs          = require("fs");

var promise = apiconnWsdl.getJsonForWSDL("http://www.webservicex.com/globalweather.asmx?WSDL");

promise.then(function(wsdls){
	
	// Get Services from all parsed WSDLs
	var serviceData = apiconnWsdl.getWSDLServices(wsdls);

	// Loop through all services and genereate yaml file
	for (var  item in serviceData.services) {
		var serviceName = serviceData.services[item].service;
		var wsdlId = serviceData.services[item].filename;
		var wsdlEntry = apiconnWsdl.findWSDLForServiceName(wsdls, serviceName);
		var swagger = apiconnWsdl.getSwaggerForService(wsdlEntry, serviceName, wsdlId);
		fs.writeFile(serviceName+".yaml", yaml.safeDump(swagger))
	}
}, function (error) {
	console.log(error.message)
});

5) Run the script

If you run the script, it will read the WSDL from the given URL and generates the <serviceName>.yaml file

You should see output like this

c:\kalliphant>
c:\kalliphant>node WSDL2SwaggerYAML.js
Generating GlobalWeather.yaml file

c:\kalliphant>

Import the generated yaml file to swagger UI, it should look like this below.

2 Comments

Comments are closed.