Swagger Code Generation Prepare
Prepare swagger definition for template engine consumption.
This is adapted from swagger-js-codegen, only keeping the logic that prepares the object for template engine. This is intended to be used in conjunction with either mustache or handlebars.
Installation
npm install swagger-js-codegen
Simple Example
Prepare swagger, then use a handlebars template to generate output from the definition.
var Handlebars = require("handlebars");
var prep = require("swagger-codegen-prepare");
var swagger = require("./petstore.json");
var data = prep.SwaggerPrepare({ swagger: swagger, className: "PetStore" });
var source = [
'{{className}}',
'{{#methods}}',
' - {{methodName}} : {{summary}}',
'{{/methods}}',
].join("\n");
var template = Handlebars.compile(source);
var result = template(data);
console.log(result);
Example Code Generator
Example using the optional 'convertType' method and 'defaultType' parameter.
var Handlebars = require("handlebars");
var prep = require("swagger-codegen-prepare");
var swagger = require("./petstore.json");
var convertType = function(swaggerType, swagger) {
var name = "std::string";
var isRef = false;
if (swaggerType.schema) {
if (swaggerType.schema.type || swaggerType.schema.$ref) {
return convertType(swaggerType.schema);
}
}
if (swaggerType.type === 'array') {
isRef = true;
if (swaggerType.items) {
name = "std::vector< " + convertType(swaggerType.items, swagger).name + " >";
} else {
name = "std::vector< std::string >";
}
} else if (swaggerType.type === 'integer') {
name = "int";
} else {
if (swaggerType.type && swaggerType.type !== "string" && swaggerType.type !== "file") {
name = swaggerType.type;
}
if (swaggerType.$ref) {
name = swaggerType.$ref.substring(swaggerType.$ref.lastIndexOf('/') + 1);
isRef = true;
}
}
return { name: name, isRef: isRef };
};
var data = prep.SwaggerPrepare({ swagger: swagger, className: "PetStore", convertType: convertType, defaultType: { type: "string", __type: { "name": "std::string" } } });
var source = [
'class {{className}}_iface {',
'public:',
' {{#methods}}',
' //-------------------------',
' //{{&summary}}',
[
' virtual {{&return.__type.name}} {{&methodName}}(',
'{{#parameters}}',
'{{#notFirstParameter}} , {{/notFirstParameter}}',
'{{&__type.name}} {{#__type.isRef}}&{{/__type.isRef}}{{&name}}',
'{{/parameters}}',
') = NULL;'
].join(''),
'{{/methods}}',
'};',
'{{#definitions}}',
'{{#hasProperties}}',
'',
'class {{name}} {',
'public:',
'{{#properties}}',
' {{&__type.name}} {{&name}};',
'{{/properties}}',
'};',
'{{/hasProperties}}',
'{{/definitions}}',
].join("\n");
var template = Handlebars.compile(source);
var result = template(data);
console.log(result);
Running the above example with the Petstore swagger example produces the below content.
class PetStore_iface {
public:
virtual std::string addPet(Pet &body) = NULL;
virtual std::string updatePet(Pet &body) = NULL;
virtual std::string findPetsByStatus(std::vector< std::string > &status) = NULL;
virtual std::string findPetsByTags(std::vector< std::string > &tags) = NULL;
virtual std::string getPetById(int petId) = NULL;
virtual std::string updatePetWithForm(int petId , std::string name , std::string status) = NULL;
virtual std::string deletePet(std::string api_key , int petId) = NULL;
virtual std::string uploadFile(int petId , std::string additionalMetadata , std::string file) = NULL;
virtual std::string getInventory() = NULL;
virtual std::string placeOrder(Order &body) = NULL;
virtual std::string getOrderById(int orderId) = NULL;
virtual std::string deleteOrder(int orderId) = NULL;
virtual std::string createUser(User &body) = NULL;
virtual std::string createUsersWithArrayInput(std::vector< User > &body) = NULL;
virtual std::string createUsersWithListInput(std::vector< User > &body) = NULL;
virtual std::string loginUser(std::string username , std::string password) = NULL;
virtual std::string logoutUser() = NULL;
virtual std::string getUserByName(std::string username) = NULL;
virtual std::string updateUser(std::string username , User &body) = NULL;
virtual std::string deleteUser(std::string username) = NULL;
};
class Order {
public:
int id;
int petId;
int quantity;
std::string shipDate;
std::string status;
boolean complete;
};
class Category {
public:
int id;
std::string name;
};
class User {
public:
int id;
std::string username;
std::string firstName;
std::string lastName;
std::string email;
std::string password;
std::string phone;
int userStatus;
};
class Tag {
public:
int id;
std::string name;
};
class Pet {
public:
int id;
Category category;
std::string name;
std::vector< std::string > photoUrls;
std::vector< Tag > tags;
std::string status;
};
class ApiResponse {
public:
int code;
std::string type;
std::string message;
};