From 24050b2635f174fee4df0c95c769cd93aae9aefe Mon Sep 17 00:00:00 2001 From: dB Date: Sat, 29 Nov 2014 13:39:08 -0500 Subject: [PATCH] Fixed compatibility with Grape HEAD, related to https://github.com/intridea/grape/pull/809. --- .rubocop_todo.yml | 10 ++++---- .travis.yml | 4 --- lib/grape-swagger.rb | 7 ++++-- spec/api_models_spec.rb | 24 ++++++------------ spec/api_root_spec.rb | 31 +++++++++++++++++++++++ spec/boolean_params_spec.rb | 2 +- spec/form_params_spec.rb | 50 ++++++------------------------------- spec/group_params_spec.rb | 2 +- spec/hash_params_spec.rb | 2 +- 9 files changed, 58 insertions(+), 74 deletions(-) create mode 100644 spec/api_root_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 09a263af..29a8ce60 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 @@ -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 @@ -36,7 +36,7 @@ Metrics/PerceivedComplexity: Style/ClassVars: Enabled: false -# Offense count: 68 +# Offense count: 69 Style/Documentation: Enabled: false diff --git a/.travis.yml b/.travis.yml index 3ce06307..fd693613 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 07d57fcd..4e65deea 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -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! diff --git a/spec/api_models_spec.rb b/spec/api_models_spec.rb index 65eede6f..128c51d4 100644 --- a/spec/api_models_spec.rb +++ b/spec/api_models_spec.rb @@ -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' => { @@ -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', @@ -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', @@ -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') diff --git a/spec/api_root_spec.rb b/spec/api_root_spec.rb new file mode 100644 index 00000000..b663cf2d --- /dev/null +++ b/spec/api_root_spec.rb @@ -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 diff --git a/spec/boolean_params_spec.rb b/spec/boolean_params_spec.rb index e94d4449..07d5325e 100644 --- a/spec/boolean_params_spec.rb +++ b/spec/boolean_params_spec.rb @@ -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'] diff --git a/spec/form_params_spec.rb b/spec/form_params_spec.rb index 5d239a9f..46935edf 100644 --- a/spec/form_params_spec.rb +++ b/spec/form_params_spec.rb @@ -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 diff --git a/spec/group_params_spec.rb b/spec/group_params_spec.rb index 3f5a4b94..82bd4d78 100644 --- a/spec/group_params_spec.rb +++ b/spec/group_params_spec.rb @@ -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'] diff --git a/spec/hash_params_spec.rb b/spec/hash_params_spec.rb index dbe42441..73570773 100644 --- a/spec/hash_params_spec.rb +++ b/spec/hash_params_spec.rb @@ -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']