diff --git a/lib/codegen.js b/lib/codegen.js index b412f091..462871ef 100644 --- a/lib/codegen.js +++ b/lib/codegen.js @@ -64,18 +64,18 @@ var getViewForSwagger2 = function(opts, type){ } var secureTypes = []; if(swagger.securityDefinitions !== undefined || op.security !== undefined) { - var mergedSecurity = _.merge([], swagger.security, op.security).map(function(security){ - return Object.keys(security); + 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); + if(swagger.securityDefinitions) { + for(var sk in swagger.securityDefinitions) { + if(mergedSecurity.join(',').indexOf(sk) !== -1){ + secureTypes.push(swagger.securityDefinitions[sk].type); + } } - } } } - + var method = { path: path, className: opts.className, @@ -86,26 +86,26 @@ var getViewForSwagger2 = function(opts, type){ 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, + isSecureToken: secureTypes.indexOf('oauth2') !== -1, + isSecureApiKey: secureTypes.indexOf('apiKey') !== -1, + isSecureBasic: secureTypes.indexOf('basic') !== -1, parameters: [], headers: [] }; - if(method.isSecure && method.isSecureToken) { - data.isSecureToken = method.isSecureToken; - } - if(method.isSecure && method.isSecureApiKey) { - data.isSecureApiKey = method.isSecureApiKey; - } - if(method.isSecure && method.isSecureBasic) { - data.isSecureBasic = method.isSecureBasic; - } - var produces = op.produces || swagger.produces; + if(method.isSecure && method.isSecureToken) { + data.isSecureToken = method.isSecureToken; + } + if(method.isSecure && method.isSecureApiKey) { + data.isSecureApiKey = method.isSecureApiKey; + } + if(method.isSecure && method.isSecureBasic) { + data.isSecureBasic = method.isSecureBasic; + } + var produces = op.produces || swagger.produces; if(produces) { method.headers.push({ - name: 'Accept', - value: `'${produces.map(function(value) { return value; }).join(', ')}'`, + name: 'Accept', + value: '\'' + produces.map(function(value) { return value; }).join(', ') + '\'' }); } diff --git a/lib/typescript.js b/lib/typescript.js index ec949812..6a08361b 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -20,8 +20,25 @@ function convertType(swaggerType, swagger) { } else if (_.isString(swaggerType.$ref)) { typespec.tsType = 'ref'; typespec.target = swaggerType.$ref.substring(swaggerType.$ref.lastIndexOf('/') + 1); + } else if (swaggerType.type === 'array') { + if (swaggerType.in === 'query' && swaggerType.collectionFormat !== 'multi') { + // arrays in query parameters are merged by csv, ssv, tsv or pipes + typespec.tsType = 'string'; + if (swaggerType.hasOwnProperty('enum')) { + // doesn't affect the compiler, but useful for documentation + typespec.tsType += ' | ' + swaggerType.enum.map(function (str) { + return typeof str === 'string' ? '\'' + str + '\'' : JSON.stringify(str); + }).join(' | '); + typespec.isAtomic = true; + } + } else { + typespec.tsType = 'array'; + } + typespec.elementType = convertType(swaggerType.items); } else if (swaggerType.hasOwnProperty('enum')) { - typespec.tsType = swaggerType.enum.map(function(str) { return JSON.stringify(str); }).join(' | '); + typespec.tsType = swaggerType.enum.map(function(str) { + return typeof str === 'string' ? '\'' + str + '\'' : JSON.stringify(str); + }).join(' | '); typespec.isAtomic = true; typespec.isEnum = true; } else if (swaggerType.type === 'string') { @@ -74,9 +91,9 @@ function convertType(swaggerType, swagger) { typespec.isObject = typespec.tsType === 'object'; typespec.isArray = typespec.tsType === 'array'; typespec.isAtomic = typespec.isAtomic || _.includes(['string', 'number', 'boolean', 'any'], typespec.tsType); + typespec.isOptional = !swaggerType.required; return typespec; - } module.exports.convertType = convertType; diff --git a/tests/generation.js b/tests/generation.js index bbbd636e..4e1b8d32 100644 --- a/tests/generation.js +++ b/tests/generation.js @@ -63,11 +63,11 @@ list.forEach(function(file){ }); assert(typeof(result), 'string'); result = CodeGen.getReactCode({ - moduleName: 'Test', - className: 'Test', - swagger: swagger - }); - assert(typeof(result), 'string'); + moduleName: 'Test', + className: 'Test', + swagger: swagger + }); + assert(typeof(result), 'string'); result = CodeGen.getAngularCode({ moduleName: 'Test', className: 'Test',