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

Commit

Permalink
feat: allow request with empty URLs (just skip)
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed May 27, 2021
1 parent 33322b9 commit 6e589d0
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ async function postmanToOpenApi (input, output, {
request: { url, method, body, description: rawDesc, header },
name: summary, tag = defaultTag, event: events
} = element
const { path, query, protocol, host, port } = scrapeURL(url)
domains.add(calculateDomains(protocol, host, port))
const joinedPath = calculatePath(path, pathDepth)
if (!paths[joinedPath]) paths[joinedPath] = {}
const { description, paramsMeta } = descriptionParse(rawDesc)
const { path, query, protocol, host, port, valid } = 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),
...parseParameters(query, header, joinedPath, paramsMeta),
responses: parseResponse(events)
paths[joinedPath][method.toLowerCase()] = {
tags: [tag],
summary,
...(description ? { description } : {}),
...parseBody(body, method),
...parseParameters(query, header, joinedPath, paramsMeta),
responses: parseResponse(events)
}
}
}

Expand Down Expand Up @@ -275,6 +277,9 @@ function calculateDomains (protocol, hosts, port) {

/** Support for collection V2 */
function scrapeURL (url) {
if (url === '' || url.raw === '') {
return { valid: false }
}
if (typeof url === 'string' || url instanceof String) {
const objUrl = new URL(url)
return {
Expand All @@ -283,10 +288,11 @@ function scrapeURL (url) {
query: [],
protocol: objUrl.protocol.slice(0, -1),
host: decodeURIComponent(objUrl.hostname).split('.'),
port: objUrl.port
port: objUrl.port,
valid: true
}
}
return url
return { ...url, valid: true }
}

/* Parse domains from operations or options */
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const EXPECTED_URL_WITH_PORT = readFileSync('./test/resources/output/UrlWithPort
const EXPECTED_EXTERNAL_DOCS = readFileSync('./test/resources/output/ExternalDocs.yml', 'utf8')
const EXPECTED_EXTERNAL_DOCS_OPTS = readFileSync('./test/resources/output/ExternalDocsOpts.yml', 'utf8')
const EXPECTED_EXTERNAL_DOCS_OPTS_PARTIAL = readFileSync('./test/resources/output/ExternalDocsOptsPartial.yml', 'utf8')
const EXPECTED_EMPTY_URL = readFileSync('./test/resources/output/EmptyUrl.yml', 'utf8')

describe('Library specs', function () {
afterEach('remove file', function () {
Expand Down Expand Up @@ -68,6 +69,7 @@ describe('Library specs', function () {
const COLLECTION_AUTH_BASIC = `./test/resources/input/${version}/AuthBasic.json`
const COLLECTION_URL_WITH_PORT = `./test/resources/input/${version}/UrlWithPort.json`
const COLLECTION_EXTERNAL_DOCS = `./test/resources/input/${version}/ExternalDocs.json`
const COLLECTION_EMPTY_URL = `./test/resources/input/${version}/EmptyUrl.json`

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
Expand Down Expand Up @@ -312,6 +314,11 @@ describe('Library specs', function () {
})
equal(result, EXPECTED_EXTERNAL_DOCS_OPTS_PARTIAL)
})

it('should not transform empty url request', async function () {
const result = await postmanToOpenApi(COLLECTION_EMPTY_URL, OUTPUT_PATH)
equal(result, EXPECTED_EMPTY_URL)
})
})
})

Expand Down
54 changes: 54 additions & 0 deletions test/resources/input/v2/EmptyUrl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"info": {
"_postman_id": "e223ab3f-2683-4759-9e41-8167a7caaf99",
"name": "Simple Post",
"description": "Just a simple collection for test",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "https://api.io/users",
"description": "Create a new user into your amazing API"
},
"response": []
},
{
"name": "Post empty raw",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "",
"description": "Create a new user into your amazing API"
},
"response": []
}
],
"variable": [
{
"key": "version",
"value": "2.3.0"
}
]
}
66 changes: 66 additions & 0 deletions test/resources/input/v21/EmptyUrl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"info": {
"_postman_id": "e223ab3f-2683-4759-9e41-8167a7caaf99",
"name": "Simple Post",
"description": "Just a simple collection for test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.io/users",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
]
},
"description": "Create a new user into your amazing API"
},
"response": []
},
{
"name": "Post empty raw",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": ""
},
"description": "Create a new user into your amazing API"
},
"response": []
}
],
"variable": [
{
"key": "version",
"value": "2.3.0"
}
]
}
29 changes: 29 additions & 0 deletions test/resources/output/EmptyUrl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: 3.0.0
info:
title: Simple Post
description: Just a simple collection for test
version: 2.3.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: {}

0 comments on commit 6e589d0

Please sign in to comment.