Skip to content

use lodash to check types in resolver.js #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
397 changes: 244 additions & 153 deletions browser/swagger-client.js

Large diffs are not rendered by default.

395 changes: 243 additions & 152 deletions browser/swagger-client.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var SwaggerAuthorizations = module.exports.SwaggerAuthorizations = function (aut
*
*/
SwaggerAuthorizations.prototype.add = function (name, auth) {
if(name && typeof name === 'object') {
if(_.isObject(name)) {
for (var key in name) {
this.authz[key] = name[key];
}
Expand Down
3 changes: 2 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var _ = {
forEach: require('lodash-compat/collection/forEach'),
indexOf: require('lodash-compat/array/indexOf'),
isArray: require('lodash-compat/lang/isArray'),
isObject: require('lodash-compat/lang/isObject'),
isFunction: require('lodash-compat/lang/isFunction'),
isPlainObject: require('lodash-compat/lang/isPlainObject'),
isUndefined: require('lodash-compat/lang/isUndefined')
Expand Down Expand Up @@ -108,7 +109,7 @@ SwaggerClient.prototype.initialize = function (url, options) {

if (typeof url === 'string') {
this.url = url;
} else if (typeof url === 'object') {
} else if (_.isObject(url)) {
options = url;
this.url = options.url;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var helpers = require('./helpers');
var jQuery = require('jquery');
var request = require('superagent');
var jsyaml = require('js-yaml');
var _ = {
isObject: require('lodash-compat/lang/isObject')
};

/*
* JQueryHttpClient is a light-weight, node or browser HTTP client
Expand Down Expand Up @@ -49,7 +52,7 @@ SwaggerHttp.prototype.execute = function (obj, opts) {
};


if (obj && typeof obj.body === 'object') {
if (_.isObject(obj) && _.isObject(obj.body)) {
// special processing for file uploads via jquery
if (obj.body.type && obj.body.type === 'formData'){
obj.contentType = false;
Expand Down
17 changes: 11 additions & 6 deletions lib/resolver.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';

var SwaggerHttp = require('./http');
var _ = {
isObject: require('lodash-compat/lang/isObject'),
isArray: require('lodash-compat/lang/isArray')
};


/**
/**
* Resolves a spec's remote references
*/
var Resolver = module.exports = function () {};
Expand Down Expand Up @@ -80,7 +85,7 @@ Resolver.prototype.resolve = function (spec, arg1, arg2, arg3) {
var response = operation.responses[responseCode];
location = '/paths' + name + '/' + method + '/responses/' + responseCode;

if(typeof response === 'object') {
if(_.isObject(response)) {
if(response.$ref) {
// response reference
this.resolveInline(root, spec, response, resolutionTable, unresolvedRefs, location);
Expand Down Expand Up @@ -346,7 +351,7 @@ Resolver.prototype.getRefs = function(spec, obj) {
if(key === '$ref' && typeof item === 'string') {
output[item] = null;
}
else if(typeof item === 'object') {
else if(_.isObject(item)) {
var o = this.getRefs(item);
for(var k in o) {
output[k] = null;
Expand All @@ -370,7 +375,7 @@ Resolver.prototype.retainRoot = function(obj, root) {
obj[key] = item;
}
}
else if(typeof item === 'object') {
else if(_.isObject(item)) {
this.retainRoot(item, root);
}
}
Expand Down Expand Up @@ -476,7 +481,7 @@ Resolver.prototype.resolveAllOf = function(spec, obj, depth) {
var item = obj[key];
if(item && typeof item.allOf !== 'undefined') {
var allOf = item.allOf;
if(Array.isArray(allOf)) {
if(_.isArray(allOf)) {
var output = {};
output['x-composed'] = true;
for(var i = 0; i < allOf.length; i++) {
Expand Down Expand Up @@ -526,7 +531,7 @@ Resolver.prototype.resolveAllOf = function(spec, obj, depth) {
obj[key] = output;
}
}
if(typeof item === 'object') {
if(_.isObject(item)) {
this.resolveAllOf(spec, item, depth + 1);
}
}
Expand Down
13 changes: 8 additions & 5 deletions lib/spec-converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

var SwaggerHttp = require('./http');
var _ = {
isObject: require('lodash-compat/lang/isObject')
};

var SwaggerSpecConverter = module.exports = function () {
this.errors = [];
Expand Down Expand Up @@ -105,7 +108,7 @@ SwaggerSpecConverter.prototype.declaration = function(obj, swagger) {
}

// build a mapping of id to name for 1.0 model resolutions
if(typeof obj === 'object') {
if(_.isObject(obj)) {
for(name in obj.models) {
var existingModel = obj.models[name];
var key = (existingModel.id || name);
Expand All @@ -125,7 +128,7 @@ SwaggerSpecConverter.prototype.declaration = function(obj, swagger) {
};

SwaggerSpecConverter.prototype.models = function(obj, swagger) {
if(typeof obj !== 'object') {
if(!_.isObject(obj)) {
return;
}
var name;
Expand Down Expand Up @@ -267,7 +270,7 @@ SwaggerSpecConverter.prototype.operations = function(path, resourcePath, obj, re
};

SwaggerSpecConverter.prototype.responseMessages = function(operation, existingOperation) {
if(typeof existingOperation !== 'object') {
if(!_.isObject(existingOperation)) {
return;
}
// build default response from the operation (1.x)
Expand Down Expand Up @@ -310,7 +313,7 @@ SwaggerSpecConverter.prototype.responseMessages = function(operation, existingOp

SwaggerSpecConverter.prototype.authorizations = function(obj) {
// TODO
if(typeof obj !== 'object') {
if(!_.isObject(obj)) {
return;
}
};
Expand Down Expand Up @@ -363,7 +366,7 @@ SwaggerSpecConverter.prototype.parameters = function(operation, obj) {
};

SwaggerSpecConverter.prototype.dataType = function(source, target) {
if(typeof source !== 'object') {
if(!_.isObject(source)) {
return;
}

Expand Down
11 changes: 6 additions & 5 deletions lib/types/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var _ = {
cloneDeep: require('lodash-compat/lang/cloneDeep'),
isUndefined: require('lodash-compat/lang/isUndefined'),
isEmpty: require('lodash-compat/lang/isEmpty')
isEmpty: require('lodash-compat/lang/isEmpty'),
isObject: require('lodash-compat/lang/isObject')
};
var helpers = require('../helpers');
var Model = require('./model');
Expand Down Expand Up @@ -585,7 +586,7 @@ Operation.prototype.getModelSampleJSON = function (type, models) {

if (typeof sampleJson === 'string') {
return sampleJson;
} else if (typeof sampleJson === 'object') {
} else if (_.isObject(sampleJson)) {
var t = sampleJson;

if (sampleJson instanceof Array && sampleJson.length > 0) {
Expand Down Expand Up @@ -619,7 +620,7 @@ Operation.prototype.execute = function (arg1, arg2, arg3, arg4, parent) {
var args = arg1 || {};
var opts = {}, success, error;

if (typeof arg2 === 'object') {
if (_.isObject(arg2)) {
opts = arg2;
success = arg3;
error = arg4;
Expand Down Expand Up @@ -795,7 +796,7 @@ Operation.prototype.setContentTypes = function (args, opts) {

/**
* Returns true if the request accepts header matches anything in this.produces.
* If this.produces contains * / *, ignore the accept header.
* If this.produces contains * / *, ignore the accept header.
* @param {string=} accepts The client request accept header.
* @return {boolean}
*/
Expand Down Expand Up @@ -827,7 +828,7 @@ Operation.prototype.asCurl = function (args) {
if (obj.body) {
var body;

if (typeof obj.body === 'object') {
if (_.isObject(obj.body)) {
body = JSON.stringify(obj.body);
} else {
body = obj.body;
Expand Down