From 375c26ccb3690e3f588d8627b51ac173c596ca61 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 2 Aug 2018 13:52:34 +0800 Subject: [PATCH] add option to select db adapter in ror (#711) --- .../codegen/CodegenConstants.java | 4 +++ .../codegen/DefaultGenerator.java | 2 +- .../languages/RubyOnRailsServerCodegen.java | 31 ++++++++++++++++++- .../resources/ruby-on-rails-server/Gemfile | 8 ++++- .../ruby-on-rails-server/database.yml | 26 ++++++++++++++++ .../ruby-on-rails/.openapi-generator/VERSION | 2 +- samples/server/petstore/ruby-on-rails/Gemfile | 8 ++++- .../ruby-on-rails/config/database.yml | 26 ++++++++++++++++ 8 files changed, 102 insertions(+), 5 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 4be60e75a51..9aa86e73645 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -257,4 +257,8 @@ public class CodegenConstants { public static final String DOCEXTENSION = "docExtension"; public static final String DOCEXTENSION_DESC = "The extension of the generated documentation files, defaults to markdown, .md"; + + public static final String DATABASE_ADAPTER = "databaseAdapter"; + public static final String DATABASE_ADAPTER_DESC = "The adapter for database (e.g. mysql, sqlite). Default: sqlite"; + } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java index 7bd9ff212bd..54455f49c89 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java @@ -95,7 +95,7 @@ public class DefaultGenerator extends AbstractGenerator implements Generator { /** * Programmatically disable the output of .openapi-generator/VERSION, .openapi-generator-ignore, - * or other metadata files used by Swagger Codegen. + * or other metadata files used by OpenAPI Generator. * * @param generateMetadata true: enable outputs, false: disable outputs */ diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java index 3a8752df73f..7271a69522b 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RubyOnRailsServerCodegen.java @@ -19,7 +19,10 @@ package org.openapitools.codegen.languages; import java.text.SimpleDateFormat; + +import org.openapitools.codegen.CliOption; import org.openapitools.codegen.CodegenType; +import org.openapitools.codegen.CodegenConstants; import org.openapitools.codegen.SupportingFile; import io.swagger.v3.oas.models.media.*; @@ -64,6 +67,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen { protected String pidFolder = tmpFolder + File.separator + "pids"; protected String socketsFolder = tmpFolder + File.separator + "sockets"; protected String vendorFolder = "vendor"; + protected String databaseAdapter = "sqlite"; + public RubyOnRailsServerCodegen() { super(); @@ -87,6 +92,9 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen { // remove modelPackage and apiPackage added by default cliOptions.clear(); + + cliOptions.add(new CliOption(CodegenConstants.DATABASE_ADAPTER, CodegenConstants.DATABASE_ADAPTER_DESC). + defaultValue("sqlite")); } @Override @@ -97,6 +105,23 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen { //setModelPackage("models"); setApiPackage("app/controllers"); + // determine which db adapter to use + if (additionalProperties.containsKey(CodegenConstants.DATABASE_ADAPTER)) { + setDatabaseAdapter((String) additionalProperties.get(CodegenConstants.DATABASE_ADAPTER)); + } else { + // not set, pass the default value to template + additionalProperties.put(CodegenConstants.DATABASE_ADAPTER, databaseAdapter); + } + + if ("sqlite".equals(databaseAdapter)) { + additionalProperties.put("isDBSQLite", Boolean.TRUE); + } else if ("mysql".equals(databaseAdapter)) { + additionalProperties.put("isDBMySQL", Boolean.TRUE); + } else { + LOGGER.warn("Unknown database {}. Defaul to 'sqlite'.", databaseAdapter); + additionalProperties.put("isDBSQLite", Boolean.TRUE); + } + supportingFiles.add(new SupportingFile("Gemfile", "", "Gemfile")); supportingFiles.add(new SupportingFile("README.md", "", "README.md")); supportingFiles.add(new SupportingFile("Rakefile", "", "Rakefile")); @@ -230,7 +255,7 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen { if (name.length() == 0) { return "ApiController"; } - // e.g. phone_number_api => PhoneNumberApi + // e.g. phone_number_controller => PhoneNumberController return camelize(name) + "Controller"; } @@ -239,4 +264,8 @@ public class RubyOnRailsServerCodegen extends AbstractRubyCodegen { generateYAMLSpecFile(objs); return super.postProcessSupportingFileData(objs); } + + public void setDatabaseAdapter(String databaseAdapter) { + this.databaseAdapter = databaseAdapter; + } } diff --git a/modules/openapi-generator/src/main/resources/ruby-on-rails-server/Gemfile b/modules/openapi-generator/src/main/resources/ruby-on-rails-server/Gemfile index a46942406d1..ea808507a70 100644 --- a/modules/openapi-generator/src/main/resources/ruby-on-rails-server/Gemfile +++ b/modules/openapi-generator/src/main/resources/ruby-on-rails-server/Gemfile @@ -3,8 +3,14 @@ source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0' +{{#isDBSQLite}} +# Use sqlite as the database for Active Record +gem 'sqlite3', '~> 1.3' +{{/isDBSQLite}} +{{#isDBMySQL}} # Use mysql as the database for Active Record gem 'mysql2', '>= 0.3.18', '< 0.5' +{{/isDBMySQL}} # Use Puma as the app server gem 'puma', '~> 3.0' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder @@ -33,4 +39,4 @@ group :development do end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/ruby-on-rails-server/database.yml b/modules/openapi-generator/src/main/resources/ruby-on-rails-server/database.yml index e536d9f37dc..170b64b24fb 100644 --- a/modules/openapi-generator/src/main/resources/ruby-on-rails-server/database.yml +++ b/modules/openapi-generator/src/main/resources/ruby-on-rails-server/database.yml @@ -1,3 +1,4 @@ +{{#isDBMySQL}} # MySQL. Versions 5.0 and up are supported. # # Install the MySQL driver @@ -52,3 +53,28 @@ production: database: api_demo_production username: api_demo password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %> +{{/isDBMySQL}} +{{#isDBSQLite}} +# SQLite version 3.x +# gem install sqlite3-ruby (not necessary on OS X Leopard) +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + adapter: sqlite3 + database: db/test.sqlite3 + pool: 5 + timeout: 5000 + +production: + adapter: sqlite3 + database: db/production.sqlite3 + pool: 5 + timeout: 5000 +{{/isDBSQLite}} \ No newline at end of file diff --git a/samples/server/petstore/ruby-on-rails/.openapi-generator/VERSION b/samples/server/petstore/ruby-on-rails/.openapi-generator/VERSION index dde25ef08e8..4395ff59232 100644 --- a/samples/server/petstore/ruby-on-rails/.openapi-generator/VERSION +++ b/samples/server/petstore/ruby-on-rails/.openapi-generator/VERSION @@ -1 +1 @@ -3.1.1-SNAPSHOT \ No newline at end of file +3.2.0-SNAPSHOT \ No newline at end of file diff --git a/samples/server/petstore/ruby-on-rails/Gemfile b/samples/server/petstore/ruby-on-rails/Gemfile index a46942406d1..ea808507a70 100644 --- a/samples/server/petstore/ruby-on-rails/Gemfile +++ b/samples/server/petstore/ruby-on-rails/Gemfile @@ -3,8 +3,14 @@ source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0' +{{#isDBSQLite}} +# Use sqlite as the database for Active Record +gem 'sqlite3', '~> 1.3' +{{/isDBSQLite}} +{{#isDBMySQL}} # Use mysql as the database for Active Record gem 'mysql2', '>= 0.3.18', '< 0.5' +{{/isDBMySQL}} # Use Puma as the app server gem 'puma', '~> 3.0' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder @@ -33,4 +39,4 @@ group :development do end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] \ No newline at end of file diff --git a/samples/server/petstore/ruby-on-rails/config/database.yml b/samples/server/petstore/ruby-on-rails/config/database.yml index e536d9f37dc..170b64b24fb 100644 --- a/samples/server/petstore/ruby-on-rails/config/database.yml +++ b/samples/server/petstore/ruby-on-rails/config/database.yml @@ -1,3 +1,4 @@ +{{#isDBMySQL}} # MySQL. Versions 5.0 and up are supported. # # Install the MySQL driver @@ -52,3 +53,28 @@ production: database: api_demo_production username: api_demo password: <%= ENV['API_DEMO_DATABASE_PASSWORD'] %> +{{/isDBMySQL}} +{{#isDBSQLite}} +# SQLite version 3.x +# gem install sqlite3-ruby (not necessary on OS X Leopard) +development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 5 + timeout: 5000 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + adapter: sqlite3 + database: db/test.sqlite3 + pool: 5 + timeout: 5000 + +production: + adapter: sqlite3 + database: db/production.sqlite3 + pool: 5 + timeout: 5000 +{{/isDBSQLite}} \ No newline at end of file