Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrades to grape 0.14.x; grape-entity 0.5.x #336

Merged
merged 4 commits into from
Feb 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ AllCops:
- vendor/**/*
- spec/**/*
- swagger_spec1.2/**/*
- example/**/*

inherit_from: .rubocop_todo.yml
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 142
Max: 333

# Offense count: 6
Metrics/CyclomaticComplexity:
Expand Down
17 changes: 8 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ language: ruby
sudo: false

rvm:
- 2.2.2
- 2.1.1
- 2.0.0
- rbx-2.2.10
- 2.3.0
- 2.2.3
- 2.1.7
- rbx-2
- jruby-19mode

matrix:
allow_failures:
- rvm: rbx-2
env:
- GRAPE_VERSION=0.8.0
- GRAPE_VERSION=0.9.0
- GRAPE_VERSION=0.10.0
- GRAPE_VERSION=0.10.1
- GRAPE_VERSION=0.11.0
- GRAPE_VERSION=0.12.0
- GRAPE_VERSION=0.13.0
- GRAPE_VERSION=0.14.0
- GRAPE_VERSION=HEAD
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

n.n.n / 2016-02-05
==================

### 0.10.3 (Next)

* Your contribution here.
[#336](https://github.com/ruby-grape/grape-swagger/pull/336) changes of swagger-2.0 fork, to support it

* updates gems, corrects parameter, which is in array, make rubocop happy
* runs under 2.3
* documents produces of an end-point
* Update api_swagger_v2_format-content_type_spec.rb
* upgrades to grape 0.14.x; grape-entity 0.5.x

### 0.10.2 (August 19, 2015)

Expand Down
12 changes: 6 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ source 'http://rubygems.org'

gemspec

# case version = ENV['GRAPE_VERSION'] || '~> 0.14.1'
# when 'HEAD'
gem 'grape', github: 'ruby-grape/grape'
# else
# gem 'grape', version
# end
case version = ENV['GRAPE_VERSION'] || '~> 0.14.0'
when 'HEAD'
gem 'grape', github: 'ruby-grape/grape'
else
gem 'grape', version
end
14 changes: 7 additions & 7 deletions grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_runtime_dependency 'grape'
s.add_runtime_dependency 'grape-entity', '0.4.8'
s.add_runtime_dependency 'grape-entity'
s.add_runtime_dependency 'awesome_print'

s.add_development_dependency 'rake'
Expand All @@ -22,12 +22,12 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bundler'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'rack-cors'
s.add_development_dependency 'rubocop', '0.33.0'
s.add_development_dependency 'kramdown', '~> 1.4.1'
s.add_development_dependency 'redcarpet', '~> 3.1.2' unless RUBY_PLATFORM.eql? 'java'
s.add_development_dependency 'rouge', '~> 1.6.1'
s.add_development_dependency 'pry'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'rubocop'
s.add_development_dependency 'kramdown'
s.add_development_dependency 'redcarpet' unless RUBY_PLATFORM.eql? 'java'
s.add_development_dependency 'rouge' unless RUBY_PLATFORM.eql? 'java'
s.add_development_dependency 'pry' unless RUBY_PLATFORM.eql? 'java'
s.add_development_dependency 'pry-byebug' unless RUBY_PLATFORM.eql? 'java'

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
Expand Down
18 changes: 9 additions & 9 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'grape-swagger/version'
require 'grape-swagger/endpoint'
require 'grape-swagger/errors'
require 'grape-swagger/doc_methods/produces'
require 'grape-swagger/doc_methods'
require 'grape-swagger/markdown/kramdown_adapter'
require 'grape-swagger/markdown/redcarpet_adapter'
Expand Down Expand Up @@ -88,11 +89,11 @@ def combine_namespace_routes(namespaces)

if namespace.options.key?(:swagger) && namespace.options[:swagger][:nested] == false
# Namespace shall appear as standalone resource, use specified name or use normalized path as name
if namespace.options[:swagger].key?(:name)
identifier = namespace.options[:swagger][:name].tr(' ', '-')
else
identifier = name.tr('_', '-').gsub(/\//, '_')
end
identifier = if namespace.options[:swagger].key?(:name)
name.tr(' ', '-')
else
name.tr('_', '-').gsub(/\//, '_')
end
@target_class.combined_namespace_identifiers[identifier] = name
@target_class.combined_namespace_routes[identifier] = namespace_routes

Expand All @@ -104,8 +105,8 @@ def combine_namespace_routes(namespaces)
# default case when not explicitly specified or nested == true
standalone_namespaces = namespaces.reject do |_, ns|
!ns.options.key?(:swagger) ||
!ns.options[:swagger].key?(:nested) ||
ns.options[:swagger][:nested] != false
!ns.options[:swagger].key?(:nested) ||
ns.options[:swagger][:nested] != false
end

parent_standalone_namespaces = standalone_namespaces.reject { |ns_name, _| !name.start_with?(ns_name) }
Expand Down Expand Up @@ -142,8 +143,7 @@ def route_path_start_with?(route, name)
route_prefix = route.route_prefix ? "/#{route.route_prefix}/#{name}" : "/#{name}"
route_versioned_prefix = route.route_prefix ? "/#{route.route_prefix}/:version/#{name}" : "/:version/#{name}"

route.route_path.start_with?(route_prefix) ||
route.route_path.start_with?(route_versioned_prefix)
route.route_path.start_with?(route_prefix, route_versioned_prefix)
end

def standalone_sub_namespaces(name, namespaces)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def defaults
base_path: nil,
markdown: false,
hide_documentation_path: true,
format: nil,
format: :json,
models: [],
info: {},
scheme: %w( https http ),
Expand Down
12 changes: 12 additions & 0 deletions lib/grape-swagger/doc_methods/produces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module GrapeSwagger
module DocMethods
class Produces
class << self
def call(*args)
return ['application/json'] unless args.flatten.present?
args.flatten.map { |x| Grape::ContentTypes::CONTENT_TYPES[x] || x }.uniq
end
end
end
end
end
66 changes: 41 additions & 25 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Endpoint
'byte' => %w(string byte),
'date' => %w(string date),
'dateTime' => %w(string date-time)
}
}.freeze

def content_types_for(target_class)
content_types = (target_class.content_types || {}).values
Expand Down Expand Up @@ -103,7 +103,7 @@ def path_item(routes, options)
path.gsub!(/:(\w+)/, '{\1}')

# set item from path, this could be used for the definitions object
@item = path.gsub(/\/\{(.+?)\}/, '').split('/').last.singularize.underscore.camelize || 'Item'
@item = path.gsub(%r{/{(.+?)}}, '').split('/').last.singularize.underscore.camelize || 'Item'
@entity = route.route_entity || route.route_success

# ... replacing version params through submitted version
Expand All @@ -129,8 +129,7 @@ def method_object(route, options)
methods[:description] = description_object(route, options[:markdown])
methods[:headers] = route.route_headers if route.route_headers

mime_types = options[:format] ? Grape::ContentTypes::CONTENT_TYPES[options[:format]] : Grape::ContentTypes::CONTENT_TYPES[:json]
methods[:produces] = [mime_types]
methods[:produces] = produces_object(route, options)

methods[:parameters] = params_object(route)
methods[:responses] = response_object(route)
Expand All @@ -150,6 +149,17 @@ def description_object(route, markdown)
description
end

def produces_object(route, options)
mime_types = GrapeSwagger::DocMethods::Produces.call(options[:format])

route_mime_types = [:route_formats, :route_content_types, :route_produces].map do |producer|
possible = route.send(producer)
GrapeSwagger::DocMethods::Produces.call(possible) if possible.present?
end.flatten.compact.uniq

route_mime_types.present? ? route_mime_types : mime_types
end

def response_object(route)
default_code = default_staus_codes[route.route_method.downcase.to_sym]
default_code[:model] = @entity if @entity
Expand All @@ -165,16 +175,15 @@ def response_object(route)
response_model = @item
response_model = expose_params_from_model(value[:model]) if value[:model]

next unless !response_model.start_with?('Swagger_doc') &&
((@definitions[response_model] && value[:code].to_s.start_with?('2')) || value[:model])

# TODO: proof that the definition exist, if model isn't specified
if !response_model.start_with?('Swagger_doc') &&
((!!@definitions[response_model] && value[:code].to_s.start_with?('2')) ||
value[:model])
if route.route_is_array
memo[value[:code]][:schema] = { 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } }
else
memo[value[:code]][:schema] = { '$ref' => "#/definitions/#{response_model}" }
end
end
memo[value[:code]][:schema] = if route.route_is_array
{ 'type' => 'array', 'items' => { '$ref' => "#/definitions/#{response_model}" } }
else
{ '$ref' => "#/definitions/#{response_model}" }
end
end
end

Expand All @@ -190,7 +199,7 @@ def default_staus_codes

def params_object(route)
partition_params(route).map do |param, value|
parse_params(param, value, route.route_path, route.route_method)
parse_params(param, { required: false }.merge(value), route.route_path, route.route_method)
end
end

Expand Down Expand Up @@ -230,7 +239,7 @@ def parse_request_params(parameters, required, route_paramter)
end

def parse_response_params(params)
return if params.empty?
return if params.nil?

params.each_with_object({}) do |x, memo|
x[0] = x.last[:as] if x.last[:as]
Expand All @@ -247,8 +256,15 @@ def parse_response_params(params)
def expose_params_from_model(model)
model_name = model.name.demodulize.camelize

# has to be adept, to be ready for grape-entity >0.5.0
parameters = model.exposures ? model.exposures : model.documentation
# DONE: has to be adept, to be ready for grape-entity >0.5.0
# TODO: this should only be a temporary hack ;)
if GrapeEntity::VERSION =~ /0\.4\.\d/
parameters = model.exposures ? model.exposures : model.documentation
elsif GrapeEntity::VERSION =~ /0\.5\.\d/
parameters = model.root_exposures.each_with_object({}) do |value, memo|
memo[value.attribute] = value.send(:options)
end
end
properties = parse_response_params(parameters)

@definitions[model_name] = { type: 'object', properties: properties }
Expand All @@ -272,7 +288,7 @@ def hidden?(route)
end

def parse_params(param, value, path, method)
items = {}
@array_items = {}

additional_documentation = value.is_a?(Hash) ? value[:documentation] : nil
data_type = data_type(value)
Expand All @@ -282,7 +298,7 @@ def parse_params(param, value, path, method)
end

description = value.is_a?(Hash) ? value[:desc] || value[:description] : nil
required = value.is_a?(Hash) ? !!value[:required] : false
required = value.is_a?(Hash) ? value[:required] : false
default_value = value.is_a?(Hash) ? value[:default] : nil
example = value.is_a?(Hash) ? value[:example] : nil
is_array = value.is_a?(Hash) ? (value[:is_array] || false) : false
Expand All @@ -291,6 +307,7 @@ def parse_params(param, value, path, method)
enum_or_range_values = parse_enum_or_range_values(values)

value_type = { value: value, data_type: data_type, path: path }

parsed_params = {
in: param_type(value_type, param, method, is_array),
name: name,
Expand All @@ -304,12 +321,10 @@ def parse_params(param, value, path, method)
parsed_params[:type], parsed_params[:format] = PRIMITIVE_MAPPINGS[data_type]
end

parsed_params[:items] = items if items.present?
parsed_params[:items] = @array_items if @array_items.present?

parsed_params[:defaultValue] = example if example
if default_value && example.blank?
parsed_params[:defaultValue] = default_value
end
parsed_params[:defaultValue] = default_value if default_value && example.blank?

parsed_params.merge!(enum_or_range_values) if enum_or_range_values
parsed_params
Expand Down Expand Up @@ -353,8 +368,9 @@ def param_type(value_type, param, method, is_array)
value_type[:value][:documentation].key?(:param_type)

if is_array
items = { '$ref' => value_type[:data_type] }
data_type = 'array'
@array_items = { 'type' => value_type[:data_type] }

'array'
end
else
case
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GrapeSwagger
VERSION = '0.10.3'
VERSION = '0.11.0'.freeze
end
Loading