Skip to content

Commit

Permalink
Make sure encoded argument is a string
Browse files Browse the repository at this point in the history
Fixes #257
  • Loading branch information
luisfpg committed Feb 26, 2021
1 parent 8311db2 commit 31f4f47
Showing 1 changed file with 80 additions and 78 deletions.
158 changes: 80 additions & 78 deletions ng-swagger-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,23 @@ function ngSwaggerGen(options) {
}

$RefParser.bundle(options.swagger,
{ dereference: { circular: false },
resolve: { http: { timeout: options.timeout } } }).then(
data => {
doGenerate(data, options);
},
err => {
console.error(
`Error reading swagger location ${options.swagger}: ${err}`
);
{
dereference: { circular: false },
resolve: { http: { timeout: options.timeout } }
}).then(
data => {
doGenerate(data, options);
},
err => {
console.error(
`Error reading swagger location ${options.swagger}: ${err}`
);
process.exit(1);
}
).catch(function (error) {
console.error(`Error: ${error}`);
process.exit(1);
}
).catch(function (error) {
console.error(`Error: ${error}`);
process.exit(1);
});
});
}

/**
Expand Down Expand Up @@ -121,7 +123,7 @@ function doGenerate(swagger, options) {
if (swagger.swagger !== '2.0') {
console.error(
'Invalid swagger specification. Must be a 2.0. Currently ' +
swagger.swagger
swagger.swagger
);
process.exit(1);
}
Expand All @@ -145,7 +147,7 @@ function doGenerate(swagger, options) {
// Read the templates
var templates = {};
var files = fs.readdirSync(options.templates);
files.forEach(function(file, index) {
files.forEach(function (file, index) {
var pos = file.indexOf('.mustache');
if (pos >= 0) {
var fullFile = path.join(options.templates, file);
Expand All @@ -157,14 +159,14 @@ function doGenerate(swagger, options) {
var fallbackTemplates = path.join(__dirname, 'templates');
fs.readdirSync(fallbackTemplates)
.forEach(function (file) {
var pos = file.indexOf('.mustache');
if (pos >= 0) {
var fullFile = path.join(fallbackTemplates, file);
if (!(file.substr(0, pos) in templates)) {
templates[file.substr(0, pos)] = fs.readFileSync(fullFile, 'utf-8');
var pos = file.indexOf('.mustache');
if (pos >= 0) {
var fullFile = path.join(fallbackTemplates, file);
if (!(file.substr(0, pos) in templates)) {
templates[file.substr(0, pos)] = fs.readFileSync(fullFile, 'utf-8');
}
}
}
});
});

// Prepare the output folder
const modelsOutput = path.join(output, 'models');
Expand All @@ -176,7 +178,7 @@ function doGenerate(swagger, options) {
var generateEnumModule = options.enumModule !== false;

// Utility function to render a template and write it to a file
var generate = function(template, model, file) {
var generate = function (template, model, file) {
var code = Mustache.render(template, model, templates)
.replace(/[^\S\r\n]+$/gm, '');
fs.writeFileSync(file, code, 'UTF-8');
Expand Down Expand Up @@ -248,7 +250,7 @@ function doGenerate(swagger, options) {
var model = models[normalizeModelName(modelName)];
if (basename == model.modelFile + '.ts'
|| basename == model.modelExampleFile + '.ts'
&& model.modelExampleStr != null) {
&& model.modelExampleStr != null) {
ok = true;
break;
}
Expand Down Expand Up @@ -318,8 +320,8 @@ function doGenerate(swagger, options) {
var fullModuleFile = path.join(output, moduleFile + '.ts');
if (options.apiModule !== false) {
generate(templates.module, applyGlobals({
services: servicesArray
}),
services: servicesArray
}),
fullModuleFile);
} else if (removeStaleFiles) {
rmIfExists(fullModuleFile);
Expand All @@ -339,8 +341,8 @@ function doGenerate(swagger, options) {
}

generate(templates.configuration, applyGlobals({
rootUrl: rootUrl,
}),
rootUrl: rootUrl,
}),
path.join(output, configurationFile + '.ts')
);
}
Expand Down Expand Up @@ -416,8 +418,8 @@ function applyTagFilter(models, services, options) {
// This model is not used - remove it
console.info(
'Ignoring model ' +
modelName +
' because it was not used by any service'
modelName +
' because it was not used by any service'
);
delete models[normalizeModelName(modelName)];
}
Expand Down Expand Up @@ -583,7 +585,7 @@ function DependenciesResolver(models, ownType) {
/**
* Adds a candidate dependency
*/
DependenciesResolver.prototype.add = function(input) {
DependenciesResolver.prototype.add = function (input) {
let deps;
if (input.allTypes) {
deps = input.allTypes;
Expand All @@ -604,7 +606,7 @@ DependenciesResolver.prototype.add = function(input) {
/**
* Returns the resolved dependencies as a list of models
*/
DependenciesResolver.prototype.get = function() {
DependenciesResolver.prototype.get = function () {
return this.dependencies;
};

Expand Down Expand Up @@ -632,8 +634,8 @@ function processModels(swagger, options) {
properties = (model.allOf.find(val => !!val.properties) || {}).properties || {};
requiredProperties = (model.allOf.find(val => !!val.required) || {}).required || [];
if (parents && parents.length) {
simpleType = null;
enumValues = null;
simpleType = null;
enumValues = null;
}
} else if (model.type === 'string') {
enumValues = model.enum || [];
Expand Down Expand Up @@ -664,7 +666,7 @@ function processModels(swagger, options) {
properties = model.properties || {};
requiredProperties = model.required || [];
additionalPropertiesType = model.additionalProperties &&
(typeof model.additionalProperties === 'object' ? propertyType(model.additionalProperties) : 'any');
(typeof model.additionalProperties === 'object' ? propertyType(model.additionalProperties) : 'any');
} else {
simpleType = propertyType(model);
}
Expand Down Expand Up @@ -725,12 +727,12 @@ function processModels(swagger, options) {
model.modelParents = parents
.filter(parentName => !!parentName)
.map(parentName => {
// Make the parent be the actual model, not the name
var parentModel = models[normalizeModelName(parentName)];
// Make the parent be the actual model, not the name
var parentModel = models[normalizeModelName(parentName)];

// Append this model on the parent's subclasses
parentModel.modelSubclasses.push(model);
return parentModel;
// Append this model on the parent's subclasses
parentModel.modelSubclasses.push(model);
return parentModel;
});
model.modelParentNames = model.modelParents.map(
(parent, index) => ({
Expand Down Expand Up @@ -790,7 +792,7 @@ function processModels(swagger, options) {
* A special case is for inline objects. In this case, the result is "object".
*/
function removeBrackets(type, nullOrUndefinedOnly) {
if(typeof nullOrUndefinedOnly === "undefined") {
if (typeof nullOrUndefinedOnly === "undefined") {
nullOrUndefinedOnly = false;
}
if (typeof type === 'object') {
Expand All @@ -799,13 +801,13 @@ function removeBrackets(type, nullOrUndefinedOnly) {
}
return 'object';
}
else if(type.replace(/ /g, '') !== type) {
else if (type.replace(/ /g, '') !== type) {
return removeBrackets(type.replace(/ /g, ''));
}
else if(type.indexOf('null|') === 0) {
else if (type.indexOf('null|') === 0) {
return removeBrackets(type.substr('null|'.length));
}
else if(type.indexOf('undefined|') === 0) {
else if (type.indexOf('undefined|') === 0) {
// Not used currently, but robust code is better code :)
return removeBrackets(type.substr('undefined|'.length));
}
Expand Down Expand Up @@ -852,7 +854,7 @@ function propertyType(property) {
return type.length == 0 ? 'null' : type;
} else if (property['x-nullable']) {
return 'null | ' + propertyType(
Object.assign(property, {'x-nullable': undefined}));
Object.assign(property, { 'x-nullable': undefined }));
} else if (!property.type && (property.anyOf || property.oneOf)) {
let variants = (property.anyOf || property.oneOf).map(propertyType);
return {
Expand All @@ -868,7 +870,7 @@ function propertyType(property) {
toString: () => variants.join(' & ')
};
} else if (Array.isArray(property.type)) {
let variants = property.type.map(type => propertyType(Object.assign({}, property, {type})));
let variants = property.type.map(type => propertyType(Object.assign({}, property, { type })));
return {
allTypes: mergeTypes(...variants),
toString: () => variants.join(' | ')
Expand All @@ -892,8 +894,8 @@ function propertyType(property) {
if (Array.isArray(property.items)) { // support for tuples
if (!property.maxItems) return 'Array<any>'; // there is unable to define unlimited tuple in TypeScript
let minItems = property.minItems || 0,
maxItems = property.maxItems,
types = property.items.map(propertyType);
maxItems = property.maxItems,
types = property.items.map(propertyType);
types.push(property.additionalItems ? propertyType(property.additionalItems) : 'any');
let variants = [];
for (let i = minItems; i <= maxItems; i++) variants.push(types.slice(0, i));
Expand Down Expand Up @@ -928,15 +930,15 @@ function propertyType(property) {
if (memberCount++) def += ', ';
type = propertyType(prop);
allTypes.push(type);
let required = property.required && property.required.indexOf(name) >= 0;
def += name + (required ? ': ' : '?: ') + type;
let required = property.required && property.required.indexOf(name) >= 0;
def += name + (required ? ': ' : '?: ') + type;
}
}
if (property.additionalProperties) {
if (memberCount++) def += ', ';
type = typeof property.additionalProperties === 'object' ?
propertyType(property.additionalProperties) : 'any';
allTypes.push(type);
propertyType(property.additionalProperties) : 'any';
allTypes.push(type);
def += '[key: string]: ' + type;
}
def += '}';
Expand Down Expand Up @@ -1042,8 +1044,8 @@ function toPathExpression(operationParameters, paramsClass, path) {
const param = operationParameters.find(p => p.paramName === pName);
const paramName = param ? param.paramVar : pName;
return paramsClass ?
"${encodeURIComponent(params." + paramName + ")}" :
"${encodeURIComponent(" + paramName + ")}";
"${encodeURIComponent(String(params." + paramName + "))}" :
"${encodeURIComponent(String(" + paramName + "))}";
});
}

Expand Down Expand Up @@ -1103,25 +1105,25 @@ function operationId(given, method, url, allKnown) {
if (generate) {
console.warn(
"Operation '" +
method +
"' on '" +
url +
"' defines no operationId. Assuming '" +
id +
"'."
method +
"' on '" +
url +
"' defines no operationId. Assuming '" +
id +
"'."
);
} else if (duplicated) {
console.warn(
"Operation '" +
method +
"' on '" +
url +
"' defines a duplicated operationId: " +
given +
'. ' +
"Assuming '" +
id +
"'."
method +
"' on '" +
url +
"' defines a duplicated operationId: " +
given +
'. ' +
"Assuming '" +
id +
"'."
);
}
allKnown.add(id);
Expand All @@ -1139,7 +1141,7 @@ function processServices(swagger, models, options) {
var sortParams = options.sortParams || 'desc';
for (var url in swagger.paths) {
var path = swagger.paths[url];
var methodParameters = path.parameters;
var methodParameters = path.parameters;
for (var method in path || {}) {
var def = path[method];
if (!def || method == 'parameters') {
Expand Down Expand Up @@ -1319,13 +1321,13 @@ function processServices(swagger, models, options) {
operation.operationIsByteArray = actualType === 'ArrayBuffer';
operation.operationResponseType =
operation.operationIsFile ? 'blob' :
operation.operationIsByteArray ? 'arraybuffer' :
operation.operationIsVoid ||
operation.operationIsString ||
operation.operationIsNumber ||
operation.operationIsBoolean ||
operation.operationIsEnum ?
'text' : 'json';
operation.operationIsByteArray ? 'arraybuffer' :
operation.operationIsVoid ||
operation.operationIsString ||
operation.operationIsNumber ||
operation.operationIsBoolean ||
operation.operationIsEnum ?
'text' : 'json';
operation.operationIsUnknown = !(
operation.operationIsVoid ||
operation.operationIsString ||
Expand Down

0 comments on commit 31f4f47

Please sign in to comment.