Skip to content

Commit

Permalink
Ensure compatibility with Grape 0.8.0, 0.9.0 and 0.9.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 10, 2014
1 parent e5302b0 commit f9f87c5
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 49 deletions.
12 changes: 6 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2014-11-10 14:38:32 -0500 using RuboCop version 0.27.0.
# on 2014-11-10 15:12:48 -0500 using RuboCop version 0.27.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 8
Metrics/AbcSize:
Max: 332
Max: 327

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 385
Max: 387

# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 92

# Offense count: 209
# Offense count: 196
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 254

# Offense count: 11
# Offense count: 12
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 354
Max: 352

# Offense count: 4
Metrics/PerceivedComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [#154](https://github.com/tim-vandecasteele/grape-swagger/pull/154): Allow classes for type declarations inside documentation - [@mrmargolis](https://github.com/mrmargolis).
* [#162](https://github.com/tim-vandecasteele/grape-swagger/pull/162): Fix performance issue related to having a large number of models - [@elado](https://github.com/elado).
* [#169](https://github.com/tim-vandecasteele/grape-swagger/pull/169): Test against multiple versions of Grape - [@dblock](https://github.com/dblock).
* [#166](https://github.com/tim-vandecasteele/grape-swagger/pull/166): Ensure compatibility with Grape 0.8.0 or newer - [@dblock](https://github.com/dblock).
* Your contribution here.

### 0.8.0 (August 30, 2014)
Expand Down
2 changes: 1 addition & 1 deletion grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.summary = 'A simple way to add auto generated documentation to your Grape API that can be displayed with Swagger.'
s.license = 'MIT'

s.add_runtime_dependency 'grape'
s.add_runtime_dependency 'grape', '>= 0.8.0'
s.add_runtime_dependency 'grape-entity'

s.add_development_dependency 'rake'
Expand Down
19 changes: 11 additions & 8 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def add_swagger_documentation(options = {})

def combine_namespaces(app)
app.endpoints.each do |endpoint|
ns = endpoint.settings.stack.last[:namespace]
ns = if endpoint.respond_to?(:namespace_stackable)
endpoint.namespace_stackable(:namespace).last
else
endpoint.settings.stack.last[:namespace]
end
@combined_namespaces[ns.space] = ns if ns

combine_namespaces(endpoint.options[:app]) if endpoint.options[:app]
Expand Down Expand Up @@ -92,8 +96,7 @@ def self.setup(options)
end
end

desc api_doc.delete(:desc), params: api_doc.delete(:params)
@last_description.merge!(api_doc)
desc api_doc.delete(:desc), api_doc
get @@mount_path do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
Expand Down Expand Up @@ -132,14 +135,14 @@ def self.setup(options)
output
end

desc specific_api_doc.delete(:desc), params: {
desc specific_api_doc.delete(:desc), { params: {
'name' => {
desc: 'Resource name of mounted API',
type: 'string',
required: true
}
}.merge(specific_api_doc.delete(:params) || {})
@last_description.merge!(specific_api_doc)
}.merge(specific_api_doc.delete(:params) || {}) }.merge(specific_api_doc)

get "#{@@mount_path}/:name" do
header['Access-Control-Allow-Origin'] = '*'
header['Access-Control-Request-Method'] = '*'
Expand Down Expand Up @@ -298,10 +301,10 @@ def parse_params(params, path, method)
end

def content_types_for(target_class)
content_types = (target_class.settings[:content_types] || {}).values
content_types = (target_class.content_types || {}).values

if content_types.empty?
formats = [target_class.settings[:format], target_class.settings[:default_format]].compact.uniq
formats = [target_class.format, target_class.default_format].compact.uniq
formats = Grape::Formatter::Base.formatters({}).keys if formats.empty?
content_types = Grape::ContentTypes::CONTENT_TYPES.select { |content_type, _mime_type| formats.include? content_type }.values
end
Expand Down
21 changes: 0 additions & 21 deletions spec/form_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ def app
{}
end

params do
requires :required_group, type: Hash do
requires :required_param_1
requires :required_param_2
end
end
post '/groups' do
{}
end

add_swagger_documentation
end
end
Expand Down Expand Up @@ -93,15 +83,4 @@ def app
]
}])
end

it 'retrieves the documentation for group parameters' do
get '/swagger_doc/groups.json'

body = JSON.parse last_response.body
parameters = body['apis'].first['operations'].first['parameters']
expect(parameters).to eq [
{ 'paramType' => 'form', 'name' => 'required_group[required_param_1]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false },
{ 'paramType' => 'form', 'name' => 'required_group[required_param_2]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false }]

end
end
31 changes: 31 additions & 0 deletions spec/group_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe 'Group Params' do
def app
Class.new(Grape::API) do
format :json

params do
requires :required_group, type: Hash do
requires :required_param_1
requires :required_param_2
end
end
post '/groups' do
{}
end

add_swagger_documentation
end
end

it 'retrieves the documentation for group parameters' do
get '/swagger_doc/groups.json'

body = JSON.parse last_response.body
parameters = body['apis'].first['operations'].first['parameters']
expect(parameters).to eq [
{ 'paramType' => 'form', 'name' => 'required_group[required_param_1]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false },
{ 'paramType' => 'form', 'name' => 'required_group[required_param_2]', 'description' => nil, 'type' => 'string', 'required' => true, 'allowMultiple' => false }]
end
end
6 changes: 3 additions & 3 deletions spec/hide_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/simple.{format}', 'description' => 'Operations about simples' },
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
Expand Down Expand Up @@ -77,7 +77,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/simple.{format}', 'description' => 'Operations about simples' },
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
Expand All @@ -92,7 +92,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/simple',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/simple/show.{format}',
'operations' => [{
Expand Down
14 changes: 7 additions & 7 deletions spec/non_default_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/something.{format}', 'description' => 'Operations about somethings' },
{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }
Expand All @@ -184,7 +184,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/something',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/0.1/something.{format}',
'operations' => [{
Expand Down Expand Up @@ -229,7 +229,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/something.{format}', 'description' => 'Operations about somethings' }
]
Expand Down Expand Up @@ -269,7 +269,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/something',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/abc/something.{format}',
'operations' => [{
Expand Down Expand Up @@ -320,7 +320,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/something',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/abc/v20/something.{format}',
'operations' => [{
Expand Down Expand Up @@ -561,7 +561,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/first.{format}', 'description' => 'Operations about firsts' }
]
Expand All @@ -574,7 +574,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/second.{format}', 'description' => 'Operations about seconds' }
]
Expand Down
6 changes: 3 additions & 3 deletions spec/simple_mounted_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def app
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [
{ 'path' => '/simple.{format}', 'description' => 'Operations about simples' },
{ 'path' => '/simple-test.{format}', 'description' => 'Operations about simple-tests' },
Expand All @@ -91,7 +91,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/simple',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/simple.{format}',
'operations' => [{
Expand All @@ -114,7 +114,7 @@ def app
'swaggerVersion' => '1.2',
'basePath' => 'http://example.org',
'resourcePath' => '/simple-test',
'produces' => ['application/xml', 'application/json', 'application/vnd.api+json', 'text/plain'],
'produces' => Grape::ContentTypes::CONTENT_TYPES.values.uniq,
'apis' => [{
'path' => '/simple-test.{format}',
'operations' => [{
Expand Down

0 comments on commit f9f87c5

Please sign in to comment.