diff --git a/lib/codegen.js b/lib/codegen.js index 54dbe7ef..9d844499 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -30,6 +30,23 @@ var getPathToMethodName = function(opts, m, path){ return m.toLowerCase() + result[0].toUpperCase() + result.substring(1); }; +var validateSecurity = function(securityDefinitions, security) { + var secureTypes = []; + if (securityDefinitions !== undefined && security !== undefined) { + var mergedSecurity = _.merge([], security).map(function(securityOptions) { + return Object.keys(securityOptions); + }); + if(securityDefinitions) { + for(var sk in securityDefinitions) { + if(mergedSecurity.join(',').indexOf(sk) !== -1) { + secureTypes = secureTypes.concat(securityDefinitions[sk].type); + } + } + } + } + return secureTypes; +}; + var getViewForSwagger2 = function(opts, type){ var swagger = opts.swagger; var methods = []; @@ -47,6 +64,8 @@ var getViewForSwagger2 = function(opts, type){ definitions: [] }; + var secureTypesGlobal = validateSecurity(swagger.securityDefinitions, swagger.security); + _.forEach(swagger.paths, function(api, path){ var globalParams = []; /** @@ -63,19 +82,8 @@ var getViewForSwagger2 = function(opts, type){ if(M === '' || authorizedMethods.indexOf(M) === -1) { return; } - var secureTypes = []; - if(swagger.securityDefinitions !== undefined || op.security !== undefined) { - var mergedSecurity = _.merge([], swagger.security, op.security).map(function(security){ - return Object.keys(security); - }); - if(swagger.securityDefinitions) { - for(var sk in swagger.securityDefinitions) { - if(mergedSecurity.join(',').indexOf(sk) !== -1){ - secureTypes.push(swagger.securityDefinitions[sk].type); - } - } - } - } + var secureTypes = secureTypesGlobal.length > 0 ? secureTypesGlobal : validateSecurity(swagger.securityDefinitions, op.security); + var methodName = (op.operationId ? normalizeName(op.operationId) : getPathToMethodName(opts, m, path)); // Make sure the method name is unique if(methods.indexOf(methodName) !== -1) { @@ -100,10 +108,10 @@ var getViewForSwagger2 = function(opts, type){ isPOST: M === 'POST', summary: op.description || op.summary, externalDocs: op.externalDocs, - isSecure: swagger.security !== undefined || op.security !== undefined, - isSecureToken: secureTypes.indexOf('oauth2') !== -1, - isSecureApiKey: secureTypes.indexOf('apiKey') !== -1, - isSecureBasic: secureTypes.indexOf('basic') !== -1, + isSecure: secureTypes.length > 0, + isSecureToken: secureTypes.indexOf('oauth2') !== -1, + isSecureApiKey: secureTypes.indexOf('apiKey') !== -1, + isSecureBasic: secureTypes.indexOf('basic') !== -1, parameters: [], headers: [] };