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

Commit

Permalink
feat: adding support for formdata
Browse files Browse the repository at this point in the history
  • Loading branch information
devNoiseConsulting committed Jul 24, 2021
1 parent 8289a98 commit 547b137
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ function parseBody (body = {}, method) {
'text/plain': {}
}
break
case "formdata":
content = {
"multipart/form-data": {
schema: {
type: "object",
properties: body.formdata.reduce((acc, v) => {
acc[v.key] = { type: v.type === 'text' ? 'string' : v.type };
if (v.hasOwnProperty("description")) {
acc[v.key].description = v.description == '' ? 'Description' : v.description;
}
if (v.hasOwnProperty("value") && v.value !== '') {
acc[v.key].example = v.value;
};
return acc;
}, {})
}
}
};
break;
}
return { requestBody: { content } }
}
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const EXPECTED_RESPONSES = readFileSync('./test/resources/output/Responses.yml',
const EXPECTED_RESPONSES_MULTI_LANG = readFileSync('./test/resources/output/ResponsesMultiLang.yml', 'utf8')
const EXPECTED_AUTH_REQUEST = readFileSync('./test/resources/output/AuthRequest.yml', 'utf8')
const EXPECTED_RESPONSES_NO_HEADERS = readFileSync('./test/resources/output/ResponsesNoHeaders.yml', 'utf8')
const EXPECTED_FORM_DATA = readFileSync('./test/resources/output/FormData.yml', 'utf8')

const AUTH_DEFINITIONS = {
myCustomAuth: {
Expand Down Expand Up @@ -102,6 +103,7 @@ describe('Library specs', function () {
const COLLECTION_RESPONSES = `./test/resources/input/${version}/Responses.json`
const COLLECTION_RESPONSES_MULTI_LANG = `./test/resources/input/${version}/ResponsesMultiLang.json`
const COLLECTION_AUTH_REQUEST = `./test/resources/input/${version}/AuthRequest.json`
const COLLECTION_FORM_DATA = `./test/resources/input/${version}/FormData.json`

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
Expand Down Expand Up @@ -395,6 +397,11 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_RESPONSES, OUTPUT_PATH, { pathDepth: 2, responseHeaders: false })
equal(result, EXPECTED_RESPONSES_NO_HEADERS)
})

// it('should parse POST methods with form data', async function () {
// const result = await postmanToOpenApi(COLLECTION_FORM_DATA, OUTPUT_PATH, { pathDepth: 2, responseHeaders: false })
// equal(result, COLLECTION_FORM_DATA)
// })
})
})

Expand Down
42 changes: 42 additions & 0 deletions test/resources/input/v2/FormData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"info": {
"_postman_id": "1eaa74c4-2d76-45f0-bd34-bff5d96f0ba8",
"name": "Form Data",
"description": "Just a collection with a form data post for test",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Register New User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces)",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login)",
"type": "text"
},
{
"key": "password",
"value": "pasword123",
"description": "password (to be used for logging in)",
"type": "text"
}
]
},
"url": "https://api.io/register"
},
"response": []
}
]
}
52 changes: 52 additions & 0 deletions test/resources/input/v21/FormData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"info": {
"_postman_id": "1eaa74c4-2d76-45f0-bd34-bff5d96f0ba8",
"name": "Form Data",
"description": "Just a collection with a form data post for test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Register New User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces)",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login)",
"type": "text"
},
{
"key": "password",
"value": "pasword123",
"description": "password (to be used for logging in)",
"type": "text"
}
]
},
"url": {
"raw": "https://api.io/register",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"register"
]
}
},
"response": []
}
]
}
36 changes: 36 additions & 0 deletions test/resources/output/FormData.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.0.0
info:
title: Form Data
description: Just a collection with a form data post for test
version: 1.0.0
servers:
- url: https://api.io
paths:
/register:
post:
tags:
- default
summary: Register New User
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
name:
type: string
description: full name of the user (accepts spaces)
example: New User
email:
type: string
description: email of the user (for notifications and login)
example: newuser@example.com
password:
type: string
description: password (to be used for logging in)
example: pasword123
responses:
'200':
description: Successful response
content:
application/json: {}

0 comments on commit 547b137

Please sign in to comment.