@@ -25,6 +25,23 @@ describe('getSpecVersion', function() {
25
25
expect ( specVersion ) . to . be . equal ( '3.0' ) ;
26
26
} ) ;
27
27
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
+
28
45
it ( 'Should resolve as 3.1 the provided spec version from a YAML input' , function ( ) {
29
46
const inputData = 'openapi: 3.1.0' +
30
47
'info:' +
@@ -48,6 +65,30 @@ describe('getSpecVersion', function() {
48
65
expect ( specVersion ) . to . be . equal ( '3.1' ) ;
49
66
} ) ;
50
67
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
+
51
92
it ( 'Should resolve as 2.0 the provided spec version from a YAML input' , function ( ) {
52
93
const inputData = 'swagger: "2.0"' +
53
94
'info:' +
@@ -74,6 +115,33 @@ describe('getSpecVersion', function() {
74
115
expect ( specVersion ) . to . be . equal ( '2.0' ) ;
75
116
} ) ;
76
117
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
+
77
145
it ( 'Should resolve as 3.0 the provided spec version from a JSON input' , function ( ) {
78
146
const inputData = {
79
147
'openapi' : '3.0.0' ,
@@ -101,6 +169,33 @@ describe('getSpecVersion', function() {
101
169
expect ( specVersion ) . to . be . equal ( '3.0' ) ;
102
170
} ) ;
103
171
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
+
104
199
it ( 'Should resolve as 3.1 the provided spec version from a JSON input' , function ( ) {
105
200
const inputData = {
106
201
'openapi' : '3.1.0' ,
@@ -137,6 +232,43 @@ describe('getSpecVersion', function() {
137
232
expect ( specVersion ) . to . be . equal ( '3.1' ) ;
138
233
} ) ;
139
234
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
+
140
272
it ( 'Should resolve as 2.0 the provided spec version from a JSON input' , function ( ) {
141
273
const inputData = {
142
274
'swagger' : '2.0' ,
@@ -173,6 +305,45 @@ describe('getSpecVersion', function() {
173
305
specVersion = getSpecVersion ( { type : jsonType , data : inputData } ) ;
174
306
expect ( specVersion ) . to . be . equal ( '2.0' ) ;
175
307
} ) ;
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
+ } ) ;
176
347
} ) ;
177
348
178
349
describe ( 'filterOptionsByVersion method' , function ( ) {
@@ -314,23 +485,23 @@ describe('compareVersion method', function () {
314
485
describe ( 'getVersionRegexBySpecificationVersion method' , function ( ) {
315
486
it ( 'should return regex for 3.0' , function ( ) {
316
487
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/' ) ;
318
489
} ) ;
319
490
it ( 'should return regex for 3.0.0' , function ( ) {
320
491
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/' ) ;
322
493
} ) ;
323
494
it ( 'should return regex for 3.1' , function ( ) {
324
495
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/' ) ;
326
497
} ) ;
327
498
it ( 'should return regex for 2.0' , function ( ) {
328
499
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/' ) ;
330
501
} ) ;
331
502
it ( 'should return regex for 3.0 as default' , function ( ) {
332
503
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/' ) ;
334
505
} ) ;
335
506
} ) ;
336
507
0 commit comments