diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d92cb1..91b4c79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## [2.5.0](https://github.com/joolfe/postman-to-openapi/compare/2.4.2...2.5.0) (2022-08-28) + + +### Features + +* if no language is chosen in body raw mode then use '*/*' ([473db6b](https://github.com/joolfe/postman-to-openapi/commit/473db6b85b66a0604552f76ef84ef99a19ffad14)) +* support now 'text/plain' when body request is raw and language is text ([53cee26](https://github.com/joolfe/postman-to-openapi/commit/53cee26c474ccd452487ad7dd213a705f63def68)) + + +### Build System + +* update version ([83f6c46](https://github.com/joolfe/postman-to-openapi/commit/83f6c46f8c0177b343809bbdf5d1b15879d52d54)) + ### [2.4.1](https://github.com/joolfe/postman-to-openapi/compare/2.2.1...2.4.1) (2022-07-25) diff --git a/docs/index.md b/docs/index.md index fb9530f..5fea780 100644 --- a/docs/index.md +++ b/docs/index.md @@ -350,7 +350,7 @@ Take into account that variable values provided in the `additionalVars` Object s ## Basic conversion -This library support the transformation from Postman collection to all the basic HTTP method as GET, POST, PUT... parse the body request of type "raw" (`Json` and `Text`), "form-data" (see ["form-data" body](#form-data-body) section for more info about this mode) and "x-www-form-urlencoded" formats. [Query parameters](#parameters-parsing) are also supported. +This library support the transformation from Postman collection to all the basic HTTP method as GET, POST, PUT... parse the body request of type "raw" (`Json` and `Text`, please see [Postman raw body](#postman-raw-body)), "form-data" (see ["form-data" body](#form-data-body) section for more info about this mode) and "x-www-form-urlencoded" formats. [Query parameters](#parameters-parsing) are also supported. Have a look to the [PostmantoOpenAPI collection](https://github.com/joolfe/postman-to-openapi/blob/master/test/resources/input/v21/PostmantoOpenAPI.json) file for an example of how to use this feature. @@ -479,6 +479,12 @@ Library `postman-to-openapi` is able to parse the Postman collection body reques A "form-data" request body will be describe as a `multipart/form-data` content with schema of type `object`. For `Text` parameter `postman-to-openapi` will parse just as a `type: string` parameter and for type `File` following OpenAPI specs is parsed as `type: string, format: binary` +## Postman raw body + +When using the `raw` mode in Postman a select box appear to choose the language, please ensure that you select a language manually, even if you see that select box have "Text" as default in some verison of postman if you choose one manually this will be saved as empty. + +The default behaviour of the library when no language is choosed in the `raw` body type is to use the content type `*/*` with schema type `string`. +
diff --git a/lib/index.js b/lib/index.js index cb6563d..864811d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -141,7 +141,7 @@ function parseExternalDocs (variables, optsExternalDocs) { function parseBody (body = {}, method) { // Swagger validation return an error if GET has body if (['GET', 'DELETE'].includes(method)) return {} - const { mode, raw, options = { raw: { language: 'json' } } } = body + const { mode, raw, options = { raw } } = body let content = {} switch (mode) { case 'raw': { @@ -156,7 +156,6 @@ function parseBody (body = {}, method) { example = raw } } - content = { 'application/json': { schema: { @@ -165,15 +164,25 @@ function parseBody (body = {}, method) { } } } - } else { + } else if (language === 'text') { content = { - 'application/json': { + 'text/plain': { schema: { type: 'string', example: raw } } } + } else { + content = { + '*/*': { + schema: { + type: 'string', + // To protect from object types we always stringify this + example: JSON.stringify(raw) + } + } + } } break } diff --git a/package.json b/package.json index bfad320..778f972 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postman-to-openapi", - "version": "2.4.2", + "version": "2.5.0", "description": "Convert postman collection to OpenAPI spec", "main": "lib/index.js", "types": "types/index.d.ts", diff --git a/test/index.spec.js b/test/index.spec.js index 04845f9..3948929 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -14,6 +14,7 @@ const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.json' const COLLECTION_NULL_HEADERS = './test/resources/input/NullHeaders.json' const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8') +const EXPECTED_BASIC_NO_OPTS = readFileSync('./test/resources/output/BasicNoOptions.yml', 'utf8') const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8') const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8') const EXPECTED_CUSTOM_TAG = readFileSync('./test/resources/output/CustomTag.yml', 'utf8') @@ -495,7 +496,7 @@ describe('Library specs', function () { it('should work if no options in request body', async function () { const result = await postmanToOpenApi(COLLECTION_NO_OPTIONS, OUTPUT_PATH, {}) - equal(result, EXPECTED_BASIC) + equal(result, EXPECTED_BASIC_NO_OPTS) }) it('should expose the version of the library', async function () { @@ -510,6 +511,6 @@ describe('Library specs', function () { it('should work with string as input (instead of a file path)', async function () { const collectionString = await readFile(COLLECTION_NO_OPTIONS, 'utf8') const result = await postmanToOpenApi(collectionString, OUTPUT_PATH, {}) - equal(result, EXPECTED_BASIC) + equal(result, EXPECTED_BASIC_NO_OPTS) }) }) diff --git a/test/resources/input/v2/RawBody.json b/test/resources/input/v2/RawBody.json index 3ef95bf..879ecf6 100644 --- a/test/resources/input/v2/RawBody.json +++ b/test/resources/input/v2/RawBody.json @@ -13,7 +13,12 @@ "header": [], "body": { "mode": "raw", - "raw": "testesttestest" + "raw": "testesttestest", + "options": { + "raw": { + "language": "text" + } + } }, "url": "https://api.io/test", "description": "Test Raw Body" diff --git a/test/resources/input/v21/RawBody.json b/test/resources/input/v21/RawBody.json index 314235e..867a0cc 100644 --- a/test/resources/input/v21/RawBody.json +++ b/test/resources/input/v21/RawBody.json @@ -13,7 +13,12 @@ "header": [], "body": { "mode": "raw", - "raw": "testesttestest" + "raw": "testesttestest", + "options": { + "raw": { + "language": "text" + } + } }, "url": { "raw": "https://api.io/test", diff --git a/test/resources/output/Basic.yml b/test/resources/output/Basic.yml index 10e8fd1..156a103 100644 --- a/test/resources/output/Basic.yml +++ b/test/resources/output/Basic.yml @@ -48,7 +48,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/BasicNoOptions.yml b/test/resources/output/BasicNoOptions.yml new file mode 100644 index 0000000..f4e5eb6 --- /dev/null +++ b/test/resources/output/BasicNoOptions.yml @@ -0,0 +1,57 @@ +openapi: 3.0.0 +info: + title: Postman to OpenAPI + description: Mi super test collection from postman + version: 1.1.0 +servers: + - url: https://api.io +paths: + /users: + post: + tags: + - default + summary: Create new User + description: Create a new user into your amazing API + requestBody: + content: + '*/*': + schema: + type: string + example: >- + "{\n \"example\": \"field\",\n \"other\": {\n + \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}" + responses: + '200': + description: Successful response + content: + application/json: {} + /posts: + post: + tags: + - default + summary: Create a post + requestBody: + content: + text/plain: {} + responses: + '200': + description: Successful response + content: + application/json: {} + /note: + post: + tags: + - default + summary: Create a note + description: Just an example of text raw body + requestBody: + content: + text/plain: + schema: + type: string + example: This is an example Note + responses: + '200': + description: Successful response + content: + application/json: {} diff --git a/test/resources/output/BasicWithAuth.yml b/test/resources/output/BasicWithAuth.yml index b74d8ed..a47e23c 100644 --- a/test/resources/output/BasicWithAuth.yml +++ b/test/resources/output/BasicWithAuth.yml @@ -62,7 +62,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/ExternalDocsOptsPartial.yml b/test/resources/output/ExternalDocsOptsPartial.yml index 74f5938..0e0c03e 100644 --- a/test/resources/output/ExternalDocsOptsPartial.yml +++ b/test/resources/output/ExternalDocsOptsPartial.yml @@ -50,7 +50,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/LicenseContactPartial.yml b/test/resources/output/LicenseContactPartial.yml index 2de138e..852f5e0 100644 --- a/test/resources/output/LicenseContactPartial.yml +++ b/test/resources/output/LicenseContactPartial.yml @@ -52,7 +52,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/LicenseContactPartial2.yml b/test/resources/output/LicenseContactPartial2.yml index 84ec419..9ca7e5d 100644 --- a/test/resources/output/LicenseContactPartial2.yml +++ b/test/resources/output/LicenseContactPartial2.yml @@ -52,7 +52,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/MultipleServers.yml b/test/resources/output/MultipleServers.yml index 4993b93..cd35848 100644 --- a/test/resources/output/MultipleServers.yml +++ b/test/resources/output/MultipleServers.yml @@ -70,7 +70,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/NoServers.yml b/test/resources/output/NoServers.yml index 333dda8..7a354dd 100644 --- a/test/resources/output/NoServers.yml +++ b/test/resources/output/NoServers.yml @@ -66,7 +66,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note diff --git a/test/resources/output/NullHeader.yml b/test/resources/output/NullHeader.yml index e6292bc..8481ac8 100644 --- a/test/resources/output/NullHeader.yml +++ b/test/resources/output/NullHeader.yml @@ -17,14 +17,13 @@ paths: Retourne un token en cas de succès requestBody: content: - application/json: + '*/*': schema: - type: object - example: - contenu: - compte: '{{compte}}' - motDePasse: '{{motDePasse}}' - identifiant: '{{identifiant}}' + type: string + example: >- + "{\r\n \"contenu\":\r\n {\r\n \"compte\": \"{{compte}}\",\r\n + \"motDePasse\": \"{{motDePasse}}\",\r\n \"identifiant\": + \"{{identifiant}}\"\r\n }\r\n}" parameters: - name: Content-Type in: header diff --git a/test/resources/output/RawBody.yml b/test/resources/output/RawBody.yml index 2d7f94f..af9e126 100644 --- a/test/resources/output/RawBody.yml +++ b/test/resources/output/RawBody.yml @@ -14,9 +14,9 @@ paths: description: Test Raw Body requestBody: content: - application/json: + text/plain: schema: - type: object + type: string example: testesttestest responses: '200': diff --git a/test/resources/output/ServersOpts.yml b/test/resources/output/ServersOpts.yml index b1eecb6..c089552 100644 --- a/test/resources/output/ServersOpts.yml +++ b/test/resources/output/ServersOpts.yml @@ -71,7 +71,7 @@ paths: description: Just an example of text raw body requestBody: content: - application/json: + text/plain: schema: type: string example: This is an example Note