Skip to content

Commit

Permalink
feat: formdata and x-www-form-urlencoded support required indicator i…
Browse files Browse the repository at this point in the history
…n postman field descriptor
  • Loading branch information
joolfe committed Nov 5, 2022
1 parent 3ea5e2e commit 8cdbbda
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
43 changes: 25 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,42 +196,40 @@ function parseBody (body = {}, method) {
}
break
case 'formdata': {
let required = body.formdata.filter(obj => /\[required\]/gi.test(obj.description)).map(obj => obj.key)
content = {
'multipart/form-data': {
schema: {
type: 'object',
...(required.length ? { required: required } : {}),
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 @@ -603,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
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 8cdbbda

Please sign in to comment.