Skip to content

Commit 4582428

Browse files
committed
Merge branch 'develop' of https://github.com/apiaddicts/openapi2postman into develop
2 parents c914810 + 88bf82a commit 4582428

File tree

13 files changed

+307
-130
lines changed

13 files changed

+307
-130
lines changed

index.js

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,24 @@ try {
3434
is_inline: false,
3535
schema_is_inline: false,
3636
schema_pretty_print: true,
37+
generate_oneOf_anyOf:false,
38+
minimal_endpoints:false,
3739
environments:[
3840
{
3941
name : "DEV",
4042
postman_collection_name: "%api_name%_DEV",
4143
postman_environment_name: "%api_name%_DEV",
4244
target_folder: "./out",
4345
has_scopes: false,
46+
microcks_headers:true,
4447
application_token: false,
45-
number_of_scopes: 0
48+
number_of_scopes: 0,
49+
application_token: false,
50+
has_scopes: false,
51+
read_only: false,
52+
validate_schema:false,
53+
host_server_pattern:"%dev%",
54+
4655
}
4756
]
4857
};
@@ -86,49 +95,54 @@ let endpointsPostman = [];
8695
const endpoints = require('./src/generator/endpoints.js')(endpointsParsed);
8796

8897
_.forEach(endpoints, function (endpoint, i) {
89-
endpoint = require('./src/generator/testStatus.js')(endpoint);
90-
endpoint = require('./src/generator/testBody.js')(endpoint, configurationFile);
91-
endpoint = require('./src/generator/contentType.js')(endpoint);
92-
endpoint = require("./src/generator/microcks.js")(endpoint);
93-
endpoint = require('./src/generator/authorization.js')(endpoint, endpoint.aux.status)
94-
global.currentId = endpoint.request.method + endpoint.request.url.path[0]
95-
global.currentId = global.currentId.replace(/{{/g,'{').replace(/}}/g,'}').split('?')[0]
96-
if (endpoint.aux.status === 404 && endpoint.aux.pathParameter) {
97-
endpoint.request.url.raw = _.replace(endpoint.request.url.raw, '{{' + endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
98-
endpoint.request.url.path[0] = _.replace(endpoint.request.url.path[0], '{{' +endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
99-
endpoint = require('./src/generator/body.js')(endpoint)
100-
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint)
101-
endpointsPostman.push(endpoint)
102-
} else if (endpoint.aux.status === 400) {
103-
global.queryParamsRequiredAdded = []
104-
let endpointPostman
105-
do{
106-
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpoint,true)
107-
if (endpointPostman) {
108-
endpointPostman = require('./src/generator/body.js')(endpointPostman)
109-
if (endpointPostman.aux.hasOwnProperty('suffix') && endpointPostman.aux.suffix.includes('wrong')) {
110-
endpointPostman.name += '.with.' + endpointPostman.aux.suffix;
111-
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointPostman);
112-
endpointPostman.request.url.path[0] = _.replace(endpointPostman.request.url.path[0], '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '}}', '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '_wrong}}')
113-
} else {
114-
endpointPostman.name += '.without.' + _.last(global.queryParamsRequiredAdded);
115-
endpointPostman.aux.suffix = '.without.' +_.last(global.queryParamsRequiredAdded);
98+
for (let index = 0; index < endpoint.count; index++) {
99+
let endpointCopy = _.cloneDeep(endpoint);
100+
endpointCopy = require('./src/generator/testStatus.js')(endpoint);
101+
endpointCopy = require('./src/generator/testBody.js')(endpoint, configurationFile);
102+
endpointCopy = require('./src/generator/contentType.js')(endpoint);
103+
endpointCopy = require("./src/generator/microcks.js")(endpoint);
104+
endpointCopy = require('./src/generator/authorization.js')(endpoint, endpoint.aux.status)
105+
global.currentId = endpointCopy.request.method + endpointCopy.request.url.path[0]
106+
global.currentId = global.currentId.replace(/{{/g,'{').replace(/}}/g,'}').split('?')[0]
107+
if (endpointCopy.aux.status === 404 && endpointCopy.aux.pathParameter) {
108+
endpointCopy.request.url.raw = _.replace(endpointCopy.request.url.raw, '{{' + endpointCopy.aux.pathParameter + '}}', '{{' +endpointCopy.aux.pathParameter + '_not_found}}')
109+
endpointCopy.request.url.path[0] = _.replace(endpointCopy.request.url.path[0], '{{' +endpointCopy.aux.pathParameter + '}}', '{{' +endpointCopy.aux.pathParameter + '_not_found}}')
110+
endpointCopy = require('./src/generator/body.js')(endpointCopy,false,false,index)
111+
endpointCopy = require('./src/generator/queryParamsRequired.js')(endpointCopy)
112+
endpointsPostman.push(endpointCopy)
113+
} else if (endpointCopy.aux.status === 400) {
114+
global.queryParamsRequiredAdded = []
115+
let endpointPostman
116+
do{
117+
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointCopy,true)
118+
if (endpointPostman) {
119+
endpointPostman = require('./src/generator/body.js')(endpointPostman,false, false, index)
120+
if (endpointPostman.aux.hasOwnProperty('suffix') && endpointPostman.aux.suffix.includes('wrong')) {
121+
endpointPostman.name += '.with.' + endpointPostman.aux.suffix;
122+
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointPostman);
123+
endpointPostman.request.url.path[0] = _.replace(endpointPostman.request.url.path[0], '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '}}', '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '_wrong}}')
124+
} else {
125+
endpointPostman.name += '.without.' + _.last(global.queryParamsRequiredAdded);
126+
endpointPostman.aux.suffix = '.without.' +_.last(global.queryParamsRequiredAdded);
127+
}
128+
endpointsPostman.push(endpointPostman);
116129
}
117-
endpointsPostman.push(endpointPostman);
130+
} while(endpointPostman)
131+
132+
const endpointWithoutQueryParamsRequired = require('./src/generator/queryParamsNotRequired.js')(endpointCopy,index);
133+
if (endpointWithoutQueryParamsRequired && endpointCopy) {
134+
endpointsPostman.push(endpointWithoutQueryParamsRequired);
118135
}
119-
} while(endpointPostman)
120-
121-
const endpointWithoutQueryParamsRequired = require('./src/generator/queryParamsNotRequired.js')(endpoint);
122-
if (endpointWithoutQueryParamsRequired && endpoint) {
123-
endpointsPostman.push(endpointWithoutQueryParamsRequired);
136+
137+
let hasWrongParams = global.configurationFile.minimal_endpoints ? false : true;
138+
139+
addBadRequestEndpoints(endpointsPostman, endpointCopy, 'requiredParams', '', true, false,index);
140+
addBadRequestEndpoints(endpointsPostman, endpointCopy, 'wrongParams', '.wrong', false, hasWrongParams,index);
141+
} else if ((endpointCopy.aux.status >= 200 && endpointCopy.aux.status < 300) || ((endpointCopy.aux.status === 401 || endpointCopy.aux.status === 403) && endpointCopy.aux.authorization)) {
142+
endpointCopy = require('./src/generator/body.js')(endpointCopy,false,false,index);
143+
endpointCopy = require('./src/generator/queryParamsRequired.js')(endpointCopy);
144+
endpointsPostman.push(endpointCopy);
124145
}
125-
126-
addBadRequestEndpoints(endpointsPostman, endpoint, 'requiredParams', '', true, false);
127-
addBadRequestEndpoints(endpointsPostman, endpoint, 'wrongParams', '.wrong', false, true);
128-
} else if ((endpoint.aux.status >= 200 && endpoint.aux.status < 300) || ((endpoint.aux.status === 401 || endpoint.aux.status === 403) && endpoint.aux.authorization)) {
129-
endpoint = require('./src/generator/body.js')(endpoint);
130-
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint);
131-
endpointsPostman.push(endpoint);
132146
}
133147
})
134148

@@ -150,6 +164,7 @@ let environments = configurationFile.environments;
150164
_.forEach(environments, function (element) {
151165
const endpointsStage = _.cloneDeep(endpointsPostman)
152166
let exclude = {}
167+
let urlPath;
153168
if ( element.read_only ) {
154169
exclude.write = true
155170
}
@@ -212,12 +227,23 @@ _.forEach(environments, function (element) {
212227
}
213228
}
214229
}
215-
if ( element.custom_authorizations_file ) {
216-
require('./src/parser/authorizationRequests.js')(endpointsStage,element.custom_authorizations_file)
230+
if(global.definition.components.securitySchemes){
231+
console.log('securitySchemes');
232+
let securityDefinition = require('./src/parser/openapiAuthorizationDefinition.js')(global.definition.components.securitySchemes)
233+
if(securityDefinition){
234+
require('./src/parser/authorizationRequests.js')(endpointsStage,securityDefinition)
235+
} else {
236+
exclude.auth = true
237+
}
217238
} else {
218239
// Elimina la cabecera Authorization de las peticiones en Postman
219240
exclude.auth = true
220241
}
242+
243+
if(element.host_server_pattern){
244+
urlPath = require('./src/generator/serverPath.js')(global.definition.servers,element.host_server_pattern)
245+
}
246+
221247
let endpointsPostmanWithFolders = require('./src/generator/folders.js')(endpointsStage, exclude)
222248
// Crea el listado de variables de entorno
223249
let environmentVariables = require('./src/generator/environmentVariablesNames.js')(endpointsPostmanWithFolders)
@@ -232,14 +258,14 @@ _.forEach(environments, function (element) {
232258
element.postman_environment_name = _.replace(element.postman_environment_name, '%api_name%', apiName)
233259
}
234260
require('./src/generator/collection.js')(element.target_folder, element.postman_collection_name, endpointsPostmanWithFolders)
235-
require('./src/generator/environment.js')(element.target_folder, element.postman_environment_name, element.host, element.port, schemaHostBasePath,environmentVariables)
261+
require('./src/generator/environment.js')(element.target_folder, element.postman_environment_name, element.host, element.port, schemaHostBasePath,environmentVariables,urlPath)
236262
})
237-
function addBadRequestEndpoints(endpointsPostman, endpointBase, memoryAlreadyAdded, suffix, withoutRequired, withWrongParam) {
263+
function addBadRequestEndpoints(endpointsPostman, endpointBase, memoryAlreadyAdded, suffix, withoutRequired, withWrongParam,index) {
238264
global[memoryAlreadyAdded] = [];
239265
do {
240266
var initialCount = global[memoryAlreadyAdded].length;
241267
let endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointBase);
242-
endpointPostman = require('./src/generator/body.js')(endpointPostman, withoutRequired, withWrongParam);
268+
endpointPostman = require('./src/generator/body.js')(endpointPostman, withoutRequired, withWrongParam,index);
243269
if (global[memoryAlreadyAdded].length > initialCount) {
244270
endpointPostman.name += '-' + _.last(global[memoryAlreadyAdded]) + suffix;
245271
endpointPostman.aux.suffix = _.last(global[memoryAlreadyAdded]) + suffix;

readme.md

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# 🛠️ Openapi2Postman ![Release](https://img.shields.io/badge/release-2.0.x-purple) ![OpenApi](https://img.shields.io/badge/-openapi-%23Clojure?style=flat&logo=openapiinitiative&logoColor=white) ![Postman](https://img.shields.io/badge/postman-FF6C37?style=flat&logo=postman&logoColor=white) [![NPM](https://img.shields.io/badge/npm-%23CB3837.svg?style=flat&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman) [![License: LGPL v3](https://img.shields.io/badge/license-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
1+
# 🛠️ Openapi2Postman ![Release](https://img.shields.io/badge/release-2.0.x-purple) ![OpenApi](https://img.shields.io/badge/-openapi-%23Clojure?style=flat&logo=openapiinitiative&logoColor=white) ![Postman](https://img.shields.io/badge/postman-FF6C37?style=flat&logo=postman&logoColor=white) [![NPM](https://img.shields.io/badge/npm-%23CB3837.svg?style=flat&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman) [![License: LGPL v3](https://img.shields.io/badge/license-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
22

33
**Openapi2Postman** creates automatic tests from Openapi 3.0 using postman format. Also, it creates environments files, depending of configuration.
4-
Those collections can be importend in postan application. The tests includes tests for 2xx, 4xx... and tests to validate output formats.
4+
Those collections can be importend in postan application. The tests includes tests for 2xx, 4xx... and tests to validate output formats.
55

66
<p align="center">
77
<a href="https://apiaddicts.org/">
@@ -11,64 +11,70 @@ Those collections can be importend in postan application. The tests includes te
1111

1212
### This repository is intended for :octocat: **community** use, it can be modified and adapted without commercial use. If you need a version, support or help for your **enterprise** or project, please contact us 📧 [email protected]
1313

14-
[![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=x&logoColor=white)](https://twitter.com/APIAddicts)
14+
[![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=x&logoColor=white)](https://twitter.com/APIAddicts)
1515
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/ZdbGqMBYy8)
1616
[![LinkedIn](https://img.shields.io/badge/linkedin-%230077B5.svg?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/company/apiaddicts/)
1717
[![Facebook](https://img.shields.io/badge/Facebook-%231877F2.svg?style=for-the-badge&logo=Facebook&logoColor=white)](https://www.facebook.com/apiaddicts)
1818
[![YouTube](https://img.shields.io/badge/YouTube-%23FF0000.svg?style=for-the-badge&logo=YouTube&logoColor=white)](https://www.youtube.com/@APIAddictslmaoo)
1919

20-
# 🙌 Join the **Openapi2postman** Adopters list
20+
# 🙌 Join the **Openapi2postman** Adopters list
21+
2122
📢 If Openapi2postman is part of your organization's toolkit, we kindly encourage you to include your company's name in our Adopters list. 🙏 This not only significantly boosts the project's visibility and reputation but also represents a small yet impactful way to give back to the project.
2223

23-
| Organization | Description of Use / Referenc |
24-
|---|---|
25-
| [CloudAppi](https://cloudappi.net/) | Apification and generation of microservices |
26-
| [Madrid Digital](https://www.comunidad.madrid/servicios/sede-electronica/madrid-digital/) | Generation of microservices |
24+
| Organization | Description of Use / Referenc |
25+
| ----------------------------------------------------------------------------------------- | ------------------------------------------- |
26+
| [CloudAppi](https://cloudappi.net/) | Apification and generation of microservices |
27+
| [Madrid Digital](https://www.comunidad.madrid/servicios/sede-electronica/madrid-digital/) | Generation of microservices |
28+
| [APIquality](https://apiquality.io/) | Common contributor |
2729

28-
# 👩🏽‍💻 Contribute to ApiAddicts
30+
# 👩🏽‍💻 Contribute to ApiAddicts
2931

3032
We're an inclusive and open community, welcoming you to join our effort to enhance ApiAddicts, and we're excited to prioritize tasks based on community input, inviting you to review and collaborate through our GitHub issue tracker.
3133

3234
Feel free to drop by and greet us on our GitHub discussion or Discord chat. You can also show your support by giving us some GitHub stars ⭐️, or by following us on Twitter, LinkedIn, and subscribing to our YouTube channel! 🚀
3335

3436
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/apiaddicts)
3537

36-
37-
# 📑 Getting started
38+
# 📑 Getting started
3839

3940
[![NPM](https://img.shields.io/badge/openapi2postman-%23CB3837.svg?style=for-the-badge&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman)
4041

4142
### Install package
43+
4244
```
4345
npm i openapi2postman -g
4446
```
4547

4648
### First steps
4749

4850
#### `Arguments`
49-
| argv | a |description |
50-
|---|---|---|
51-
| --file | -f | path openapi file |
52-
| --configuration | -c | path config file |
5351

54-
#### `Normal` usage
52+
| argv | a | description |
53+
| --------------- | --- | ----------------- |
54+
| --file | -f | path openapi file |
55+
| --configuration | -c | path config file |
5556

57+
#### `Normal` usage
5658

5759
```
5860
o2p -c .\example\o2p_config_file.json -f .\example\petstore.yaml
5961
```
6062

61-
#### `Default` config
63+
#### `Default` config
6264

6365
```
6466
o2p -f .\example\petstore.yaml
6567
```
68+
6669
If you do not use the -c argument, the following default configuration will be used. The output path `target_folder` is `./out`.
70+
6771
```
6872
api_name: filename,
6973
is_inline: false,
7074
schema_is_inline: false,
7175
schema_pretty_print: true,
76+
generate_oneOf_anyOf:false,
77+
minimal_endpoints:false,
7278
environments:[
7379
{
7480
name : "DEV",
@@ -79,59 +85,60 @@ If you do not use the -c argument, the following default configuration will be u
7985
application_token: false,
8086
number_of_scopes: 0,
8187
microcks_headers: false
88+
read_only: false,
89+
validate_schema:false,
90+
host_server_pattern:"%dev%",
8291
}
8392
]
8493
```
8594

86-
8795
## Table of content
8896

89-
* [Structure and submodules](#structure-and-submodules)
90-
* [Build and run](#build-and-run)
91-
* [Prerequisites](#prerequisites)
92-
* [Get the source code for the first time](#get-the-source-code-for-the-first-time)
93-
* [Run](#run)
94-
* [Get the latest changes](#get-the-latest-changes)
95-
* [Contribute](#contribute)
96-
* [IDE support](#ide-support)
97-
* [IntelliJ IDEA](#intellij-idea)
98-
* [Eclipse](#eclipse)
99-
* [Documentation](#documentation)
100-
* [Advanced Functionality](#advanced-functionality)
101-
* [Additional resources](#additional-resources)
102-
97+
- [Structure and submodules](#structure-and-submodules)
98+
- [Build and run](#build-and-run)
99+
- [Prerequisites](#prerequisites)
100+
- [Get the source code for the first time](#get-the-source-code-for-the-first-time)
101+
- [Run](#run)
102+
- [Get the latest changes](#get-the-latest-changes)
103+
- [Contribute](#contribute)
104+
- [IDE support](#ide-support)
105+
- [IntelliJ IDEA](#intellij-idea)
106+
- [Eclipse](#eclipse)
107+
- [Documentation](#documentation)
108+
- [Advanced Functionality](#advanced-functionality)
109+
- [Additional resources](#additional-resources)
103110

104111
## Structure and submodules
105112

106-
* *[docs](docs)* - This module contents the guides to configurate and run the product. Documents Pdfs by CloudAppi.
107-
* *[example](example)* - Integration and system tests for SoapUI.
108-
* *[src](src)* – Source code
109-
* *[test](soapui-maven-plugin-tester)* - Testing folder
113+
- _[docs](docs)_ - This module contents the guides to configurate and run the product. Documents Pdfs by CloudAppi.
114+
- _[example](example)_ - Integration and system tests for SoapUI.
115+
- _[src](src)_ – Source code
116+
- _[test](soapui-maven-plugin-tester)_ - Testing folder
110117

111118
## Build and run
119+
112120
### Prerequisites
113-
You need node v10 or later.
114121

122+
You need node v10 or later.
115123

116124
### run
117125

118126
Please review the o2p_config_file.json before to execute the command
119-
Execute
127+
Execute
120128
node index.js --configuration o2p_config_file.json --file example/swagger_provincias.yml
121129

122-
The output files are the following (in example folder):
123-
* SWAGGER_API_TestSuite_DEV.postman_collection.json
124-
* SWAGGER_API_TestSuiteEnv_DEV.postman_environment.json
125-
* SWAGGER_API_TestSuite_PROD.postman_collection.json
126-
* SWAGGER_API_TestSuiteEnv_PROD.postman_environment.json
130+
The output files are the following (in example folder):
127131

132+
- SWAGGER_API_TestSuite_DEV.postman_collection.json
133+
- SWAGGER_API_TestSuiteEnv_DEV.postman_environment.json
134+
- SWAGGER_API_TestSuite_PROD.postman_collection.json
135+
- SWAGGER_API_TestSuiteEnv_PROD.postman_environment.json
128136

129137
In postman:
130138

131-
* import generated postman collection file in postman
132-
* import generated postman environment file in postman
133-
* update variables in the envirnoment to test all cases
134-
139+
- import generated postman collection file in postman
140+
- import generated postman environment file in postman
141+
- update variables in the envirnoment to test all cases
135142

136143
### Configuration file
137144

@@ -146,11 +153,11 @@ Node.js and npm package manager are required to run the tool. Its adviced to use
146153
[https://nodejs.org/es/download/](https://nodejs.org/es/download/)
147154

148155
## 💛 Sponsors
156+
149157
<p align="center">
150158
<a href="https://apiaddicts.org/">
151159
<img src="https://apiaddicts.cloudappi.net/web/image/4248/LOGOCloudappi2020Versiones-01.png" alt="cloudappi" width="150"/>
152160
<img src="https://www.comunidad.madrid/sites/default/files/styles/block_teaser_image/public/img/logos-simbolos/logo_centrado_md.png?itok=4rTUhmcj" alt="md" width="150"/>
153161
<img src="https://apiaddicts-web.s3.eu-west-1.amazonaws.com/wp-content/uploads/2022/03/17155736/cropped-APIAddicts-logotipo_rojo.png" height = "75">
154162
</a>
155163
</p>
156-

0 commit comments

Comments
 (0)