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

Fixed compatibility with Grape HEAD. #182

Merged
merged 1 commit into from
Nov 29, 2014
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
10 changes: 5 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2014-11-28 11:38:25 -0500 using RuboCop version 0.27.0.
# on 2014-11-29 13:48:01 -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
Expand All @@ -12,13 +12,13 @@ Metrics/AbcSize:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 394
Max: 397

# Offense count: 4
# Offense count: 5
Metrics/CyclomaticComplexity:
Max: 93

# Offense count: 202
# Offense count: 195
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 254
Expand All @@ -36,7 +36,7 @@ Metrics/PerceivedComplexity:
Style/ClassVars:
Enabled: false

# Offense count: 68
# Offense count: 69
Style/Documentation:
Enabled: false

Expand Down
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
7 changes: 5 additions & 2 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ 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_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