Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #213 from joolfe/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joolfe authored Aug 29, 2022
2 parents 09a3f26 + 99c8d6a commit 490d2cc
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 8 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## [2.6.0](https://github.com/joolfe/postman-to-openapi/compare/2.5.0...2.6.0) (2022-08-29)


### Features

* add JSON format support ([c16a09f](https://github.com/joolfe/postman-to-openapi/commit/c16a09ff3acdfebdef44bffff4aef6f57afde447))


### Documentation

* document new option ([c0a37ae](https://github.com/joolfe/postman-to-openapi/commit/c0a37aef1c74fa9e0db78e540c38c3cffa544aed))


### Build System

* lock file ([2813792](https://github.com/joolfe/postman-to-openapi/commit/2813792f90b60001a97bba76e1f8337fcaa4419d))
* update version and ts devs ([7aea545](https://github.com/joolfe/postman-to-openapi/commit/7aea5453d1bc938d45d10331e487674b92514432))

## [2.5.0](https://github.com/joolfe/postman-to-openapi/compare/2.4.2...2.5.0) (2022-08-28)


Expand Down
7 changes: 7 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ The third parameter used in the library method is an `options` object containing
| [responseHeaders](#responseheaders-boolean) | Indicate if should parse the response headers from the collection examples. |
| [replaceVars](#replacevars-boolean) | Boolean value to indicate if postman variables should be replaced.|
| [additionalVars](#additionalvars-object) | Object to provide additional values for variables replacement.|
| [outputFormat](#outputformat-string) | Indicate the format of the output document. |

### info (Object)

Expand Down Expand Up @@ -346,6 +347,12 @@ This parameter is a json Object that contain as key the variable name and as val

Take into account that variable values provided in the `additionalVars` Object supersede those defined at Postman collection level.

### outputFormat (string)

Indicates the resulting format of the OpenAPI document between `json` and `yaml`, the resulting file will be writte using this format and also the result value fo the method `postmanToOpenApi(...)` will use this format.

Default value is `yaml`, if you use a unknown value `yaml` will be used.

# Features

## Basic conversion
Expand Down
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const jsonc = require('jsonc-parser')
async function postmanToOpenApi (input, output, {
info = {}, defaultTag = 'default', pathDepth = 0,
auth: optsAuth, servers, externalDocs = {}, folders = {},
responseHeaders = true, replaceVars = false, additionalVars = {}
responseHeaders = true, replaceVars = false, additionalVars = {}, outputFormat = 'yaml'
} = {}) {
// TODO validate?
let collectionFile = await resolveInput(input)
Expand Down Expand Up @@ -66,11 +66,11 @@ async function postmanToOpenApi (input, output, {
...parseTags(tags),
paths
}
const openApiYml = dump(openApi, { skipInvalid: true })
const openApiDoc = outputFormat === 'json' ? JSON.stringify(openApi, null, 4) : dump(openApi, { skipInvalid: true })
if (output != null) {
await writeFile(output, openApiYml, 'utf8')
await writeFile(output, openApiDoc, 'utf8')
}
return openApiYml
return openApiDoc
}

/* Calculate the tags for folders items based on the options */
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-to-openapi",
"version": "2.5.0",
"version": "2.6.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.json'
const COLLECTION_NULL_HEADERS = './test/resources/input/NullHeaders.json'

const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const EXPECTED_BASIC_JSON = readFileSync('./test/resources/output/Basic.json', 'utf8')
const EXPECTED_BASIC_NO_OPTS = readFileSync('./test/resources/output/BasicNoOptions.yml', 'utf8')
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
Expand Down Expand Up @@ -491,6 +492,11 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_JSON_COMMENTS, OUTPUT_PATH, { pathDepth: 2 })
equal(result, EXPECTED_COLLECTION_JSON_COMMENTS)
})

it('should return "json" format is requested', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, { outputFormat: 'json' })
equal(result, EXPECTED_BASIC_JSON)
})
})
})

Expand Down
96 changes: 96 additions & 0 deletions test/resources/output/Basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"openapi": "3.0.0",
"info": {
"title": "Postman to OpenAPI",
"description": "Mi super test collection from postman",
"version": "1.1.0"
},
"servers": [
{
"url": "https://api.io"
}
],
"paths": {
"/users": {
"post": {
"tags": [
"default"
],
"summary": "Create new User",
"description": "Create a new user into your amazing API",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"example": "field",
"other": {
"data1": "yes",
"data2": "no"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {}
}
}
}
}
},
"/posts": {
"post": {
"tags": [
"default"
],
"summary": "Create a post",
"requestBody": {
"content": {
"text/plain": {}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {}
}
}
}
}
},
"/note": {
"post": {
"tags": [
"default"
],
"summary": "Create a note",
"description": "Just an example of text raw body",
"requestBody": {
"content": {
"text/plain": {
"schema": {
"type": "string",
"example": "This is an example Note"
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {}
}
}
}
}
}
}
}
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export interface Options {
responseHeaders?: boolean,
// Default value false
replaceVars?: boolean,
additionalVars?: { [key: string]: string }
additionalVars?: { [key: string]: string },
// Default value 'yaml'
outputFormat?: 'json' | 'yaml'
}

export default function postmanToOpenApi (input: string, output?: string, options?: Options) : Promise<string>

0 comments on commit 490d2cc

Please sign in to comment.