Skip to content

Commit

Permalink
Fixed compatibility with Grape HEAD, related to ruby-grape/grape#809.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Nov 29, 2014
1 parent c4f49d1 commit 3c578d6
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 69 deletions.
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ env:
- GRAPE_VERSION=0.8.0
- GRAPE_VERSION=0.9.0
- GRAPE_VERSION=HEAD

matrix:
allow_failures:
- env: GRAPE_VERSION=HEAD
8 changes: 6 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ def add_swagger_documentation(options = {})

@combined_routes = {}
routes.each do |route|
route_match = route.route_path.split(/^.*?#{route.route_prefix.to_s}/).last.match('\/([\w|-]*?)[\.\/\(]')
next if route_match.nil?
route_path = route.route_path
#route_path += "(.:format)" unless route_path.ends_with?('(.:format)')
route_match = route_path.split(/^.*?#{route.route_prefix.to_s}/).last
next unless route_match
route_match = route_match.match('\/([\w|-]*?)[\.\/\(]') || route_match.match('\/([\w|-]*)')
next unless route_match
resource = route_match.captures.first
next if resource.empty?
resource.downcase!
Expand Down
24 changes: 7 additions & 17 deletions spec/api_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,21 @@ def app
end

it 'returns type' do
get '/swagger_doc/something.json'
get '/swagger_doc/something'
result = JSON.parse(last_response.body)
expect(result['apis'].first['operations'].first['type']).to eq 'Something'
end

it 'includes nested type' do
get '/swagger_doc/thing.json'
get '/swagger_doc/thing'
result = JSON.parse(last_response.body)
expect(result['apis'].first['operations'].first['type']).to eq 'Some::Thing'
end

it 'includes entities which are only used as composition' do
get '/swagger_doc/somethingelse.json'
get '/swagger_doc/somethingelse'
result = JSON.parse(last_response.body)
expect(result['apis']).to eq([{
'path' => '/somethingelse.{format}',
'operations' => [{
'notes' => '',
'type' => 'SomeThingElse',
'summary' => 'This gets somthing else.',
'nickname' => 'GET-somethingelse---format-',
'method' => 'GET',
'parameters' => []
}]
}])
expect(result['apis'][0]['path']).to start_with '/somethingelse'

expect(result['models']['SomeThingElse']).to include('id' => 'SomeThingElse',
'properties' => {
Expand Down Expand Up @@ -217,7 +207,7 @@ def app
end

it 'includes enum values in params and documentation.' do
get '/swagger_doc/enum_description_in_entity.json'
get '/swagger_doc/enum_description_in_entity'
result = JSON.parse(last_response.body)
expect(result['models']['EnumValues']).to eq(
'id' => 'EnumValues',
Expand All @@ -239,7 +229,7 @@ def app
end

it 'includes referenced models in those with aliased references.' do
get '/swagger_doc/aliasedthing.json'
get '/swagger_doc/aliasedthing'
result = JSON.parse(last_response.body)
expect(result['models']['AliasedThing']).to eq(
'id' => 'AliasedThing',
Expand All @@ -257,7 +247,7 @@ def app
end

it 'includes all entities with four levels of nesting' do
get '/swagger_doc/nesting.json'
get '/swagger_doc/nesting'
result = JSON.parse(last_response.body)

expect(result['models']).to include('FirstLevel', 'SecondLevel', 'ThirdLevel', 'FourthLevel')
Expand Down
31 changes: 31 additions & 0 deletions spec/api_root_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe 'simple root api' do

before :all do
class ApiRoot < Grape::API
format :json
prefix 'api'
version 'v2', using: :header, vendor: 'artsy', strict: false
get do
{}
end
add_swagger_documentation
end
end

def app
ApiRoot
end

it 'retrieves swagger-documentation on /swagger_doc' do
get '/api/swagger_doc'
expect(JSON.parse(last_response.body)).to eq(
'apiVersion' => '0.1',
'swaggerVersion' => '1.2',
'info' => {},
'produces' => ["application/json"],
'apis' => [{ 'path' => '/swagger_doc.{format}', 'description' => 'Operations about swagger_docs' }]
)
end
end
2 changes: 1 addition & 1 deletion spec/boolean_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def app
end

subject do
get '/swagger_doc/splines.json'
get '/swagger_doc/splines'
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
Expand Down
50 changes: 7 additions & 43 deletions spec/form_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,16 @@ def app
end

subject do
get '/swagger_doc/items.json'
get '/swagger_doc/items'
JSON.parse(last_response.body)
end

it 'retrieves the documentation form params' do
expect(subject['apis']).to eq([
{
'path' => '/items.{format}',
'operations' => [
{
'notes' => '',
'summary' => '',
'nickname' => 'POST-items---format-',
'method' => 'POST',
'parameters' => [{ 'paramType' => 'form', 'name' => 'name', 'description' => 'name of item', 'type' => 'string', 'required' => true, 'allowMultiple' => false }],
'type' => 'void'
}
]
}, {
'path' => '/items/{id}.{format}',
'operations' => [
{
'notes' => '',
'summary' => '',
'nickname' => 'PUT-items--id---format-',
'method' => 'PUT',
'parameters' => [
{ 'paramType' => 'path', 'name' => 'id', 'description' => 'id of item', 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32' },
{ 'paramType' => 'form', 'name' => 'name', 'description' => 'name of item', 'type' => 'string', 'required' => true, 'allowMultiple' => false },
{ 'paramType' => 'form', 'name' => 'conditions', 'description' => 'conditions of item', 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32', 'enum' => [1, 2, 3] }
],
'type' => 'void'
},
{
'notes' => '',
'summary' => '',
'nickname' => 'PATCH-items--id---format-',
'method' => 'PATCH',
'parameters' => [
{ 'paramType' => 'path', 'name' => 'id', 'description' => 'id of item', 'type' => 'integer', 'required' => true, 'allowMultiple' => false, 'format' => 'int32' },
{ 'paramType' => 'form', 'name' => 'name', 'description' => 'name of item', 'type' => 'string', 'required' => true, 'allowMultiple' => false },
{ 'paramType' => 'form', 'name' => 'conditions', 'description' => 'conditions of item', 'type' => 'string', 'required' => false, 'allowMultiple' => false, 'enum' => %w(1 2) }
],
'type' => 'void'
}
]
}])
expect(subject['apis'].count).to eq 2
expect(subject['apis'][0]['path']).to start_with '/items'
expect(subject['apis'][0]['operations'][0]['method']).to eq 'POST'
expect(subject['apis'][1]['path']).to start_with '/items/{id}'
expect(subject['apis'][1]['operations'][0]['method']).to eq 'PUT'
expect(subject['apis'][1]['operations'][1]['method']).to eq 'PATCH'
end
end
2 changes: 1 addition & 1 deletion spec/group_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def app
end

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

body = JSON.parse last_response.body
parameters = body['apis'].first['operations'].first['parameters']
Expand Down
2 changes: 1 addition & 1 deletion spec/hash_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def app
end

subject do
get '/swagger_doc/splines.json'
get '/swagger_doc/splines'
expect(last_response.status).to eq 200
body = JSON.parse last_response.body
body['apis'].first['operations'].first['parameters']
Expand Down

0 comments on commit 3c578d6

Please sign in to comment.