diff --git a/bin/rails5-petstore-server.sh b/bin/rails5-petstore-server.sh index 28b3ca3069bb..9997a83f6a7b 100755 --- a/bin/rails5-petstore-server.sh +++ b/bin/rails5-petstore-server.sh @@ -25,7 +25,7 @@ then fi # if you've executed sbt assembly previously it will use that instead. -export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" +export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties -DdebugSupportingFiles" ags="$@ generate -t modules/swagger-codegen/src/main/resources/rails5 -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l rails5 -o samples/server/petstore/rails5" java $JAVA_OPTS -jar $executable $ags diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Rails5ServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Rails5ServerCodegen.java index cbf77338ab3c..4bb4c8e883ca 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Rails5ServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/Rails5ServerCodegen.java @@ -1,5 +1,7 @@ package io.swagger.codegen.languages; +import java.text.SimpleDateFormat; +import java.util.Date; import com.fasterxml.jackson.core.JsonProcessingException; import io.swagger.codegen.CodegenConfig; @@ -24,6 +26,7 @@ import org.slf4j.LoggerFactory; public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig { private static final Logger LOGGER = LoggerFactory.getLogger(Rails5ServerCodegen.class); + private static final SimpleDateFormat MIGRATE_FILE_NAME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); protected String gemName; protected String moduleName; @@ -57,12 +60,13 @@ public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig public Rails5ServerCodegen() { super(); - apiPackage = "app/controllers"; outputFolder = "generated-code" + File.separator + "rails5"; - - // no model - modelTemplateFiles.clear(); + apiPackage = "app/controllers"; apiTemplateFiles.put("controller.mustache", ".rb"); + + modelPackage = "app/models"; + modelTemplateFiles.put("model.mustache", ".rb"); + embeddedTemplateDir = templateDir = "rails5"; typeMapping.clear(); @@ -77,21 +81,21 @@ public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig "if", "not", "return", "undef", "yield") ); - languageSpecificPrimitives.add("int"); - languageSpecificPrimitives.add("array"); - languageSpecificPrimitives.add("map"); - languageSpecificPrimitives.add("string"); - languageSpecificPrimitives.add("DateTime"); - - typeMapping.put("long", "int"); - typeMapping.put("integer", "int"); - typeMapping.put("Array", "array"); - typeMapping.put("String", "string"); - typeMapping.put("List", "array"); - typeMapping.put("map", "map"); - //TODO binary should be mapped to byte array - // mapped to String as a workaround + typeMapping.put("string", "string"); + typeMapping.put("char", "string"); + typeMapping.put("int", "integer"); + typeMapping.put("integer", "integer"); + typeMapping.put("long", "integer"); + typeMapping.put("short", "integer"); + typeMapping.put("float", "float"); + typeMapping.put("double", "decimal"); + typeMapping.put("number", "float"); + typeMapping.put("date", "date"); + typeMapping.put("DateTime", "datetime"); + typeMapping.put("boolean", "boolean"); typeMapping.put("binary", "string"); + typeMapping.put("ByteArray", "string"); + typeMapping.put("UUID", "string"); // remove modelPackage and apiPackage added by default cliOptions.clear(); @@ -145,6 +149,7 @@ public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("secrets.yml", configFolder, "secrets.yml")); supportingFiles.add(new SupportingFile("spring.rb", configFolder, "spring.rb")); supportingFiles.add(new SupportingFile(".keep", migrateFolder, ".keep")); + supportingFiles.add(new SupportingFile("migrate.mustache", migrateFolder, "0_init_tables.rb")); supportingFiles.add(new SupportingFile("schema.rb", dbFolder, "schema.rb")); supportingFiles.add(new SupportingFile("seeds.rb", dbFolder, "seeds.rb")); supportingFiles.add(new SupportingFile(".keep", tasksFolder, ".keep")); @@ -204,24 +209,6 @@ public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig return super.getTypeDeclaration(p); } - @Override - public String getSwaggerType(Property p) { - String swaggerType = super.getSwaggerType(p); - String type = null; - if (typeMapping.containsKey(swaggerType)) { - type = typeMapping.get(swaggerType); - if (languageSpecificPrimitives.contains(type)) { - return type; - } - } else { - type = swaggerType; - } - if (type == null) { - return null; - } - return type; - } - @Override public String toDefaultValue(Property p) { return "null"; @@ -249,6 +236,16 @@ public class Rails5ServerCodegen extends DefaultCodegen implements CodegenConfig return name; } + @Override + public String getSwaggerType(Property p) { + String swaggerType = super.getSwaggerType(p); + String type = null; + if (typeMapping.containsKey(swaggerType)) { + return typeMapping.get(swaggerType); + } + return "string"; + } + @Override public String toParamName(String name) { // should be the same as variable name diff --git a/modules/swagger-codegen/src/main/resources/rails5/README.md b/modules/swagger-codegen/src/main/resources/rails5/README.md index c5cb90075a67..841b82ad6569 100644 --- a/modules/swagger-codegen/src/main/resources/rails5/README.md +++ b/modules/swagger-codegen/src/main/resources/rails5/README.md @@ -14,7 +14,7 @@ bundle install This sample was generated with the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. ``` -bin/rake db:create +bin/rake db:create db:migrate bin/rails s ``` diff --git a/modules/swagger-codegen/src/main/resources/rails5/migrate.mustache b/modules/swagger-codegen/src/main/resources/rails5/migrate.mustache new file mode 100644 index 000000000000..508b0ce7edef --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/rails5/migrate.mustache @@ -0,0 +1,15 @@ +=begin +{{> info}} +=end + +class InitTables < ActiveRecord::Migration + def change{{#models}}{{#model}} + create_table :{{classFilename}}, id: false do |t|{{#vars}}{{#isContainer}} + t.string :{{name}}{{/isContainer}}{{^isContainer}} + t.{{datatype}} :{{{name}}}{{/isContainer}}{{/vars}} + + t.timestamps + end +{{/model}}{{/models}} + end +end diff --git a/modules/swagger-codegen/src/main/resources/rails5/model.mustache b/modules/swagger-codegen/src/main/resources/rails5/model.mustache new file mode 100644 index 000000000000..edfe9fac56c8 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/rails5/model.mustache @@ -0,0 +1,12 @@ +=begin +{{> info}} +=end + +{{#models}}{{#model}} +class {{classname}} < ApplicationRecord +{{#requiredVars}} + validate_presence_of :{{name}} +{{/requiredVars}}{{#vars}}{{#isListContainer}} + serialize :{{name}}, Array{{/isListContainer}}{{#isMapContainer}} + serialize :{{name}}, Hash{{/isMapContainer}}{{/vars}} +end{{/model}}{{/models}} diff --git a/samples/server/petstore/rails5/README.md b/samples/server/petstore/rails5/README.md index c5cb90075a67..841b82ad6569 100644 --- a/samples/server/petstore/rails5/README.md +++ b/samples/server/petstore/rails5/README.md @@ -14,7 +14,7 @@ bundle install This sample was generated with the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. ``` -bin/rake db:create +bin/rake db:create db:migrate bin/rails s ``` diff --git a/samples/server/petstore/rails5/app/models/api_response.rb b/samples/server/petstore/rails5/app/models/api_response.rb new file mode 100644 index 000000000000..1ad59e6c7920 --- /dev/null +++ b/samples/server/petstore/rails5/app/models/api_response.rb @@ -0,0 +1,27 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class ApiResponse < ApplicationRecord + +end diff --git a/samples/server/petstore/rails5/app/models/category.rb b/samples/server/petstore/rails5/app/models/category.rb new file mode 100644 index 000000000000..3adaa4883095 --- /dev/null +++ b/samples/server/petstore/rails5/app/models/category.rb @@ -0,0 +1,27 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class Category < ApplicationRecord + +end diff --git a/samples/server/petstore/rails5/app/models/order.rb b/samples/server/petstore/rails5/app/models/order.rb new file mode 100644 index 000000000000..963eb8e18eda --- /dev/null +++ b/samples/server/petstore/rails5/app/models/order.rb @@ -0,0 +1,27 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class Order < ApplicationRecord + +end diff --git a/samples/server/petstore/rails5/app/models/pet.rb b/samples/server/petstore/rails5/app/models/pet.rb new file mode 100644 index 000000000000..da927b6260e5 --- /dev/null +++ b/samples/server/petstore/rails5/app/models/pet.rb @@ -0,0 +1,31 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class Pet < ApplicationRecord + validate_presence_of :name + validate_presence_of :photo_urls + + serialize :photo_urls, Array + serialize :tags, Array +end diff --git a/samples/server/petstore/rails5/app/models/tag.rb b/samples/server/petstore/rails5/app/models/tag.rb new file mode 100644 index 000000000000..096f132e2c2a --- /dev/null +++ b/samples/server/petstore/rails5/app/models/tag.rb @@ -0,0 +1,27 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class Tag < ApplicationRecord + +end diff --git a/samples/server/petstore/rails5/app/models/user.rb b/samples/server/petstore/rails5/app/models/user.rb new file mode 100644 index 000000000000..47f81df10899 --- /dev/null +++ b/samples/server/petstore/rails5/app/models/user.rb @@ -0,0 +1,27 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + + +class User < ApplicationRecord + +end diff --git a/samples/server/petstore/rails5/db/migrate/0_init_tables.rb b/samples/server/petstore/rails5/db/migrate/0_init_tables.rb new file mode 100644 index 000000000000..5fa060fb4dc6 --- /dev/null +++ b/samples/server/petstore/rails5/db/migrate/0_init_tables.rb @@ -0,0 +1,84 @@ +=begin +Swagger Petstore + +This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters. + +OpenAPI spec version: 1.0.0 +Contact: apiteam@swagger.io +Generated by: https://github.com/swagger-api/swagger-codegen.git + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +=end + +class InitTables < ActiveRecord::Migration + def change + create_table :api_response, id: false do |t| + t.integer :code + t.string :type + t.string :message + + t.timestamps + end + + create_table :category, id: false do |t| + t.integer :id + t.string :name + + t.timestamps + end + + create_table :order, id: false do |t| + t.integer :id + t.integer :pet_id + t.integer :quantity + t.datetime :ship_date + t.string :status + t.boolean :complete + + t.timestamps + end + + create_table :pet, id: false do |t| + t.integer :id + t.string :category + t.string :name + t.string :photo_urls + t.string :tags + t.string :status + + t.timestamps + end + + create_table :tag, id: false do |t| + t.integer :id + t.string :name + + t.timestamps + end + + create_table :user, id: false do |t| + t.integer :id + t.string :username + t.string :first_name + t.string :last_name + t.string :email + t.string :password + t.string :phone + t.integer :user_status + + t.timestamps + end + + end +end