API Specification format conversion

Bharath Kesavan
2 min readSep 24, 2022

--

This article is about converting Open API Specification (OAS) to RAML. Convert your API Specification from swagger to RAML and vice versa. This is achieved using the npm package webapi-parser. Prerequisite for this is to know basics of node js. This parser will read the input specification file and convert it into mentioned format.

Sample Use Case:

  1. Migration from other API Gateways

2. Microservices which only consume swagger — E.g. Test Script Generator, Pipeline Verify

Install the node package

$ npm install webapi-parser

Steps

Listed below are the generic steps used to convert OAS to RAML and vice versa

  1. Parse the input API Specification
  2. Resolve the parsed model — including references into the spec
  3. Generate file in required format

Convert OAS to RAML

Import the “webapi-parser”. Define the input and the output location.

In the code shown, the parse and resolve is executed with “wap.oas30”. This is used when the input OAS is of version 3.0.0. Use this when swagger file is defined as “openapi”: “3.0.0” in the starting. If you are using version 2.0, then “wap.oas20” should be used to parse and resolve the swagger.

Run the code with command “node filename.js” and RAML will be generated in the path mentioned.

const wap = require('webapi-parser').WebApiParseconst path = require('path')
async function main () {try {//importing file const inPath = path.join(__dirname, '../js/input.json')const outPath= path.join(__dirname, '../js/generated.raml')const docPath = `file://${inPath}`
//format convertion using the 3 steps mentionedconst apiModel = await wap.oas30.parse(docPath)const resApiModel = await wap.oas30.resolve(apiModel)const file = await wap.raml10.generateFile(resApiModel, `file://${outPath}`)
console.log('generated')} catch (error) {console.error(error);}}
main()

Convert RAML to OAS

Similarly when converting into/ out of RAML format, use “wap.raml10”. If you are using the previous version of RAML then use “wap.raml08”.

const apiModel = await wap.raml10.parse(docPath)const resApiModel = await wap.raml10.resolve(apiModel)const file = await wap.oas30.generateFile(resApiModel, `file://${outPath}`)

Further Reading:

--

--