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

Commit 96d02a7

Browse files
committed
feat: Use desc from folders for tags #53
Closes #53
1 parent 0771a2e commit 96d02a7

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ Have a look to the [SimplePost collection](https://github.com/joolfe/postman-to-
193193

194194
In postman you can add [folders](https://learning.postman.com/docs/sending-requests/intro-to-collections/) inside your collection to group requests and keep the collection clean, in OpenAPI there are no folders but exist the concept of [tags](https://swagger.io/specification/#tag-object) that has the same approximate meaning, this library automatically detect folders and use the name of the folder as tag name in the transformation. Right now is not possible to have more than one tag value for each operation.
195195

196+
As part of the implementation we now support `description` for [tags](https://swagger.io/specification/#tag-object), just add a description into the Postman Collection folder and automatically the `tags` section will be filled in the he OpenApi spec.
197+
196198
Have a look to the [FolderCollection](https://github.com/joolfe/postman-to-openapi/blob/master/test/resources/input/FolderCollection.json) file for an example of how to use this feature.
197199

198200
## Parameters parsing

lib/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'defau
1010
const { item: items } = postmanJson
1111
const paths = {}
1212
const domains = new Set()
13+
const tags = {}
1314

1415
for (let [i, element] of items.entries()) {
15-
const { item, name } = element
16+
const { item, name, description: tagDesc } = element
1617
if (item != null) { // is a folder
1718
const tagged = item.map(e => ({ ...e, tag: name }))
19+
tags[name] = tagDesc
1820
items.splice(i, 1, ...tagged)
1921
element = tagged.shift()
2022
}
@@ -47,6 +49,7 @@ async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'defau
4749
info: compileInfo(postmanJson, info),
4850
...parseServers(domains, servers),
4951
...parseAuth(postmanJson, auth),
52+
...parseTags(tags),
5053
paths
5154
}
5255

@@ -254,4 +257,11 @@ function parseServers (domains, serversOpts) {
254257
return (servers.length > 0) ? { servers } : {}
255258
}
256259

260+
/* Transform a object of tags in an array of tags */
261+
function parseTags (tagsObj) {
262+
const tags = Object.entries(tagsObj)
263+
.map(([name, description]) => ({ name, description }))
264+
return (tags.length > 0) ? { tags } : {}
265+
}
266+
257267
module.exports = postmanToOpenApi

test/resources/input/FolderCollection.json

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info": {
3-
"_postman_id": "3ad7e1b4-f3a1-4a8f-8bd0-56ff18f53339",
3+
"_postman_id": "769a0782-149c-40f1-9532-74e2c116bb04",
44
"name": "Folder Collection",
55
"description": "Just a simple collection for test",
66
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
@@ -68,6 +68,29 @@
6868
"response": []
6969
}
7070
],
71+
"description": "Operations at User level",
72+
"event": [
73+
{
74+
"listen": "prerequest",
75+
"script": {
76+
"id": "c69515db-ca18-4c49-b50f-421608912d3c",
77+
"type": "text/javascript",
78+
"exec": [
79+
""
80+
]
81+
}
82+
},
83+
{
84+
"listen": "test",
85+
"script": {
86+
"id": "d69a3a2c-1074-4318-9768-fdc958e03038",
87+
"type": "text/javascript",
88+
"exec": [
89+
""
90+
]
91+
}
92+
}
93+
],
7194
"protocolProfileBehavior": {}
7295
},
7396
{
@@ -102,6 +125,29 @@
102125
"response": []
103126
}
104127
],
128+
"description": "Operations for Post items",
129+
"event": [
130+
{
131+
"listen": "prerequest",
132+
"script": {
133+
"id": "5d11f381-af50-45b2-8614-6a5bccfdb6d6",
134+
"type": "text/javascript",
135+
"exec": [
136+
""
137+
]
138+
}
139+
},
140+
{
141+
"listen": "test",
142+
"script": {
143+
"id": "7eed3e03-a385-45c2-9979-eeeba859623b",
144+
"type": "text/javascript",
145+
"exec": [
146+
""
147+
]
148+
}
149+
}
150+
],
105151
"protocolProfileBehavior": {}
106152
},
107153
{

test/resources/output/Folders.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ info:
55
version: 2.3.0
66
servers:
77
- url: 'https://api.io'
8+
tags:
9+
- name: Users
10+
description: Operations at User level
11+
- name: Posts
12+
description: Operations for Post items
813
paths:
914
/users:
1015
post:

0 commit comments

Comments
 (0)