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

Commit 9e459f1

Browse files
committed
feat: POST body uses as example in openapi
1 parent a4cc2c4 commit 9e459f1

6 files changed

Lines changed: 55 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,3 @@
1616
### Reverts
1717

1818
* Revert "Testing commit lint" ([bd7d8c5](https://github.com/joolfe/postman-to-openapi/commit/bd7d8c5d42c83a0ba76907aeb6315461db763b34))
19-
20-
21-

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Or in other words, transform [this specification](https://schema.getpostman.com/
99
- Postman Collection v2.1
1010
- OpenApi 3.0
1111

12+
- POST request with JSON body.
13+
- Allow extract the api version from a collection general `variable`.
14+
1215
## Development
1316

1417
This project use for development:

lib/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ async function postmanToOpenApi (input, output, { save = true }) {
77
// TODO validate?
88
const collectionFile = await readFile(input)
99
const postmanJson = JSON.parse(collectionFile)
10-
const { item: items, info: { name: title, description } } = postmanJson
10+
const { item: items, info: { name: title, description }, variable } = postmanJson
11+
const version = getVarValue(variable, 'version', '1.0.0')
1112
const paths = {}
1213

1314
for (const item of items) {
14-
const { request: { url: { path }, method, body }, name: summary } = item
15+
const { request: { url: { path }, method, body, description }, name: summary } = item
1516
const compiledPath = '/' + path.join('/')
1617
if (!paths[compiledPath]) paths[compiledPath] = {}
1718
paths[compiledPath][method.toLowerCase()] = {
1819
summary,
20+
...(description ? { description } : {}),
1921
requestBody: parseBody(body),
2022
responses: {
2123
200: {
@@ -33,7 +35,7 @@ async function postmanToOpenApi (input, output, { save = true }) {
3335
info: {
3436
title,
3537
description,
36-
version: '1.0.0'
38+
version
3739
},
3840
paths
3941
}
@@ -66,4 +68,9 @@ function parseBody (body) {
6668
}
6769
}
6870

71+
function getVarValue (variables, name, def) {
72+
const variable = variables.find(({ key }) => key === name)
73+
return variable ? variable.value : def
74+
}
75+
6976
module.exports = postmanToOpenApi

test/index.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const OUTPUT_PATH = path.join(__dirname, '/openAPIRes.yml')
1111
const COLLECTION_BASIC = path.join(__dirname, '/resources/input/PostmantoOpenAPI.postman_collection.json')
1212
const EXPECTED_BASIC = readFileSync(path.join(__dirname, './resources/output/Basic.yml'), 'utf8')
1313

14-
describe('First test', function () {
14+
describe('Library specs', function () {
1515
afterEach('remove file', function () {
1616
if (existsSync(OUTPUT_PATH)) {
1717
unlinkSync(OUTPUT_PATH)
@@ -20,7 +20,6 @@ describe('First test', function () {
2020

2121
it('should work with a basic transform', async function () {
2222
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
23-
2423
equal(EXPECTED_BASIC, result.slice(0, -1))
2524
ok(existsSync(OUTPUT_PATH))
2625
})

test/resources/input/PostmantoOpenAPI.postman_collection.json

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"item": [
99
{
10-
"name": "Post example",
10+
"name": "Create a user",
1111
"request": {
1212
"method": "POST",
1313
"header": [],
@@ -21,22 +21,22 @@
2121
}
2222
},
2323
"url": {
24-
"raw": "https://www.google.es/hola",
24+
"raw": "https://api.io/users",
2525
"protocol": "https",
2626
"host": [
27-
"www",
28-
"google",
29-
"es"
27+
"api",
28+
"io"
3029
],
3130
"path": [
32-
"hola"
31+
"users"
3332
]
34-
}
33+
},
34+
"description": "This is a post request with json body"
3535
},
3636
"response": []
3737
},
3838
{
39-
"name": "Post example 2",
39+
"name": "Create a post",
4040
"request": {
4141
"method": "POST",
4242
"header": [],
@@ -50,15 +50,14 @@
5050
}
5151
},
5252
"url": {
53-
"raw": "https://www.google.es/hola",
53+
"raw": "https://api.io/posts",
5454
"protocol": "https",
5555
"host": [
56-
"www",
57-
"google",
58-
"es"
56+
"api",
57+
"io"
5958
],
6059
"path": [
61-
"hola"
60+
"posts"
6261
]
6362
}
6463
},
@@ -87,5 +86,12 @@
8786
}
8887
}
8988
],
89+
"variable": [
90+
{
91+
"id": "cfcda50b-827a-4a48-8059-1344d044cba7",
92+
"key": "version",
93+
"value": "1.1.0"
94+
}
95+
],
9096
"protocolProfileBehavior": {}
9197
}

test/resources/output/Basic.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@ openapi: 3.0.0
22
info:
33
title: Postman to OpenAPI
44
description: 'Mi super test collection from postman '
5-
version: 1.0.0
5+
version: 1.1.0
66
paths:
7-
/hola:
7+
/users:
88
post:
9-
summary: Post example 2
9+
summary: Create a user
10+
description: This is a post request with json body
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
type: object
16+
example:
17+
example: field
18+
other:
19+
data1: 'yes'
20+
data2: 'no'
21+
responses:
22+
'200':
23+
description: Successful response
24+
content:
25+
application/json: {}
26+
/posts:
27+
post:
28+
summary: Create a post
1029
requestBody:
1130
content:
1231
text/plain: {}

0 commit comments

Comments
 (0)