From bef2dac0229d98057795910ccf30cb3a98d36ae5 Mon Sep 17 00:00:00 2001 From: wing328 Date: Sun, 13 Dec 2015 17:22:30 +0800 Subject: [PATCH] fix add route in sinatra --- .../src/main/resources/sinatra/Swaggering.rb | 6 ++++-- .../src/main/resources/sinatra/api.mustache | 2 +- .../src/main/resources/sinatra/my_app.mustache | 7 ++++--- samples/server/petstore/sinatra/api/pet_api.rb | 16 ++++++++-------- .../server/petstore/sinatra/api/store_api.rb | 8 ++++---- .../server/petstore/sinatra/api/user_api.rb | 18 +++++++++--------- .../server/petstore/sinatra/lib/swaggering.rb | 6 ++++-- samples/server/petstore/sinatra/my_app.rb | 4 ++++ 8 files changed, 38 insertions(+), 29 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/sinatra/Swaggering.rb b/modules/swagger-codegen/src/main/resources/sinatra/Swaggering.rb index 79aca1da2c9..14882a924d7 100644 --- a/modules/swagger-codegen/src/main/resources/sinatra/Swaggering.rb +++ b/modules/swagger-codegen/src/main/resources/sinatra/Swaggering.rb @@ -31,9 +31,10 @@ class Swaggering < Sinatra::Base end def self.add_route(method, path, swag={}, opts={}, &block) - fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + #fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + fullPath = path.gsub(/{(.*)}/, ':\1') - accepted = case method + accepted = case method.to_s.downcase when 'get' get(fullPath, opts, &block) true @@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base put(fullPath, opts, &block) true else + puts "Error adding route: #{method} #{fullPath}" false end diff --git a/modules/swagger-codegen/src/main/resources/sinatra/api.mustache b/modules/swagger-codegen/src/main/resources/sinatra/api.mustache index 7dffb94cdf0..27830feed31 100644 --- a/modules/swagger-codegen/src/main/resources/sinatra/api.mustache +++ b/modules/swagger-codegen/src/main/resources/sinatra/api.mustache @@ -3,7 +3,7 @@ require 'json' {{#operations}} {{#operation}} -MyApp.add_route('{{httpMethod}}', '{{path}}', { +MyApp.add_route('{{httpMethod}}', '{{basePathWithoutHost}}{{path}}', { "resourcePath" => "/{{baseName}}", "summary" => "{{{summary}}}", "nickname" => "{{nickname}}", diff --git a/modules/swagger-codegen/src/main/resources/sinatra/my_app.mustache b/modules/swagger-codegen/src/main/resources/sinatra/my_app.mustache index 8c4a16d7ff5..7d4e87adffc 100644 --- a/modules/swagger-codegen/src/main/resources/sinatra/my_app.mustache +++ b/modules/swagger-codegen/src/main/resources/sinatra/my_app.mustache @@ -7,6 +7,7 @@ class MyApp < Swaggering end end -{{#apis}} -require './lib/{{className}}.rb' -{{/apis}} +# include the api files +Dir["./api/*.rb"].each { |file| + require file +} diff --git a/samples/server/petstore/sinatra/api/pet_api.rb b/samples/server/petstore/sinatra/api/pet_api.rb index 5f995c531d8..7ccad2056b9 100644 --- a/samples/server/petstore/sinatra/api/pet_api.rb +++ b/samples/server/petstore/sinatra/api/pet_api.rb @@ -1,7 +1,7 @@ require 'json' -MyApp.add_route('PUT', '/pet', { +MyApp.add_route('PUT', '/v2/pet', { "resourcePath" => "/Pet", "summary" => "Update an existing pet", "nickname" => "update_pet", @@ -28,7 +28,7 @@ MyApp.add_route('PUT', '/pet', { end -MyApp.add_route('POST', '/pet', { +MyApp.add_route('POST', '/v2/pet', { "resourcePath" => "/Pet", "summary" => "Add a new pet to the store", "nickname" => "add_pet", @@ -55,7 +55,7 @@ MyApp.add_route('POST', '/pet', { end -MyApp.add_route('GET', '/pet/findByStatus', { +MyApp.add_route('GET', '/v2/pet/findByStatus', { "resourcePath" => "/Pet", "summary" => "Finds Pets by status", "nickname" => "find_pets_by_status", @@ -85,7 +85,7 @@ MyApp.add_route('GET', '/pet/findByStatus', { end -MyApp.add_route('GET', '/pet/findByTags', { +MyApp.add_route('GET', '/v2/pet/findByTags', { "resourcePath" => "/Pet", "summary" => "Finds Pets by tags", "nickname" => "find_pets_by_tags", @@ -115,7 +115,7 @@ MyApp.add_route('GET', '/pet/findByTags', { end -MyApp.add_route('GET', '/pet/{petId}', { +MyApp.add_route('GET', '/v2/pet/{petId}', { "resourcePath" => "/Pet", "summary" => "Find pet by ID", "nickname" => "get_pet_by_id", @@ -142,7 +142,7 @@ MyApp.add_route('GET', '/pet/{petId}', { end -MyApp.add_route('POST', '/pet/{petId}', { +MyApp.add_route('POST', '/v2/pet/{petId}', { "resourcePath" => "/Pet", "summary" => "Updates a pet in the store with form data", "nickname" => "update_pet_with_form", @@ -169,7 +169,7 @@ MyApp.add_route('POST', '/pet/{petId}', { end -MyApp.add_route('DELETE', '/pet/{petId}', { +MyApp.add_route('DELETE', '/v2/pet/{petId}', { "resourcePath" => "/Pet", "summary" => "Deletes a pet", "nickname" => "delete_pet", @@ -203,7 +203,7 @@ MyApp.add_route('DELETE', '/pet/{petId}', { end -MyApp.add_route('POST', '/pet/{petId}/uploadImage', { +MyApp.add_route('POST', '/v2/pet/{petId}/uploadImage', { "resourcePath" => "/Pet", "summary" => "uploads an image", "nickname" => "upload_file", diff --git a/samples/server/petstore/sinatra/api/store_api.rb b/samples/server/petstore/sinatra/api/store_api.rb index 24498387412..37938b304db 100644 --- a/samples/server/petstore/sinatra/api/store_api.rb +++ b/samples/server/petstore/sinatra/api/store_api.rb @@ -1,7 +1,7 @@ require 'json' -MyApp.add_route('GET', '/store/inventory', { +MyApp.add_route('GET', '/v2/store/inventory', { "resourcePath" => "/Store", "summary" => "Returns pet inventories by status", "nickname" => "get_inventory", @@ -21,7 +21,7 @@ MyApp.add_route('GET', '/store/inventory', { end -MyApp.add_route('POST', '/store/order', { +MyApp.add_route('POST', '/v2/store/order', { "resourcePath" => "/Store", "summary" => "Place an order for a pet", "nickname" => "place_order", @@ -48,7 +48,7 @@ MyApp.add_route('POST', '/store/order', { end -MyApp.add_route('GET', '/store/order/{orderId}', { +MyApp.add_route('GET', '/v2/store/order/{orderId}', { "resourcePath" => "/Store", "summary" => "Find purchase order by ID", "nickname" => "get_order_by_id", @@ -75,7 +75,7 @@ MyApp.add_route('GET', '/store/order/{orderId}', { end -MyApp.add_route('DELETE', '/store/order/{orderId}', { +MyApp.add_route('DELETE', '/v2/store/order/{orderId}', { "resourcePath" => "/Store", "summary" => "Delete purchase order by ID", "nickname" => "delete_order", diff --git a/samples/server/petstore/sinatra/api/user_api.rb b/samples/server/petstore/sinatra/api/user_api.rb index 98f50549ba1..7b890004891 100644 --- a/samples/server/petstore/sinatra/api/user_api.rb +++ b/samples/server/petstore/sinatra/api/user_api.rb @@ -1,7 +1,7 @@ require 'json' -MyApp.add_route('POST', '/user', { +MyApp.add_route('POST', '/v2/user', { "resourcePath" => "/User", "summary" => "Create user", "nickname" => "create_user", @@ -28,7 +28,7 @@ MyApp.add_route('POST', '/user', { end -MyApp.add_route('POST', '/user/createWithArray', { +MyApp.add_route('POST', '/v2/user/createWithArray', { "resourcePath" => "/User", "summary" => "Creates list of users with given input array", "nickname" => "create_users_with_array_input", @@ -55,7 +55,7 @@ MyApp.add_route('POST', '/user/createWithArray', { end -MyApp.add_route('POST', '/user/createWithList', { +MyApp.add_route('POST', '/v2/user/createWithList', { "resourcePath" => "/User", "summary" => "Creates list of users with given input array", "nickname" => "create_users_with_list_input", @@ -82,7 +82,7 @@ MyApp.add_route('POST', '/user/createWithList', { end -MyApp.add_route('GET', '/user/login', { +MyApp.add_route('GET', '/v2/user/login', { "resourcePath" => "/User", "summary" => "Logs user into the system", "nickname" => "login_user", @@ -122,7 +122,7 @@ MyApp.add_route('GET', '/user/login', { end -MyApp.add_route('GET', '/user/logout', { +MyApp.add_route('GET', '/v2/user/logout', { "resourcePath" => "/User", "summary" => "Logs out current logged in user session", "nickname" => "logout_user", @@ -142,7 +142,7 @@ MyApp.add_route('GET', '/user/logout', { end -MyApp.add_route('GET', '/user/{username}', { +MyApp.add_route('GET', '/v2/user/{username}', { "resourcePath" => "/User", "summary" => "Get user by user name", "nickname" => "get_user_by_name", @@ -154,7 +154,7 @@ MyApp.add_route('GET', '/user/{username}', { { "name" => "username", - "description" => "The name that needs to be fetched. Use user1 for testing. ", + "description" => "The name that needs to be fetched. Use user1 for testing.", "dataType" => "string", "paramType" => "path", }, @@ -169,7 +169,7 @@ MyApp.add_route('GET', '/user/{username}', { end -MyApp.add_route('PUT', '/user/{username}', { +MyApp.add_route('PUT', '/v2/user/{username}', { "resourcePath" => "/User", "summary" => "Updated user", "nickname" => "update_user", @@ -203,7 +203,7 @@ MyApp.add_route('PUT', '/user/{username}', { end -MyApp.add_route('DELETE', '/user/{username}', { +MyApp.add_route('DELETE', '/v2/user/{username}', { "resourcePath" => "/User", "summary" => "Delete user", "nickname" => "delete_user", diff --git a/samples/server/petstore/sinatra/lib/swaggering.rb b/samples/server/petstore/sinatra/lib/swaggering.rb index 79aca1da2c9..14882a924d7 100644 --- a/samples/server/petstore/sinatra/lib/swaggering.rb +++ b/samples/server/petstore/sinatra/lib/swaggering.rb @@ -31,9 +31,10 @@ class Swaggering < Sinatra::Base end def self.add_route(method, path, swag={}, opts={}, &block) - fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + #fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + fullPath = path.gsub(/{(.*)}/, ':\1') - accepted = case method + accepted = case method.to_s.downcase when 'get' get(fullPath, opts, &block) true @@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base put(fullPath, opts, &block) true else + puts "Error adding route: #{method} #{fullPath}" false end diff --git a/samples/server/petstore/sinatra/my_app.rb b/samples/server/petstore/sinatra/my_app.rb index 9f4d991d0f1..33376ef311d 100644 --- a/samples/server/petstore/sinatra/my_app.rb +++ b/samples/server/petstore/sinatra/my_app.rb @@ -7,3 +7,7 @@ class MyApp < Swaggering end end +# include the api files +Dir["./api/*.rb"].each { |file| + require file +}