Skip to content

Commit 1bfb143

Browse files
Merge pull request #53 from apiaddicts/develop
Develop
2 parents 7423bdb + 88bf82a commit 1bfb143

13 files changed

+306
-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: 54 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,65 +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 |
2728
| [APIquality](https://apiquality.io/) | Common contributor |
2829

29-
# 👩🏽‍💻 Contribute to ApiAddicts
30+
# 👩🏽‍💻 Contribute to ApiAddicts
3031

3132
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.
3233

3334
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! 🚀
3435

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

37-
38-
# 📑 Getting started
38+
# 📑 Getting started
3939

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

4242
### Install package
43+
4344
```
4445
npm i openapi2postman -g
4546
```
4647

4748
### First steps
4849

4950
#### `Arguments`
50-
| argv | a |description |
51-
|---|---|---|
52-
| --file | -f | path openapi file |
53-
| --configuration | -c | path config file |
5451

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

57+
#### `Normal` usage
5758

5859
```
5960
o2p -c .\example\o2p_config_file.json -f .\example\petstore.yaml
6061
```
6162

62-
#### `Default` config
63+
#### `Default` config
6364

6465
```
6566
o2p -f .\example\petstore.yaml
6667
```
68+
6769
If you do not use the -c argument, the following default configuration will be used. The output path `target_folder` is `./out`.
70+
6871
```
6972
api_name: filename,
7073
is_inline: false,
7174
schema_is_inline: false,
7275
schema_pretty_print: true,
76+
generate_oneOf_anyOf:false,
77+
minimal_endpoints:false,
7378
environments:[
7479
{
7580
name : "DEV",
@@ -80,59 +85,60 @@ If you do not use the -c argument, the following default configuration will be u
8085
application_token: false,
8186
number_of_scopes: 0,
8287
microcks_headers: false
88+
read_only: false,
89+
validate_schema:false,
90+
host_server_pattern:"%dev%",
8391
}
8492
]
8593
```
8694

87-
8895
## Table of content
8996

90-
* [Structure and submodules](#structure-and-submodules)
91-
* [Build and run](#build-and-run)
92-
* [Prerequisites](#prerequisites)
93-
* [Get the source code for the first time](#get-the-source-code-for-the-first-time)
94-
* [Run](#run)
95-
* [Get the latest changes](#get-the-latest-changes)
96-
* [Contribute](#contribute)
97-
* [IDE support](#ide-support)
98-
* [IntelliJ IDEA](#intellij-idea)
99-
* [Eclipse](#eclipse)
100-
* [Documentation](#documentation)
101-
* [Advanced Functionality](#advanced-functionality)
102-
* [Additional resources](#additional-resources)
103-
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)
104110

105111
## Structure and submodules
106112

107-
* *[docs](docs)* - This module contents the guides to configurate and run the product. Documents Pdfs by CloudAppi.
108-
* *[example](example)* - Integration and system tests for SoapUI.
109-
* *[src](src)* – Source code
110-
* *[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
111117

112118
## Build and run
119+
113120
### Prerequisites
114-
You need node v10 or later.
115121

122+
You need node v10 or later.
116123

117124
### run
118125

119126
Please review the o2p_config_file.json before to execute the command
120-
Execute
127+
Execute
121128
node index.js --configuration o2p_config_file.json --file example/swagger_provincias.yml
122129

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

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
129136

130137
In postman:
131138

132-
* import generated postman collection file in postman
133-
* import generated postman environment file in postman
134-
* update variables in the envirnoment to test all cases
135-
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
136142

137143
### Configuration file
138144

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

149155
## 💛 Sponsors
156+
150157
<p align="center">
151158
<a href="https://apiaddicts.org/">
152159
<img src="https://apiaddicts.cloudappi.net/web/image/4248/LOGOCloudappi2020Versiones-01.png" alt="cloudappi" width="150"/>
153160
<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"/>
154161
<img src="https://apiaddicts-web.s3.eu-west-1.amazonaws.com/wp-content/uploads/2022/03/17155736/cropped-APIAddicts-logotipo_rojo.png" height = "75">
155162
</a>
156163
</p>
157-

0 commit comments

Comments
 (0)