Skip to content

Commit 1492596

Browse files
Merge pull request #81 from apiaddicts/develop
Develop
2 parents ba2ccb2 + 29ad639 commit 1492596

File tree

6 files changed

+94
-82
lines changed

6 files changed

+94
-82
lines changed

src/generator/environmentVariablesNames.js

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -62,79 +62,81 @@ module.exports = function() {
6262
}
6363

6464
function extractVariablesFromString(numerateItem, string, items, itemKeys, id, isUserToken, queryParams = []){
65-
const re = /\{\{(.*?)\}\}/g
66-
const newItems = string.match(re)
67-
for (let i in newItems){
68-
newItems[i] = newItems[i].substring(0,newItems[i].length - 2).substring(2);
69-
if (_.includes(['host','port','basePath'], newItems[i])){
70-
continue;
71-
}
72-
let key = isUserToken? newItems[i] : numerateItem + newItems[i];
73-
if (!_.includes(itemKeys, key)) {
74-
let value = typeof global.environmentVariables[id+newItems[i]] !== 'undefined' ? global.environmentVariables[id+newItems[i]] : '';
75-
if (newItems[i].includes('_not_found')) {
76-
value = require('../utils/exampleForField.js')(
77-
{
78-
name: newItems[i],
79-
type: 'string',
80-
example: value
81-
},
82-
true
83-
);
65+
if(string){
66+
const re = /\{\{(.*?)\}\}/g
67+
const newItems = string.match(re)
68+
for (let i in newItems){
69+
newItems[i] = newItems[i].substring(0,newItems[i].length - 2).substring(2);
70+
if (_.includes(['host','port','basePath'], newItems[i])){
71+
continue;
8472
}
85-
if (queryParams.length > 0 && queryParams.filter(s => s.example !== 'application/pdf')) {
86-
if (!!queryParams.find(s => s.name === newItems[i])) {
87-
const queryItem = queryParams.find(s => s.name === newItems[i]);
88-
const exampleExist = !!queryItem;
89-
value = require('../utils/exampleForField.js')(
90-
{
91-
name: newItems[i],
92-
type: queryItem.type,
93-
example: validExample(queryItem),
94-
},
95-
!exampleExist
96-
);
97-
}
98-
if (newItems[i].includes('_wrong')) {
99-
const queryItem = queryParams.find(s => s.name === newItems[i].replace('_wrong', ''));
100-
value = require('../utils/exampleForField.js')(
73+
let key = isUserToken? newItems[i] : numerateItem + newItems[i];
74+
if (!_.includes(itemKeys, key)) {
75+
let value = typeof global.environmentVariables[id+newItems[i]] !== 'undefined' ? global.environmentVariables[id+newItems[i]] : '';
76+
if (newItems[i].includes('_not_found')) {
77+
value = require('../utils/exampleForField.js')(
10178
{
10279
name: newItems[i],
103-
type: queryItem.type,
104-
example: validExample(queryItem),
80+
type: 'string',
81+
example: value
10582
},
10683
true
10784
);
10885
}
109-
}
110-
// Cuando la variable is_inline del fichero de configuración no diga lo contrario,
111-
// se guardarán las variables de entorno
112-
if (!global.configurationFile.is_inline || isUserToken) {
113-
items.push({
114-
"description": {
115-
"content": "",
116-
"type": "text/plain"
117-
},
118-
"value": value,
119-
"key": key,
120-
"enabled": true
121-
});
122-
itemKeys.push(key);
123-
string = string.replace('{{'+newItems[i]+'}}', '{{'+key+'}}');
86+
if (queryParams.length > 0 && queryParams.filter(s => s.example !== 'application/pdf')) {
87+
if (!!queryParams.find(s => s.name === newItems[i])) {
88+
const queryItem = queryParams.find(s => s.name === newItems[i]);
89+
const exampleExist = !!queryItem;
90+
value = require('../utils/exampleForField.js')(
91+
{
92+
name: newItems[i],
93+
type: queryItem.type,
94+
example: validExample(queryItem),
95+
},
96+
!exampleExist
97+
);
98+
}
99+
if (newItems[i].includes('_wrong')) {
100+
const queryItem = queryParams.find(s => s.name === newItems[i].replace('_wrong', ''));
101+
value = require('../utils/exampleForField.js')(
102+
{
103+
name: newItems[i],
104+
type: queryItem.type,
105+
example: validExample(queryItem),
106+
},
107+
true
108+
);
109+
}
110+
}
111+
// Cuando la variable is_inline del fichero de configuración no diga lo contrario,
112+
// se guardarán las variables de entorno
113+
if (!global.configurationFile.is_inline || isUserToken) {
114+
items.push({
115+
"description": {
116+
"content": "",
117+
"type": "text/plain"
118+
},
119+
"value": value,
120+
"key": key,
121+
"enabled": true
122+
});
123+
itemKeys.push(key);
124+
string = string.replace('{{'+newItems[i]+'}}', '{{'+key+'}}');
125+
} else {
126+
// Se añade el valor del campo en el propio objeto de la petición
127+
if (typeof value !== 'string') {
128+
string = string.replace(`"{{${newItems[i]}}}"`, value);
129+
}
130+
string = string.replace(`{{${newItems[i]}}}`, value);
131+
}
124132
} else {
125-
// Se añade el valor del campo en el propio objeto de la petición
126-
if (typeof value !== 'string') {
127-
string = string.replace(`"{{${newItems[i]}}}"`, value);
133+
if (!global.configurationFile.is_inline || isUserToken) {
134+
string = string.replace('{{'+newItems[i]+'}}', '{{'+key+'}}');
128135
}
129-
string = string.replace(`{{${newItems[i]}}}`, value);
130-
}
131-
} else {
132-
if (!global.configurationFile.is_inline || isUserToken) {
133-
string = string.replace('{{'+newItems[i]+'}}', '{{'+key+'}}');
134136
}
135137
}
138+
return string;
136139
}
137-
return string;
138140
}
139141
function extractVariablesFromTest(aux, execCode, numerateItem, items){
140142
if (global.configurationFile.schema_is_inline === false) {

src/generator/serverPath.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55

66
module.exports = function(){
77
return function get(servers,pattern){
8-
9-
const cleanPattern = pattern.replace(/%/g, "");
10-
const serverFound = servers.find(server => server.url.includes(cleanPattern));
8+
if(servers){
9+
const cleanPattern = pattern.replace(/%/g, "");
10+
const serverFound = servers.find(server => server.url.includes(cleanPattern));
11+
12+
if(serverFound){
13+
const allArray = serverFound.url.split( '/' )
14+
const protocol = allArray[0]
15+
const domain = allArray[2]
16+
const host = protocol + '//' + domain
17+
return {
18+
host: host,
19+
basePath: serverFound.url.replace(host, '')
20+
};
21+
}
1122

12-
if(serverFound){
13-
const allArray = serverFound.url.split( '/' )
14-
const protocol = allArray[0]
15-
const domain = allArray[2]
16-
const host = protocol + '//' + domain
17-
return {
18-
host: host,
19-
basePath: serverFound.url.replace(host, '')
20-
};
23+
return;
2124
}
2225

23-
return;
24-
2526
}
2627
}()

src/parser/openapi3/body.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ module.exports = function() {
139139
continue
140140
} else if (k === 'description') {
141141
continue
142-
} else {
143-
require('../../utils/error.js')('the property '+k+' of allOf is not implemented')
142+
} else if (k === 'items') {
143+
continue
144+
}else {
145+
console.warn('the property '+k+' of allOf is not implemented')
144146
}
145147
}
146148

src/parser/swagger2/body.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ module.exports = function() {
104104
continue;
105105
} else if (k === 'description'){
106106
continue;
107-
} else {
108-
require('../../utils/error.js')('the property '+k+' of allOf is not implemented');
107+
} else if (k === 'items') {
108+
continue
109+
}else {
110+
console.warn('the property '+k+' of allOf is not implemented')
109111
}
110112
}
111113

src/parser/swagger2/headers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = function() {
1818
_.forEach(headers, function(header) {
1919
result.push({
2020
key: header.name,
21-
type: header.schema.type,
21+
type: header.type,
2222
required : header.required,
2323
value: getExamples(header)
2424
});

src/utils/exampleForField.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ module.exports = function() {
3333
}
3434

3535
function getExampleForString(example, isWrong, maxLength) {
36+
const MAX_SAFE_LENGTH = 100000;
37+
const base = String(example || 'badstring').slice(0, 10);
38+
const parsedLength = Number(maxLength);
39+
const isSafe = Number.isFinite(parsedLength) && parsedLength <= MAX_SAFE_LENGTH;
3640
if (isWrong) {
3741
// Crear caso de error con maxLength + 1
38-
if (example && maxLength) {
39-
return _.padEnd(example, maxLength + 1, 'z');
42+
if (isSafe) {
43+
return _.padEnd(base, parsedLength + 1, 'z');
4044
}
4145
return getValueFromConfigurationFile('wrong','string','badstring')
4246
}
@@ -46,6 +50,7 @@ module.exports = function() {
4650
function getExampleForDate(example, isWrong) {
4751
if (isWrong) {
4852
// El mes con valor 50 para casos de error
53+
if(typeof example == 'object') example = '2022-01-01'
4954
if (example) {
5055
let date = example.split('-');
5156
date[1] = '50';

0 commit comments

Comments
 (0)