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

Commit

Permalink
Merge pull request #129 from devNoiseConsulting/master
Browse files Browse the repository at this point in the history
Merge into develop for review and update docs
  • Loading branch information
joolfe authored Jul 30, 2021
2 parents e8283c1 + 9994c8d commit 8254840
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/assets/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $header-bg-color-secondary: #5f5278 !default;
$body-link-color: #3b95ef !default;
$section-headings-color: #ff6b37 !default;

@import "{{ site.theme }}";
@import "jekyll-theme-cayman";

strong {
color: #788286;
Expand Down
32 changes: 23 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ function parseBody (body = {}, method) {
'multipart/form-data': {
schema: {
type: 'object',
properties: body.formdata.reduce((obj, { key, type, description, value }) => {
obj[key] = {
type: 'string',
...(description ? { description } : {}),
...(value ? { example: value } : {}),
...(type === 'file' ? { format: 'binary' } : {})
}
return obj
}, {})
properties: body.formdata.reduce(mapFormData(), {})
}
}
}
break
case 'urlencoded':
content = {
'application/x-www-form-urlencoded': {
schema: {
properties: body.urlencoded.reduce(mapFormData(), {})
}
}
}
Expand All @@ -188,6 +189,19 @@ function parseBody (body = {}, method) {
return { requestBody: { content } }
}

/* Accumulator function for form data values */
function mapFormData () {
return (obj, { key, type, description, value }) => {
obj[key] = {
type: inferType(value),
...(description ? { description: description.replace(/ ?\[required\] ?/gi, '') } : {}),
...(value ? { example: value } : {}),
...(type === 'file' ? { format: 'binary' } : {})
}
return obj
}
}

/* Parse the Postman query and header and transform into OpenApi parameters */
function parseParameters (query = [], header, paths, paramsMeta = {}) {
// parse Headers
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const EXPECTED_RESPONSES_MULTI_LANG = readFileSync('./test/resources/output/Resp
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 EXPECTED_WWW_FORM_URLENCODED = readFileSync('./test/resources/output/WwwFormUrlencoded.yml', 'utf8')

const AUTH_DEFINITIONS = {
myCustomAuth: {
Expand Down Expand Up @@ -104,6 +105,7 @@ describe('Library specs', function () {
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`
const COLLECTION_WWW_FORM_URLENCODED = `./test/resources/input/${version}/WwwFormUrlencoded.json`

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

it('should parse POST methods with www form urlencoded', async function () {
const result = await postmanToOpenApi(COLLECTION_WWW_FORM_URLENCODED, OUTPUT_PATH, {})
equal(result, EXPECTED_WWW_FORM_URLENCODED)
})
})
})

Expand Down
7 changes: 4 additions & 3 deletions test/resources/input/v2/FormData.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces)",
"description": "full name of the user (accepts spaces) [required]",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login)",
"description": "email of the user (for notifications and login) [required]",
"type": "text"
},
{
Expand All @@ -34,6 +34,7 @@
{
"key": "profileImage",
"description": "User avatar",
"contentType": "application/octet-stream",
"type": "file",
"src": []
}
Expand All @@ -44,4 +45,4 @@
"response": []
}
]
}
}
41 changes: 41 additions & 0 deletions test/resources/input/v2/WwwFormUrlencoded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"info": {
"_postman_id": "1eaa74c4-2d76-45f0-bd34-bff5d96f0ba8",
"name": "Form Data",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "Register New User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces) [required]",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login) [required]",
"type": "text"
},
{
"key": "password",
"value": "pasword123",
"description": "password (to be used for logging in) [required]",
"type": "text"
}
]
},
"url": "https://api.io/register"
},
"response": []
}
]
}
7 changes: 4 additions & 3 deletions test/resources/input/v21/FormData.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces)",
"description": "full name of the user (accepts spaces) [required]",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login)",
"description": "email of the user (for notifications and login) [required]",
"type": "text"
},
{
Expand All @@ -34,6 +34,7 @@
{
"key": "profileImage",
"description": "User avatar",
"contentType": "application/octet-stream",
"type": "file",
"src": []
}
Expand All @@ -54,4 +55,4 @@
"response": []
}
]
}
}
51 changes: 51 additions & 0 deletions test/resources/input/v21/WwwFormUrlencoded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"info": {
"_postman_id": "1eaa74c4-2d76-45f0-bd34-bff5d96f0ba8",
"name": "Form Data",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Register New User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "name",
"value": "New User",
"description": "full name of the user (accepts spaces) [required]",
"type": "text"
},
{
"key": "email",
"value": "newuser@example.com",
"description": "email of the user (for notifications and login) [required]",
"type": "text"
},
{
"key": "password",
"value": "pasword123",
"description": "password (to be used for logging in) [required]",
"type": "text"
}
]
},
"url": {
"raw": "https://api.io/register",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"register"
]
}
},
"response": []
}
]
}
34 changes: 34 additions & 0 deletions test/resources/output/WwwFormUrlencoded.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
openapi: 3.0.0
info:
title: Form Data
version: 1.0.0
servers:
- url: https://api.io
paths:
/register:
post:
tags:
- default
summary: Register New User
requestBody:
content:
application/x-www-form-urlencoded:
schema:
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 8254840

Please sign in to comment.