Skip to content

Commit

Permalink
Merge pull request #239 from joolfe/develop
Browse files Browse the repository at this point in the history
feat: formdata and x-www-form-urlencoded support required indicator in postman field descriptor
  • Loading branch information
joolfe authored Nov 5, 2022
2 parents e5cfeaf + 8cdbbda commit 66916ec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
44 changes: 27 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,40 +195,41 @@ function parseBody (body = {}, method) {
'text/plain': {}
}
break
case 'formdata':
case 'formdata': {
content = {
'multipart/form-data': {
schema: {
type: 'object',
properties: body.formdata.reduce(mapFormData(), {})
}
}
'multipart/form-data': parseFormData(body.formdata)
}
break
}
case 'urlencoded':
content = {
'application/x-www-form-urlencoded': {
schema: {
properties: body.urlencoded.reduce(mapFormData(), {})
}
}
'application/x-www-form-urlencoded': parseFormData(body.urlencoded)
}
break
}
return { requestBody: { content } }
}

/* Accumulator function for form data values */
function mapFormData () {
return (obj, { key, type, description, value }) => {
obj[key] = {
/** Parse the body for create a form data structure */
function parseFormData (data) {
const objectSchema = {
schema: {
type: 'object'
}
}
return data.reduce((obj, { key, type, description, value }) => {
const { schema } = obj
if (isRequired(description)) {
(schema.required = schema.required || []).push(key)
}
(schema.properties = schema.properties || {})[key] = {
type: inferType(value),
...(description ? { description: description.replace(/ ?\[required\] ?/gi, '') } : {}),
...(value ? { example: value } : {}),
...(type === 'file' ? { format: 'binary' } : {})
}
return obj
}
}, objectSchema)
}

/**
Expand Down Expand Up @@ -600,6 +601,15 @@ async function resolveInput (input) {
}
}

/**
* return if the provided text contains the '[required]' mark
* @param {*} text The text where we should look for the required mark
* @returns boolean
*/
function isRequired (text) {
return /\[required\]/gi.test(text)
}

postmanToOpenApi.version = version

module.exports = postmanToOpenApi
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postman-to-openapi",
"version": "2.7.2",
"version": "2.8.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions test/resources/output/FormData.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ paths:
multipart/form-data:
schema:
type: object
required:
- name
- email
properties:
name:
type: string
Expand Down
5 changes: 5 additions & 0 deletions test/resources/output/FormUrlencoded.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ paths:
content:
application/x-www-form-urlencoded:
schema:
type: object
required:
- name
- email
- password
properties:
name:
type: string
Expand Down

0 comments on commit 66916ec

Please sign in to comment.