mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-03 14:10:56 +00:00
Organize generated Ruby code into a module
This commit is contained in:
parent
6a336f2e1e
commit
104bcc1f89
@ -7,7 +7,7 @@ import java.util.*;
|
|||||||
|
|
||||||
public class CodegenModel {
|
public class CodegenModel {
|
||||||
public String parent;
|
public String parent;
|
||||||
public String name, classname, description, classVarName, modelJson;
|
public String name, classname, importPath, description, classVarName, modelJson;
|
||||||
public String defaultValue;
|
public String defaultValue;
|
||||||
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>();
|
public List<CodegenProperty> vars = new ArrayList<CodegenProperty>();
|
||||||
public Set<String> imports = new HashSet<String>();
|
public Set<String> imports = new HashSet<String>();
|
||||||
|
@ -428,6 +428,7 @@ public class DefaultCodegen {
|
|||||||
m.name = name;
|
m.name = name;
|
||||||
m.description = escapeText(model.getDescription());
|
m.description = escapeText(model.getDescription());
|
||||||
m.classname = toModelName(name);
|
m.classname = toModelName(name);
|
||||||
|
m.importPath = toModelImport(name);
|
||||||
m.classVarName = toVarName(name);
|
m.classVarName = toVarName(name);
|
||||||
m.modelJson = Json.pretty(model);
|
m.modelJson = Json.pretty(model);
|
||||||
m.externalDocs = model.getExternalDocs();
|
m.externalDocs = model.getExternalDocs();
|
||||||
|
@ -165,6 +165,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator {
|
|||||||
operation.putAll(config.additionalProperties());
|
operation.putAll(config.additionalProperties());
|
||||||
operation.put("classname", config.toApiName(tag));
|
operation.put("classname", config.toApiName(tag));
|
||||||
operation.put("classVarName", config.toApiVarName(tag));
|
operation.put("classVarName", config.toApiVarName(tag));
|
||||||
|
operation.put("importPath", config.toApiImport(tag));
|
||||||
|
|
||||||
allOperations.add(new HashMap<String, Object>(operation));
|
allOperations.add(new HashMap<String, Object>(operation));
|
||||||
for (int i = 0; i < allOperations.size(); i++) {
|
for (int i = 0; i < allOperations.size(); i++) {
|
||||||
|
@ -8,10 +8,9 @@ import java.util.*;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||||
protected String invokerPackage = "com.wordnik.client";
|
protected String gemName = "swagger-client";
|
||||||
protected String groupId = "com.wordnik";
|
protected String moduleName = null;
|
||||||
protected String artifactId = "swagger-client";
|
protected String libFolder = "lib";
|
||||||
protected String artifactVersion = "1.0.0";
|
|
||||||
|
|
||||||
public CodegenType getTag() {
|
public CodegenType getTag() {
|
||||||
return CodegenType.CLIENT;
|
return CodegenType.CLIENT;
|
||||||
@ -25,10 +24,18 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return "Generates a Ruby client library.";
|
return "Generates a Ruby client library.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate Ruby module name from the gem name, e.g. use "SwaggerClient" for "swagger-client".
|
||||||
|
*/
|
||||||
|
public String generateModuleName() {
|
||||||
|
return camelize(gemName.replaceAll("[^\\w]+", "_"));
|
||||||
|
}
|
||||||
|
|
||||||
public RubyClientCodegen() {
|
public RubyClientCodegen() {
|
||||||
super();
|
super();
|
||||||
modelPackage = "models";
|
moduleName = generateModuleName();
|
||||||
apiPackage = "lib";
|
modelPackage = gemName + "/models";
|
||||||
|
apiPackage = gemName + "/api";
|
||||||
outputFolder = "generated-code/ruby";
|
outputFolder = "generated-code/ruby";
|
||||||
modelTemplateFiles.put("model.mustache", ".rb");
|
modelTemplateFiles.put("model.mustache", ".rb");
|
||||||
apiTemplateFiles.put("api.mustache", ".rb");
|
apiTemplateFiles.put("api.mustache", ".rb");
|
||||||
@ -46,10 +53,8 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
"if", "not", "return", "undef", "yield")
|
"if", "not", "return", "undef", "yield")
|
||||||
);
|
);
|
||||||
|
|
||||||
additionalProperties.put("invokerPackage", invokerPackage);
|
additionalProperties.put("gemName", gemName);
|
||||||
additionalProperties.put("groupId", groupId);
|
additionalProperties.put("moduleName", moduleName);
|
||||||
additionalProperties.put("artifactId", artifactId);
|
|
||||||
additionalProperties.put("artifactVersion", artifactVersion);
|
|
||||||
|
|
||||||
languageSpecificPrimitives.add("int");
|
languageSpecificPrimitives.add("int");
|
||||||
languageSpecificPrimitives.add("array");
|
languageSpecificPrimitives.add("array");
|
||||||
@ -64,15 +69,18 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
typeMapping.put("List", "array");
|
typeMapping.put("List", "array");
|
||||||
typeMapping.put("map", "map");
|
typeMapping.put("map", "map");
|
||||||
|
|
||||||
supportingFiles.add(new SupportingFile("swagger-client.gemspec.mustache", "", "swagger-client.gemspec"));
|
String baseFolder = "lib/" + gemName;
|
||||||
supportingFiles.add(new SupportingFile("swagger-client.mustache", "", "lib/swagger-client.rb"));
|
String swaggerFolder = baseFolder + "/swagger";
|
||||||
supportingFiles.add(new SupportingFile("swagger.mustache", "", "lib/swagger.rb"));
|
String modelFolder = baseFolder + "/models";
|
||||||
supportingFiles.add(new SupportingFile("monkey.mustache", "", "lib/monkey.rb"));
|
supportingFiles.add(new SupportingFile("swagger-client.gemspec.mustache", "", gemName + ".gemspec"));
|
||||||
supportingFiles.add(new SupportingFile("swagger/request.mustache", "", "lib/swagger/request.rb"));
|
supportingFiles.add(new SupportingFile("swagger-client.mustache", "lib", gemName + ".rb"));
|
||||||
supportingFiles.add(new SupportingFile("swagger/response.mustache", "", "lib/swagger/response.rb"));
|
supportingFiles.add(new SupportingFile("monkey.mustache", baseFolder, "monkey.rb"));
|
||||||
supportingFiles.add(new SupportingFile("swagger/version.mustache", "", "lib/swagger/version.rb"));
|
supportingFiles.add(new SupportingFile("swagger.mustache", baseFolder, "swagger.rb"));
|
||||||
supportingFiles.add(new SupportingFile("swagger/configuration.mustache", "", "lib/swagger/configuration.rb"));
|
supportingFiles.add(new SupportingFile("swagger/request.mustache", swaggerFolder, "request.rb"));
|
||||||
supportingFiles.add(new SupportingFile("base_object.mustache", "", "models/base_object.rb"));
|
supportingFiles.add(new SupportingFile("swagger/response.mustache", swaggerFolder, "response.rb"));
|
||||||
|
supportingFiles.add(new SupportingFile("swagger/version.mustache", swaggerFolder, "version.rb"));
|
||||||
|
supportingFiles.add(new SupportingFile("swagger/configuration.mustache", swaggerFolder, "configuration.rb"));
|
||||||
|
supportingFiles.add(new SupportingFile("base_object.mustache", modelFolder, "base_object.rb"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,11 +90,11 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String apiFileFolder() {
|
public String apiFileFolder() {
|
||||||
return outputFolder + "/" + apiPackage().replace('.', File.separatorChar);
|
return outputFolder + File.separatorChar + "lib" + File.separatorChar + gemName + File.separatorChar + "api";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String modelFileFolder() {
|
public String modelFileFolder() {
|
||||||
return outputFolder + "/" + modelPackage().replace('.', File.separatorChar);
|
return outputFolder + File.separatorChar + "lib" + File.separatorChar + gemName + File.separatorChar + "models";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -199,5 +207,14 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
return underscore(operationId);
|
return underscore(operationId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toModelImport(String name) {
|
||||||
|
return modelPackage() + "/" + toModelFilename(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toApiImport(String name) {
|
||||||
|
return apiPackage() + "/" + toApiFilename(name);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
require "uri"
|
require "uri"
|
||||||
|
|
||||||
|
module {{moduleName}}
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
class {{classname}}
|
class {{classname}}
|
||||||
basePath = "{{basePath}}"
|
basePath = "{{basePath}}"
|
||||||
# apiInvoker = APIInvoker
|
# apiInvoker = APIInvoker
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
@ -53,5 +54,6 @@ class {{classname}}
|
|||||||
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}} Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make{{/returnType}}
|
{{#returnContainer}}response.map {|response| {{/returnContainer}}obj = {{returnBaseType}}.new() and obj.build_from_hash(response){{#returnContainer}} }{{/returnContainer}}{{/returnType}}{{^returnType}} Swagger::Request.new(:{{httpMethod}}, path, {:params => query_params,:headers => header_params, :form_params => form_params, :body => post_body}).make{{/returnType}}
|
||||||
end
|
end
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
end
|
end
|
||||||
{{/operations}}
|
{{/operations}}
|
||||||
|
end
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
require_relative 'base_object'
|
require_relative 'base_object'
|
||||||
|
|
||||||
{{#models}}#{{description}}
|
module {{moduleName}}
|
||||||
{{#model}}class {{classname}} < BaseObject
|
{{#models}} # {{description}}
|
||||||
|
{{#model}} class {{classname}} < BaseObject
|
||||||
attr_accessor {{#vars}}:{{{name}}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{newline}}
|
attr_accessor {{#vars}}:{{{name}}}{{#hasMore}}, {{/hasMore}}{{/vars}}{{newline}}
|
||||||
# attribute mapping from ruby-style variable name to JSON key
|
# attribute mapping from ruby-style variable name to JSON key
|
||||||
def self.attribute_map
|
def self.attribute_map
|
||||||
@ -35,7 +36,7 @@ require_relative 'base_object'
|
|||||||
end
|
end
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
|
||||||
{{/model}}
|
{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
end
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
$:.push File.expand_path("../lib", __FILE__)
|
$:.push File.expand_path("../lib", __FILE__)
|
||||||
require "swagger/version"
|
require "{{gemName}}/swagger/version"
|
||||||
|
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = "{{artifactId}}"
|
s.name = "{{gemName}}"
|
||||||
s.version = Swagger::VERSION
|
s.version = {{moduleName}}::Swagger::VERSION
|
||||||
s.platform = Gem::Platform::RUBY
|
s.platform = Gem::Platform::RUBY
|
||||||
s.authors = ["Zeke Sikelianos", "Tony Tam"]
|
s.authors = ["Zeke Sikelianos", "Tony Tam"]
|
||||||
s.email = ["zeke@wordnik.com", "tony@wordnik.com"]
|
s.email = ["zeke@wordnik.com", "tony@wordnik.com"]
|
||||||
@ -12,8 +12,6 @@ Gem::Specification.new do |s|
|
|||||||
s.summary = %q{A ruby wrapper for the swagger APIs}
|
s.summary = %q{A ruby wrapper for the swagger APIs}
|
||||||
s.description = %q{This gem maps to a swagger API}
|
s.description = %q{This gem maps to a swagger API}
|
||||||
|
|
||||||
s.rubyforge_project = "{{artifactId}}"
|
|
||||||
|
|
||||||
s.add_dependency 'typhoeus', '>=0.2.1'
|
s.add_dependency 'typhoeus', '>=0.2.1'
|
||||||
s.add_dependency 'addressable', '>=2.2.4'
|
s.add_dependency 'addressable', '>=2.2.4'
|
||||||
s.add_dependency 'json', '>=1.4.6'
|
s.add_dependency 'json', '>=1.4.6'
|
||||||
|
@ -1,5 +1,26 @@
|
|||||||
require 'monkey'
|
# Swagger common files
|
||||||
require 'swagger'
|
require '{{gemName}}/monkey'
|
||||||
|
require '{{gemName}}/swagger'
|
||||||
|
require '{{gemName}}/swagger/configuration'
|
||||||
|
require '{{gemName}}/swagger/request'
|
||||||
|
require '{{gemName}}/swagger/response'
|
||||||
|
require '{{gemName}}/swagger/version'
|
||||||
|
|
||||||
Dir[File.join(File.dirname(__FILE__), "../lib/*.rb")].each {|file| require file if file !~ /swagger-client\.rb\z/ }
|
# Models
|
||||||
Dir[File.join(File.dirname(__FILE__), "../models/*.rb")].each {|file| require file }
|
{{#models}}
|
||||||
|
{{#model}}
|
||||||
|
require '{{importPath}}'
|
||||||
|
{{/model}}
|
||||||
|
{{/models}}
|
||||||
|
|
||||||
|
# APIs
|
||||||
|
{{#apiInfo}}
|
||||||
|
{{#apis}}
|
||||||
|
require '{{importPath}}'
|
||||||
|
{{/apis}}
|
||||||
|
{{/apiInfo}}
|
||||||
|
|
||||||
|
module {{moduleName}}
|
||||||
|
# Initialize the default configuration
|
||||||
|
Swagger.configuration ||= Swagger::Configuration.new
|
||||||
|
end
|
@ -1,15 +1,8 @@
|
|||||||
require 'monkey'
|
|
||||||
require 'swagger/configuration'
|
|
||||||
require 'swagger/request'
|
|
||||||
require 'swagger/response'
|
|
||||||
require 'swagger/version'
|
|
||||||
require 'logger'
|
require 'logger'
|
||||||
require 'json'
|
require 'json'
|
||||||
|
|
||||||
module Swagger
|
module {{moduleName}}
|
||||||
|
module Swagger
|
||||||
@configuration = Configuration.new
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :logger
|
attr_accessor :logger
|
||||||
|
|
||||||
@ -74,13 +67,12 @@ module Swagger
|
|||||||
response_body = request.response.body
|
response_body = request.response.body
|
||||||
Swagger.configuration.auth_token = response_body['token']
|
Swagger.configuration.auth_token = response_body['token']
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
class ServerError < StandardError
|
||||||
|
end
|
||||||
|
|
||||||
class ServerError < StandardError
|
class ClientError < StandardError
|
||||||
end
|
end
|
||||||
|
|
||||||
class ClientError < StandardError
|
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
module Swagger
|
module {{moduleName}}
|
||||||
|
module Swagger
|
||||||
class Configuration
|
class Configuration
|
||||||
require 'swagger/version'
|
|
||||||
|
|
||||||
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
|
attr_accessor :format, :api_key, :username, :password, :auth_token, :scheme, :host, :base_path, :user_agent, :logger, :inject_format, :force_ending_format, :camelize_params, :user_agent
|
||||||
|
|
||||||
# Defaults go in here..
|
# Defaults go in here..
|
||||||
@ -16,7 +14,6 @@ module Swagger
|
|||||||
@force_ending_format = false
|
@force_ending_format = false
|
||||||
@camelize_params = true
|
@camelize_params = true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
module Swagger
|
module {{moduleName}}
|
||||||
|
module Swagger
|
||||||
class Request
|
class Request
|
||||||
require 'uri'
|
require 'uri'
|
||||||
require 'addressable/uri'
|
require 'addressable/uri'
|
||||||
require 'typhoeus'
|
require 'typhoeus'
|
||||||
require "swagger/version"
|
|
||||||
|
|
||||||
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
|
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers, :form_params
|
||||||
|
|
||||||
|
|
||||||
# All requests must have an HTTP method and a path
|
# All requests must have an HTTP method and a path
|
||||||
# Optionals parameters are :params, :headers, :body, :format, :host
|
# Optionals parameters are :params, :headers, :body, :format, :host
|
||||||
#
|
|
||||||
def initialize(http_method, path, attributes={})
|
def initialize(http_method, path, attributes={})
|
||||||
attributes[:format] ||= Swagger.configuration.format
|
attributes[:format] ||= Swagger.configuration.format
|
||||||
attributes[:params] ||= {}
|
attributes[:params] ||= {}
|
||||||
@ -50,7 +47,6 @@ module Swagger
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Construct a base URL
|
# Construct a base URL
|
||||||
#
|
|
||||||
def url(options = {})
|
def url(options = {})
|
||||||
u = Addressable::URI.new(
|
u = Addressable::URI.new(
|
||||||
:scheme => Swagger.configuration.scheme,
|
:scheme => Swagger.configuration.scheme,
|
||||||
@ -69,7 +65,6 @@ module Swagger
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Iterate over the params hash, injecting any path values into the path string
|
# Iterate over the params hash, injecting any path values into the path string
|
||||||
#
|
|
||||||
# e.g. /word.{format}/{word}/entries => /word.json/cat/entries
|
# e.g. /word.{format}/{word}/entries => /word.json/cat/entries
|
||||||
def interpreted_path
|
def interpreted_path
|
||||||
p = self.path.dup
|
p = self.path.dup
|
||||||
@ -99,7 +94,6 @@ module Swagger
|
|||||||
|
|
||||||
# Massage the request body into a state of readiness
|
# Massage the request body into a state of readiness
|
||||||
# If body is a hash, camelize all keys then convert to a json string
|
# If body is a hash, camelize all keys then convert to a json string
|
||||||
#
|
|
||||||
def body=(value)
|
def body=(value)
|
||||||
if value.is_a?(Hash)
|
if value.is_a?(Hash)
|
||||||
value = value.inject({}) do |memo, (k,v)|
|
value = value.inject({}) do |memo, (k,v)|
|
||||||
@ -127,7 +121,6 @@ module Swagger
|
|||||||
|
|
||||||
# Construct a query string from the query-string-type params
|
# Construct a query string from the query-string-type params
|
||||||
def query_string
|
def query_string
|
||||||
|
|
||||||
# Iterate over all params,
|
# Iterate over all params,
|
||||||
# .. removing the ones that are part of the path itself.
|
# .. removing the ones that are part of the path itself.
|
||||||
# .. stringifying values so Addressable doesn't blow up.
|
# .. stringifying values so Addressable doesn't blow up.
|
||||||
@ -260,4 +253,5 @@ module Swagger
|
|||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
module Swagger
|
module {{moduleName}}
|
||||||
|
module Swagger
|
||||||
class Response
|
class Response
|
||||||
require 'json'
|
require 'json'
|
||||||
|
|
||||||
@ -65,6 +65,6 @@ module Swagger
|
|||||||
def pretty_headers
|
def pretty_headers
|
||||||
JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
|
JSON.pretty_generate(headers).gsub(/\n/, '<br/>')
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
module Swagger
|
module {{moduleName}}
|
||||||
VERSION = "4.06.08"
|
module Swagger
|
||||||
|
VERSION = "{{appVersion}}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user