Skip to content

Commit c16465b

Browse files
authored
Merge pull request #721 from postmanlabs/feature/fix-incorrect-definition-validation
Fixed an issue where definition validation was not considering multiple white space characters.
2 parents 4f2dc7f + 2d0842b commit c16465b

File tree

2 files changed

+177
-6
lines changed

2 files changed

+177
-6
lines changed

lib/common/versionUtils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const _ = require('lodash'),
2121
* @returns {object} the resultant regular expresion using the provided data
2222
*/
2323
function getVersionRegexp({ key, version }) {
24-
return new RegExp(`${key}['|"]?:\\s?[\\\]?['|"]?${version}`);
24+
return new RegExp(`${key}['|"]?\\s*:\\s*[\\\]?['|"]?${version}`);
2525
}
2626

2727
/**

test/unit/versionUtils.test.js

Lines changed: 176 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ describe('getSpecVersion', function() {
2525
expect(specVersion).to.be.equal('3.0');
2626
});
2727

28+
it('Should resolve as 3.0 even if the provided spec contain spaces before version from a YAML input', function() {
29+
const inputData = 'openapi : 3.0.0' +
30+
'info:' +
31+
' version: 1.0.0' +
32+
' title: Sample API' +
33+
' description: A sample API to illustrate OpenAPI concepts' +
34+
'paths:' +
35+
' /list:' +
36+
' get:' +
37+
' description: Returns a list of stuff' +
38+
' responses:' +
39+
' \'200\':' +
40+
' description: Successful response',
41+
specVersion = getSpecVersion({ type: stringType, data: inputData });
42+
expect(specVersion).to.be.equal('3.0');
43+
});
44+
2845
it('Should resolve as 3.1 the provided spec version from a YAML input', function() {
2946
const inputData = 'openapi: 3.1.0' +
3047
'info:' +
@@ -48,6 +65,30 @@ describe('getSpecVersion', function() {
4865
expect(specVersion).to.be.equal('3.1');
4966
});
5067

68+
it('Should resolve as 3.1 even if the provided spec contain spaces before version from a YAML input', function() {
69+
// Below data contains tabs and spaces before version field which is considered a valid YAML
70+
const inputData = 'openapi : \t"3.1.0"' +
71+
'info:' +
72+
' title: Non-oAuth Scopes example' +
73+
' version: 1.0.0' +
74+
'paths:' +
75+
' /users:' +
76+
' get:' +
77+
' security:' +
78+
' - bearerAuth:' +
79+
' - \'read:users\'' +
80+
' - \'public\'' +
81+
'components:' +
82+
' securitySchemes:' +
83+
' bearerAuth:' +
84+
' type: http' +
85+
' scheme: bearer' +
86+
' bearerFormat: jwt' +
87+
' description: \'note: non-oauth scopes are not defined at the securityScheme level\'',
88+
specVersion = getSpecVersion({ type: stringType, data: inputData });
89+
expect(specVersion).to.be.equal('3.1');
90+
});
91+
5192
it('Should resolve as 2.0 the provided spec version from a YAML input', function() {
5293
const inputData = 'swagger: "2.0"' +
5394
'info:' +
@@ -74,6 +115,33 @@ describe('getSpecVersion', function() {
74115
expect(specVersion).to.be.equal('2.0');
75116
});
76117

118+
it('Should resolve as 2.0 even if the provided spec contain spaces before version from a YAML input', function() {
119+
// Below data contains newline before version field which is considered a valid YAML
120+
const inputData = 'swagger :\n "2.0"\n' +
121+
'info:' +
122+
' version: 1.0.0' +
123+
' title: Swagger Petstore' +
124+
' license:' +
125+
' name: MIT' +
126+
'host: petstore.swagger.io' +
127+
'basePath: /v1' +
128+
'schemes:' +
129+
' - http' +
130+
'consumes:' +
131+
' - application/json' +
132+
'produces:' +
133+
' - application/json' +
134+
'paths:' +
135+
' /pets:' +
136+
' get:' +
137+
' summary: List all pets' +
138+
' operationId: listPets' +
139+
' tags:' +
140+
' - pets',
141+
specVersion = getSpecVersion({ type: stringType, data: inputData });
142+
expect(specVersion).to.be.equal('2.0');
143+
});
144+
77145
it('Should resolve as 3.0 the provided spec version from a JSON input', function() {
78146
const inputData = {
79147
'openapi': '3.0.0',
@@ -101,6 +169,33 @@ describe('getSpecVersion', function() {
101169
expect(specVersion).to.be.equal('3.0');
102170
});
103171

172+
it('Should resolve as 3.0 even if the provided spec contain spaces before version from a JSON input', function() {
173+
const inputData = `{
174+
'openapi' : '3.0.0',
175+
'info': {
176+
'version': '1.0.0',
177+
'title': 'Sample API',
178+
'description': 'A sample API to illustrate OpenAPI concepts'
179+
},
180+
'paths': {
181+
'/users': {
182+
'get': {
183+
'security': [
184+
{
185+
'bearerAuth': [
186+
'read:users',
187+
'public'
188+
]
189+
}
190+
]
191+
}
192+
}
193+
}
194+
}`,
195+
specVersion = getSpecVersion({ type: stringType, data: inputData });
196+
expect(specVersion).to.be.equal('3.0');
197+
});
198+
104199
it('Should resolve as 3.1 the provided spec version from a JSON input', function() {
105200
const inputData = {
106201
'openapi': '3.1.0',
@@ -137,6 +232,43 @@ describe('getSpecVersion', function() {
137232
expect(specVersion).to.be.equal('3.1');
138233
});
139234

235+
it('Should resolve as 3.1 even if the provided spec contain spaces before version from a JSON input', function() {
236+
// Below data contains both tab and spaces after openapi field
237+
const inputData = `{
238+
'openapi' : '3.1.0',
239+
'info': {
240+
'title': 'Non-oAuth Scopes example',
241+
'version': '1.0.0'
242+
},
243+
'paths': {
244+
'/users': {
245+
'get': {
246+
'security': [
247+
{
248+
'bearerAuth': [
249+
'read:users',
250+
'public'
251+
]
252+
}
253+
]
254+
}
255+
}
256+
},
257+
'components': {
258+
'securitySchemes': {
259+
'bearerAuth': {
260+
'type': 'http',
261+
'scheme': 'bearer',
262+
'bearerFormat': 'jwt',
263+
'description': 'note: non-oauth scopes are not defined at the securityScheme level'
264+
}
265+
}
266+
}
267+
}`,
268+
specVersion = getSpecVersion({ type: stringType, data: inputData });
269+
expect(specVersion).to.be.equal('3.1');
270+
});
271+
140272
it('Should resolve as 2.0 the provided spec version from a JSON input', function() {
141273
const inputData = {
142274
'swagger': '2.0',
@@ -173,6 +305,45 @@ describe('getSpecVersion', function() {
173305
specVersion = getSpecVersion({ type: jsonType, data: inputData });
174306
expect(specVersion).to.be.equal('2.0');
175307
});
308+
309+
it('Should resolve as 2.0 even if the provided spec contain spaces before version from a JSON input', function() {
310+
// Below data contains new line before version field which is a valid json
311+
const inputData = `{
312+
'swagger':
313+
'2.0',
314+
'info': {
315+
'version': '1.0.0',
316+
'title': 'Swagger Petstore',
317+
'license': {
318+
'name': 'MIT'
319+
}
320+
},
321+
'host': 'petstore.swagger.io',
322+
'basePath': '/v1',
323+
'schemes': [
324+
'http'
325+
],
326+
'consumes': [
327+
'application/json'
328+
],
329+
'produces': [
330+
'application/json'
331+
],
332+
'paths': {
333+
'/pets': {
334+
'get': {
335+
'summary': 'List all pets',
336+
'operationId': 'listPets',
337+
'tags': [
338+
'pets'
339+
]
340+
}
341+
}
342+
}
343+
}`,
344+
specVersion = getSpecVersion({ type: stringType, data: inputData });
345+
expect(specVersion).to.be.equal('2.0');
346+
});
176347
});
177348

178349
describe('filterOptionsByVersion method', function() {
@@ -314,23 +485,23 @@ describe('compareVersion method', function () {
314485
describe('getVersionRegexBySpecificationVersion method', function () {
315486
it('should return regex for 3.0', function () {
316487
const result = getVersionRegexBySpecificationVersion('3.0');
317-
expect(result.toString()).to.equal('/openapi[\'|\"]?:\\s?[\\]?[\'|\"]?3.0/');
488+
expect(result.toString()).to.equal('/openapi[\'|\"]?\\s*:\\s*[\\]?[\'|\"]?3.0/');
318489
});
319490
it('should return regex for 3.0.0', function () {
320491
const result = getVersionRegexBySpecificationVersion('3.0.0');
321-
expect(result.toString()).to.equal('/openapi[\'|\"]?:\\s?[\\]?[\'|\"]?3.0/');
492+
expect(result.toString()).to.equal('/openapi[\'|\"]?\\s*:\\s*[\\]?[\'|\"]?3.0/');
322493
});
323494
it('should return regex for 3.1', function () {
324495
const result = getVersionRegexBySpecificationVersion('3.1');
325-
expect(result.toString()).to.equal('/openapi[\'|\"]?:\\s?[\\]?[\'|\"]?3.1/');
496+
expect(result.toString()).to.equal('/openapi[\'|\"]?\\s*:\\s*[\\]?[\'|\"]?3.1/');
326497
});
327498
it('should return regex for 2.0', function () {
328499
const result = getVersionRegexBySpecificationVersion('2.0');
329-
expect(result.toString()).to.equal('/swagger[\'|\"]?:\\s?[\\]?[\'|\"]?2.0/');
500+
expect(result.toString()).to.equal('/swagger[\'|\"]?\\s*:\\s*[\\]?[\'|\"]?2.0/');
330501
});
331502
it('should return regex for 3.0 as default', function () {
332503
const result = getVersionRegexBySpecificationVersion('invalid');
333-
expect(result.toString()).to.equal('/openapi[\'|\"]?:\\s?[\\]?[\'|\"]?3.0/');
504+
expect(result.toString()).to.equal('/openapi[\'|\"]?\\s*:\\s*[\\]?[\'|\"]?3.0/');
334505
});
335506
});
336507

0 commit comments

Comments
 (0)