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 #211 from joolfe/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joolfe authored Aug 28, 2022
2 parents f5ee60d + b3d8926 commit 09a3f26
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 27 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
8 changes: 7 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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`.

</div></div>
<div class="tilted-section"><div markdown="1">

Expand Down
17 changes: 13 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand All @@ -156,7 +156,6 @@ function parseBody (body = {}, method) {
example = raw
}
}

content = {
'application/json': {
schema: {
Expand All @@ -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
}
Expand Down
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.4.2",
"version": "2.5.0",
"description": "Convert postman collection to OpenAPI spec",
"main": "lib/index.js",
"types": "types/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 () {
Expand All @@ -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)
})
})
7 changes: 6 additions & 1 deletion test/resources/input/v2/RawBody.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion test/resources/input/v21/RawBody.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"header": [],
"body": {
"mode": "raw",
"raw": "testesttestest"
"raw": "testesttestest",
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://api.io/test",
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/Basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions test/resources/output/BasicNoOptions.yml
Original file line number Diff line number Diff line change
@@ -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: {}
2 changes: 1 addition & 1 deletion test/resources/output/BasicWithAuth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/ExternalDocsOptsPartial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/LicenseContactPartial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/LicenseContactPartial2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/MultipleServers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/NoServers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 6 additions & 7 deletions test/resources/output/NullHeader.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/resources/output/RawBody.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ paths:
description: Test Raw Body
requestBody:
content:
application/json:
text/plain:
schema:
type: object
type: string
example: testesttestest
responses:
'200':
Expand Down
2 changes: 1 addition & 1 deletion test/resources/output/ServersOpts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09a3f26

Please sign in to comment.