diff --git a/lib/index.js b/lib/index.js index 0ef1103..ec9f1d3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) } /** @@ -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 diff --git a/package-lock.json b/package-lock.json index c4361ef..7610e13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "postman-to-openapi", - "version": "2.7.2", + "version": "2.8.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "postman-to-openapi", - "version": "2.7.2", + "version": "2.8.0", "license": "MIT", "dependencies": { "commander": "^8.3.0", diff --git a/package.json b/package.json index 432aacc..882ea61 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/resources/output/FormUrlencoded.yml b/test/resources/output/FormUrlencoded.yml index 6e1d141..82dbdb5 100644 --- a/test/resources/output/FormUrlencoded.yml +++ b/test/resources/output/FormUrlencoded.yml @@ -14,6 +14,11 @@ paths: content: application/x-www-form-urlencoded: schema: + type: object + required: + - name + - email + - password properties: name: type: string