grape-swagger/lib/grape-swagger.rb

434 lines
16 KiB
Ruby
Raw Normal View History

require 'kramdown'
require 'grape-swagger/version'
2012-07-19 16:37:46 +08:00
module Grape
class API
class << self
attr_reader :combined_routes, :combined_namespaces
2012-07-19 16:37:46 +08:00
2014-07-14 21:59:11 +08:00
def add_swagger_documentation(options = {})
2012-07-19 16:37:46 +08:00
documentation_class = create_documentation_class
2014-07-14 21:59:11 +08:00
documentation_class.setup({ target_class: self }.merge(options))
2012-07-19 16:37:46 +08:00
mount(documentation_class)
2013-06-18 21:56:15 +08:00
@combined_routes = {}
routes.each do |route|
2013-11-03 22:50:08 +08:00
route_match = route.route_path.split(route.route_prefix).last.match('\/([\w|-]*?)[\.\/\(]')
next if route_match.nil?
resource = route_match.captures.first
2013-06-18 21:56:15 +08:00
next if resource.empty?
resource.downcase!
@combined_routes[resource] ||= []
2014-07-14 21:59:11 +08:00
next if @@hide_documentation_path && route.route_path.include?(@@mount_path)
@combined_routes[resource] << route
2013-06-18 21:56:15 +08:00
end
@combined_namespaces = {}
endpoints.each do |endpoint|
ns = endpoint.settings.stack.last[:namespace]
@combined_namespaces[ns.space] = ns if ns
end
2012-07-19 16:37:46 +08:00
end
private
def create_documentation_class
2012-07-19 16:37:46 +08:00
Class.new(Grape::API) do
class << self
def name
@@class_name
2012-07-19 16:37:46 +08:00
end
end
def self.setup(options)
defaults = {
2014-07-14 21:59:11 +08:00
target_class: nil,
mount_path: '/swagger_doc',
base_path: nil,
api_version: '0.1',
markdown: false,
hide_documentation_path: false,
hide_format: false,
format: nil,
models: [],
info: {},
authorizations: nil,
root_base_path: true
}
2014-02-04 07:54:26 +08:00
options = defaults.merge(options)
2013-11-27 20:35:36 +08:00
target_class = options[:target_class]
@@mount_path = options[:mount_path]
2013-12-06 08:48:26 +08:00
@@class_name = options[:class_name] || options[:mount_path].gsub('/', '')
2013-11-27 20:35:36 +08:00
@@markdown = options[:markdown]
@@hide_format = options[:hide_format]
api_version = options[:api_version]
base_path = options[:base_path]
authorizations = options[:authorizations]
2013-11-28 04:09:29 +08:00
root_base_path = options[:root_base_path]
2013-11-27 20:43:44 +08:00
extra_info = options[:info]
@@models = options[:models] || []
2013-11-27 18:37:09 +08:00
@@hide_documentation_path = options[:hide_documentation_path]
if options[:format]
[:format, :default_format, :default_error_formatter].each do |method|
send(method, options[:format])
end
end
desc 'Swagger compatible API description'
get @@mount_path do
2013-12-06 08:48:26 +08:00
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
2014-02-04 07:54:26 +08:00
2014-07-14 21:59:11 +08:00
routes = target_class.combined_routes
namespaces = target_class.combined_namespaces
if @@hide_documentation_path
2014-07-14 21:59:11 +08:00
routes.reject! { |route, _value| "/#{route}/".index(parse_path(@@mount_path, nil) << '/') == 0 }
end
2013-12-06 08:50:13 +08:00
routes_array = routes.keys.map do |local_route|
next if routes[local_route].all?(&:route_hidden)
2014-02-04 07:54:26 +08:00
url_format = '.{format}' unless @@hide_format
description = namespaces[local_route] && namespaces[local_route].options[:desc]
description ||= "Operations about #{local_route.pluralize}"
{
2014-07-14 21:59:11 +08:00
path: "/#{local_route}#{url_format}",
description: description
}
2013-12-06 08:50:13 +08:00
end.compact
2013-11-27 18:37:09 +08:00
output = {
apiVersion: api_version,
2014-07-14 21:59:11 +08:00
swaggerVersion: '1.2',
produces: content_types_for(target_class),
apis: routes_array,
info: parse_info(extra_info)
}
2013-11-27 18:37:09 +08:00
2014-07-14 21:59:11 +08:00
output[:authorizations] = authorizations unless authorizations.nil? || authorizations.empty?
2013-11-27 18:37:09 +08:00
output
end
2014-07-14 21:59:11 +08:00
desc 'Swagger compatible API description for specific API', params: {
'name' => {
desc: 'Resource name of mounted API',
type: 'string',
required: true
}
2013-12-06 08:50:13 +08:00
}
get "#{@@mount_path}/:name" do
2013-11-27 18:37:09 +08:00
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
2014-02-04 07:54:26 +08:00
models = []
2014-07-14 21:59:11 +08:00
routes = target_class.combined_routes[params[:name]]
2014-02-04 07:54:26 +08:00
ops = routes.reject(&:route_hidden).group_by do |route|
parse_path(route.route_path, api_version)
end
apis = []
2014-07-14 21:59:11 +08:00
ops.each do |path, op_routes|
operations = op_routes.map do |route|
2014-02-04 07:54:26 +08:00
notes = as_markdown(route.route_notes)
2014-07-14 23:09:17 +08:00
2014-07-14 23:04:15 +08:00
http_codes = parse_http_codes(route.route_http_codes, models)
2014-02-04 07:54:26 +08:00
2014-07-14 21:59:11 +08:00
models << if @@models.present?
@@models
elsif route.route_entity.present?
route.route_entity
end
models = models.flatten.compact
2014-02-04 07:54:26 +08:00
operation = {
2014-07-14 21:59:11 +08:00
notes: notes.to_s,
summary: route.route_description || '',
nickname: route.route_nickname || (route.route_method + route.route_path.gsub(/[\/:\(\)\.]/, '-')),
method: route.route_method,
parameters: parse_header_params(route.route_headers) + parse_params(route.route_params, route.route_path, route.route_method),
type: 'void'
2014-02-04 07:54:26 +08:00
}
2014-07-14 21:59:11 +08:00
operation[:authorizations] = route.route_authorizations unless route.route_authorizations.nil? || route.route_authorizations.empty?
if operation[:parameters].any? { | param | param[:type] == 'File' }
operation.merge!(consumes: ['multipart/form-data'])
2014-05-28 23:42:27 +08:00
end
2014-07-14 21:59:11 +08:00
operation.merge!(responseMessages: http_codes) unless http_codes.empty?
2014-05-30 22:41:34 +08:00
if route.route_entity
type = parse_entity_name(route.route_entity)
if route.instance_variable_get(:@options)[:is_array]
2014-07-14 21:59:11 +08:00
operation.merge!(
'type' => 'array',
'items' => generate_typeref(type)
)
2014-05-30 22:41:34 +08:00
else
2014-07-14 21:59:11 +08:00
operation.merge!('type' => type)
2014-05-30 22:41:34 +08:00
end
end
2014-02-04 07:54:26 +08:00
operation
end.compact
apis << {
path: path,
operations: operations
}
2014-02-04 07:54:26 +08:00
end
api_description = {
apiVersion: api_version,
2014-07-14 21:59:11 +08:00
swaggerVersion: '1.2',
resourcePath: "/#{params[:name]}",
produces: content_types_for(target_class),
2014-02-04 07:54:26 +08:00
apis: apis
}
2013-11-27 21:29:40 +08:00
2014-07-14 21:59:11 +08:00
base_path = parse_base_path(base_path, request)
api_description[:basePath] = base_path if base_path && base_path.size > 0 && root_base_path != false
api_description[:models] = parse_entity_models(models) unless models.empty?
2014-07-14 21:59:11 +08:00
api_description[:authorizations] = authorizations if authorizations
2014-02-04 07:54:26 +08:00
api_description
end
2012-07-19 16:37:46 +08:00
end
helpers do
def as_markdown(description)
2014-07-14 21:59:11 +08:00
description && @@markdown ? Kramdown::Document.new(strip_heredoc(description), input: 'GFM', enable_coderay: false).to_html : description
end
def parse_params(params, path, method)
2013-12-06 08:48:26 +08:00
params ||= []
params.map do |param, value|
2014-05-28 23:42:27 +08:00
value[:type] = 'File' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
items = {}
raw_data_type = value.is_a?(Hash) ? (value[:type] || 'string').to_s : 'string'
2014-07-14 21:59:11 +08:00
data_type = case raw_data_type
when 'Boolean', 'Date', 'Integer', 'String'
raw_data_type.downcase
2014-07-14 21:59:11 +08:00
when 'BigDecimal'
'long'
when 'DateTime'
'dateTime'
when 'Numeric'
'double'
else
parse_entity_name(raw_data_type)
end
description = value.is_a?(Hash) ? value[:desc] || value[:description] : ''
required = value.is_a?(Hash) ? !!value[:required] : false
default_value = value.is_a?(Hash) ? value[:default] : nil
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
if value.is_a?(Hash) && value.key?(:param_type)
2014-07-14 21:59:11 +08:00
param_type = value[:param_type]
if is_array
2014-07-14 21:59:11 +08:00
items = { '$ref' => data_type }
data_type = 'array'
end
else
2014-07-14 21:59:11 +08:00
param_type = case
2014-05-13 04:15:23 +08:00
when path.include?(":#{param}")
'path'
2014-07-14 21:59:11 +08:00
when %w(POST PUT PATCH).include?(method)
if is_primitive?(data_type)
'form'
2014-05-31 00:21:23 +08:00
else
'body'
end
else
2014-05-13 04:15:23 +08:00
'query'
end
end
name = (value.is_a?(Hash) && value[:full_name]) || param
2014-02-04 07:54:26 +08:00
parsed_params = {
2014-07-14 21:59:11 +08:00
paramType: param_type,
name: name,
description: as_markdown(description),
2014-07-14 21:59:11 +08:00
type: data_type,
required: required,
allowMultiple: is_array
2013-12-06 08:48:26 +08:00
}
2014-07-14 21:59:11 +08:00
parsed_params.merge!(format: 'int32') if data_type == 'integer'
parsed_params.merge!(format: 'int64') if data_type == 'long'
parsed_params.merge!(items: items) if items.present?
parsed_params.merge!(defaultValue: default_value) if default_value
parsed_params
end
end
2014-02-04 07:54:26 +08:00
def content_types_for(target_class)
content_types = (target_class.settings[:content_types] || {}).values
2014-02-04 07:54:26 +08:00
if content_types.empty?
formats = [target_class.settings[:format], target_class.settings[:default_format]].compact.uniq
formats = Grape::Formatter::Base.formatters({}).keys if formats.empty?
2014-07-14 21:59:11 +08:00
content_types = Grape::ContentTypes::CONTENT_TYPES.select { |content_type, _mime_type| formats.include? content_type }.values
end
2014-02-04 07:54:26 +08:00
content_types.uniq
end
def parse_info(info)
{
contact: info[:contact],
description: as_markdown(info[:description]),
license: info[:license],
licenseUrl: info[:license_url],
termsOfServiceUrl: info[:terms_of_service_url],
title: info[:title]
2014-07-14 21:59:11 +08:00
}.delete_if { |_, value| value.blank? }
end
def parse_header_params(params)
2013-12-06 08:48:26 +08:00
params ||= []
2014-02-04 07:54:26 +08:00
2013-12-06 08:48:26 +08:00
params.map do |param, value|
2014-07-14 21:59:11 +08:00
data_type = 'String'
description = value.is_a?(Hash) ? value[:description] : ''
required = value.is_a?(Hash) ? !!value[:required] : false
default_value = value.is_a?(Hash) ? value[:default] : nil
2014-07-14 21:59:11 +08:00
param_type = 'header'
2014-02-04 07:54:26 +08:00
parsed_params = {
2014-07-14 21:59:11 +08:00
paramType: param_type,
2013-12-06 08:48:26 +08:00
name: param,
description: as_markdown(description),
2014-07-14 21:59:11 +08:00
type: data_type,
required: required
2013-12-06 08:48:26 +08:00
}
2014-07-14 21:59:11 +08:00
parsed_params.merge!(defaultValue: default_value) if default_value
parsed_params
2012-07-19 16:37:46 +08:00
end
end
2012-08-17 00:07:00 +08:00
def parse_path(path, version)
2012-07-19 16:37:46 +08:00
# adapt format to swagger format
2013-12-06 08:48:26 +08:00
parsed_path = path.gsub('(.:format)', @@hide_format ? '' : '.{format}')
# This is attempting to emulate the behavior of
# Rack::Mount::Strexp. We cannot use Strexp directly because
# all it does is generate regular expressions for parsing URLs.
# TODO: Implement a Racc tokenizer to properly generate the
# parsed path.
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
2012-08-17 00:07:00 +08:00
# add the version
version ? parsed_path.gsub('{version}', version) : parsed_path
2012-07-19 16:37:46 +08:00
end
2014-07-14 23:09:17 +08:00
def parse_entity_name(model)
if model.respond_to?(:entity_name)
model.entity_name
else
name = model.to_s
entity_parts = name.split('::')
entity_parts.reject! { |p| p == 'Entity' || p == 'Entities' }
entity_parts.join('::')
end
2014-02-04 09:15:23 +08:00
end
def parse_entity_models(models)
result = {}
2013-12-06 09:50:47 +08:00
models.each do |model|
name = parse_entity_name(model)
properties = {}
required = []
2014-02-04 07:54:26 +08:00
2013-12-06 09:50:47 +08:00
model.documentation.each do |property_name, property_info|
p = property_info.dup
2014-02-04 07:54:26 +08:00
2014-07-14 21:59:11 +08:00
required << property_name.to_s if p.delete(:required)
if p.delete(:is_array)
p[:items] = generate_typeref(p[:type])
2014-07-14 21:59:11 +08:00
p[:type] = 'array'
else
p.merge! generate_typeref(p.delete(:type))
end
2013-12-06 09:50:47 +08:00
# rename Grape Entity's "desc" to "description"
2014-07-14 21:59:11 +08:00
property_description = p.delete(:desc)
p[:description] = property_description if property_description
properties[property_name] = p
end
2014-02-04 07:54:26 +08:00
result[name] = {
2013-12-06 08:48:26 +08:00
id: model.instance_variable_get(:@root) || name,
2014-05-31 00:21:23 +08:00
properties: properties
}
2014-05-31 00:21:23 +08:00
result[name].merge!(required: required) unless required.empty?
end
2014-02-04 07:54:26 +08:00
result
end
def is_primitive?(type)
%w(integer long float double string byte boolean date dateTime).include? type
end
def generate_typeref(type)
if is_primitive? type
2014-07-14 21:59:11 +08:00
{ 'type' => type }
else
2014-07-14 21:59:11 +08:00
{ '$ref' => type }
end
end
2014-07-14 23:04:15 +08:00
def parse_http_codes(codes, models)
codes ||= {}
2014-07-14 23:04:15 +08:00
codes.map do |k, v, m|
models << m if m
2014-07-14 23:09:17 +08:00
http_code_hash = {
code: k,
2014-07-14 23:04:15 +08:00
message: v
}
2014-07-14 23:09:17 +08:00
http_code_hash[:responseModel] = parse_entity_name(m) if m
http_code_hash
end
end
2013-12-06 08:48:26 +08:00
def try(*args, &block)
if args.empty? && block_given?
yield self
2013-12-06 08:48:26 +08:00
elsif respond_to?(args.first)
public_send(*args, &block)
end
end
def strip_heredoc(string)
indent = string.scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
string.gsub(/^[ \t]{#{indent}}/, '')
end
2013-06-19 16:35:50 +08:00
def parse_base_path(base_path, request)
if base_path.is_a?(Proc)
base_path.call(request)
elsif base_path.is_a?(String)
URI(base_path).relative? ? URI.join(request.base_url, base_path).to_s : base_path
else
request.base_url
end
2013-06-19 16:35:50 +08:00
end
2012-07-19 16:37:46 +08:00
end
end
end
end
end
end