Skip to content

Commit b33ba3e

Browse files
committed
fixed deprecated parsing per swagger-api/swagger-ui#907, #232
1 parent ef54d2b commit b33ba3e

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

lib/swagger-client.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,20 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
578578
this.description = args.description;
579579
this.useJQuery = parent.useJQuery;
580580

581+
if(typeof this.deprecated === 'string') {
582+
switch(this.deprecated.toLowerCase()) {
583+
case 'true': case 'yes': case '1': {
584+
this.deprecated = true;
585+
break;
586+
}
587+
case 'false': case 'no': case '0': case null: {
588+
this.deprecated = false;
589+
break;
590+
}
591+
default: this.deprecated = Boolean(this.deprecated);
592+
}
593+
}
594+
581595
if(definitions) {
582596
// add to global models
583597
var key;
@@ -1683,6 +1697,7 @@ var SwaggerResource = function (resourceObj, api) {
16831697
this.description = resourceObj.description;
16841698
this.authorizations = (resourceObj.authorizations || {});
16851699

1700+
16861701
var parts = this.path.split('/');
16871702
this.name = parts[parts.length - 1].replace('.{format}', '');
16881703
this.basePath = this.api.basePath;
@@ -1761,7 +1776,7 @@ SwaggerResource.prototype.addApiDeclaration = function (response) {
17611776
this.consumes = response.consumes;
17621777
if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0)
17631778
this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
1764-
1779+
this.resourcePath = response.resourcePath;
17651780
this.addModels(response.models);
17661781
if (response.apis) {
17671782
for (var i = 0 ; i < response.apis.length; i++) {
@@ -2040,9 +2055,23 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
20402055
this.consumes = consumes;
20412056
this.produces = produces;
20422057
this.authorizations = typeof authorizations !== 'undefined' ? authorizations : resource.authorizations;
2043-
this.deprecated = (typeof deprecated === 'string' ? Boolean(deprecated) : deprecated);
2058+
this.deprecated = deprecated;
20442059
this['do'] = __bind(this['do'], this);
20452060

2061+
if(typeof this.deprecated === 'string') {
2062+
switch(this.deprecated.toLowerCase()) {
2063+
case 'true': case 'yes': case '1': {
2064+
this.deprecated = true;
2065+
break;
2066+
}
2067+
case 'false': case 'no': case '0': case null: {
2068+
this.deprecated = false;
2069+
break;
2070+
}
2071+
default: this.deprecated = Boolean(this.deprecated);
2072+
}
2073+
}
2074+
20462075
if (errors.length > 0) {
20472076
console.error('SwaggerOperation errors', errors, arguments);
20482077
this.resource.api.fail(errors);

lib/swagger-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/swagger-a.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ var Operation = function(parent, scheme, operationId, httpMethod, path, args, de
296296
this.description = args.description;
297297
this.useJQuery = parent.useJQuery;
298298

299+
if(typeof this.deprecated === 'string') {
300+
switch(this.deprecated.toLowerCase()) {
301+
case 'true': case 'yes': case '1': {
302+
this.deprecated = true;
303+
break;
304+
}
305+
case 'false': case 'no': case '0': case null: {
306+
this.deprecated = false;
307+
break;
308+
}
309+
default: this.deprecated = Boolean(this.deprecated);
310+
}
311+
}
312+
299313
if(definitions) {
300314
// add to global models
301315
var key;

src/js/swagger-compat.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ var SwaggerResource = function (resourceObj, api) {
161161
this.description = resourceObj.description;
162162
this.authorizations = (resourceObj.authorizations || {});
163163

164+
164165
var parts = this.path.split('/');
165166
this.name = parts[parts.length - 1].replace('.{format}', '');
166167
this.basePath = this.api.basePath;
@@ -239,7 +240,7 @@ SwaggerResource.prototype.addApiDeclaration = function (response) {
239240
this.consumes = response.consumes;
240241
if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0)
241242
this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
242-
243+
this.resourcePath = response.resourcePath;
243244
this.addModels(response.models);
244245
if (response.apis) {
245246
for (var i = 0 ; i < response.apis.length; i++) {
@@ -518,9 +519,23 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no
518519
this.consumes = consumes;
519520
this.produces = produces;
520521
this.authorizations = typeof authorizations !== 'undefined' ? authorizations : resource.authorizations;
521-
this.deprecated = (typeof deprecated === 'string' ? Boolean(deprecated) : deprecated);
522+
this.deprecated = deprecated;
522523
this['do'] = __bind(this['do'], this);
523524

525+
if(typeof this.deprecated === 'string') {
526+
switch(this.deprecated.toLowerCase()) {
527+
case 'true': case 'yes': case '1': {
528+
this.deprecated = true;
529+
break;
530+
}
531+
case 'false': case 'no': case '0': case null: {
532+
this.deprecated = false;
533+
break;
534+
}
535+
default: this.deprecated = Boolean(this.deprecated);
536+
}
537+
}
538+
524539
if (errors.length > 0) {
525540
console.error('SwaggerOperation errors', errors, arguments);
526541
this.resource.api.fail(errors);

test/compat/request.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ describe('request operations', function() {
4545
it('generate a get request', function() {
4646
var petApi = sample.pet;
4747
var req = petApi.getPetById({petId: 1}, {mock: true});
48-
4948
expect(petApi.operations.getPetById.deprecated).toBe(true);
5049
});
5150

test/operation.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,38 @@ describe('operations', function() {
248248
var op = new swagger.Operation({}, 'http', 'test', 'get', '/fantastic', { parameters: parameters });
249249
expect(op.parameters[0].signature).toEqual('Array[date-time]');
250250
});
251+
252+
it('should process the deprecated flag as boolean true', function() {
253+
var parameters = [
254+
{ in: 'query', name: 'year', type: 'array', items: {type: 'string', format: 'date-time'} }
255+
];
256+
var op = new swagger.Operation({}, 'http', 'test', 'get', '/fantastic', { parameters: parameters, deprecated: true });
257+
expect(op.deprecated).toEqual(true);
258+
});
259+
260+
it('should process the deprecated flag as boolean false', function() {
261+
var parameters = [
262+
{ in: 'query', name: 'year', type: 'array', items: {type: 'string', format: 'date-time'} }
263+
];
264+
var op = new swagger.Operation({}, 'http', 'test', 'get', '/fantastic', { parameters: parameters, deprecated: false });
265+
expect(op.deprecated).toEqual(false);
266+
});
267+
268+
it('should process the deprecated flag as string true', function() {
269+
var parameters = [
270+
{ in: 'query', name: 'year', type: 'array', items: {type: 'string', format: 'date-time'} }
271+
];
272+
var op = new swagger.Operation({}, 'http', 'test', 'get', '/fantastic', { parameters: parameters, deprecated: 'true' });
273+
expect(op.deprecated).toEqual(true);
274+
});
275+
276+
it('should process the deprecated flag as string false', function() {
277+
var parameters = [
278+
{ in: 'query', name: 'year', type: 'array', items: {type: 'string', format: 'date-time'} }
279+
];
280+
var op = new swagger.Operation({}, 'http', 'test', 'get', '/fantastic', { parameters: parameters, deprecated: 'false' });
281+
expect(op.deprecated).toEqual(false);
282+
});
251283
});
252284

253285
var quantityQP = {

0 commit comments

Comments
 (0)