Merge pull request #1714 from wing328/ruby_sinatra_fix

[Ruby][Sinatra] fix bug with adding routes and add swagger.yaml
This commit is contained in:
wing328
2015-12-14 11:32:28 +08:00
11 changed files with 738 additions and 29 deletions

View File

@@ -1,5 +1,7 @@
package io.swagger.codegen.languages;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenType;
@@ -8,10 +10,13 @@ import io.swagger.codegen.SupportingFile;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.MapProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.Swagger;
import io.swagger.util.Yaml;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
@@ -73,6 +78,7 @@ public class SinatraServerCodegen extends DefaultCodegen implements CodegenConfi
supportingFiles.add(new SupportingFile("config.ru", "", "config.ru"));
supportingFiles.add(new SupportingFile("Gemfile", "", "Gemfile"));
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
supportingFiles.add(new SupportingFile("swagger.mustache","","swagger.yaml"));
}
public CodegenType getTag() {
@@ -213,5 +219,17 @@ public class SinatraServerCodegen extends DefaultCodegen implements CodegenConfi
return underscore(operationId);
}
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Swagger swagger = (Swagger)objs.get("swagger");
if(swagger != null) {
try {
objs.put("swagger-yaml", Yaml.mapper().writeValueAsString(swagger));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
return super.postProcessSupportingFileData(objs);
}
}

View File

@@ -26,14 +26,22 @@ class Swaggering < Sinatra::Base
cross_origin
Swaggering.to_resource_listing
}
# for swagger.yaml
get("/swagger.yaml") {
cross_origin
File.read("./swagger.yaml");
}
@@configuration ||= Configuration.new
yield(@@configuration) if block_given?
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 +55,7 @@ class Swaggering < Sinatra::Base
put(fullPath, opts, &block)
true
else
puts "Error adding route: #{method} #{fullPath}"
false
end

View File

@@ -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}}",

View File

@@ -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
}

View File

@@ -0,0 +1 @@
{{{swagger-yaml}}}