Skip to content
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

fixed handling of arrays in query, optionally with enum #172

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/codegen.js
Expand Up @@ -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,
Expand All @@ -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(', ') + '\''
});
}

Expand Down
21 changes: 19 additions & 2 deletions lib/typescript.js
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions tests/generation.js
Expand Up @@ -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',
Expand Down