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

Commit

Permalink
fix: close #216, an error when there exist an empty folder at the end…
Browse files Browse the repository at this point in the history
… of the collection
  • Loading branch information
joolfe committed Sep 17, 2022
1 parent 490d2cc commit 4d7424c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
41 changes: 22 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function postmanToOpenApi (input, output, {
const securitySchemes = {}

for (let [i, element] of items.entries()) {
while (element.item != null) { // is a folder
while (element != null && element.item != null) { // is a folder
const { item, description: tagDesc } = element
const tag = calculateFolderTag(element, folders)
const tagged = item.map(e => ({ ...e, tag }))
Expand All @@ -35,24 +35,27 @@ async function postmanToOpenApi (input, output, {
// Empty folders will have tagged empty
element = (tagged.length > 0) ? tagged.shift() : items[i]
}
const {
request: { url, method, body, description: rawDesc, header, auth },
name: summary, tag = defaultTag, event: events, response
} = element
const { path, query, protocol, host, port, valid, pathVars } = scrapeURL(url)
if (valid) {
domains.add(calculateDomains(protocol, host, port))
const joinedPath = calculatePath(path, pathDepth)
if (!paths[joinedPath]) paths[joinedPath] = {}
const { description, paramsMeta } = descriptionParse(rawDesc)
paths[joinedPath][method.toLowerCase()] = {
tags: [tag],
summary,
...(description ? { description } : {}),
...parseBody(body, method),
...parseOperationAuth(auth, securitySchemes, optsAuth),
...parseParameters(query, header, joinedPath, paramsMeta, pathVars),
...parseResponse(response, events, responseHeaders)
// If there are an empty folder at the end of the colection elements could be `undefined`
if (element != null) {
const {
request: { url, method, body, description: rawDesc, header, auth },
name: summary, tag = defaultTag, event: events, response
} = element
const { path, query, protocol, host, port, valid, pathVars } = scrapeURL(url)
if (valid) {
domains.add(calculateDomains(protocol, host, port))
const joinedPath = calculatePath(path, pathDepth)
if (!paths[joinedPath]) paths[joinedPath] = {}
const { description, paramsMeta } = descriptionParse(rawDesc)
paths[joinedPath][method.toLowerCase()] = {
tags: [tag],
summary,
...(description ? { description } : {}),
...parseBody(body, method),
...parseOperationAuth(auth, securitySchemes, optsAuth),
...parseParameters(query, header, joinedPath, paramsMeta, pathVars),
...parseResponse(response, events, responseHeaders)
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/resources/input/v2/FolderCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@
"url": "https://api.io/info"
},
"response": []
},
{
"name": "Empty Folder 2",
"item": [],
"protocolProfileBehavior": {}
}
],
"variable": [
Expand Down
5 changes: 5 additions & 0 deletions test/resources/input/v21/FolderCollection.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@
}
},
"response": []
},
{
"name": "Empty Folder 2",
"item": [],
"protocolProfileBehavior": {}
}
],
"variable": [
Expand Down
1 change: 1 addition & 0 deletions test/resources/output/Folders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tags:
- name: Folder 1 > Folder 2 > Folder 3 > Folder 4 > Folder 5
description: Folder 5 description
- name: Empty Folder
- name: Empty Folder 2
paths:
/users/admin/roles:
get:
Expand Down
1 change: 1 addition & 0 deletions test/resources/output/FoldersNoConcat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tags:
- name: Folder 5
description: Folder 5 description
- name: Empty Folder
- name: Empty Folder 2
paths:
/users/admin/roles:
get:
Expand Down
1 change: 1 addition & 0 deletions test/resources/output/FoldersSeparator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tags:
- name: Folder 1-Folder 2-Folder 3-Folder 4-Folder 5
description: Folder 5 description
- name: Empty Folder
- name: Empty Folder 2
paths:
/users/admin/roles:
get:
Expand Down

0 comments on commit 4d7424c

Please sign in to comment.