diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 679267f5..40559101 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,43 +1,49 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2016-10-31 11:51:42 +0100 using RuboCop version 0.45.0. +# on 2016-11-25 12:17:21 +0800 using RuboCop version 0.45.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: 27 +# Offense count: 2 +Lint/Eval: + Exclude: + - 'lib/grape-swagger/doc_methods/path_string.rb' + - 'lib/grape-swagger/endpoint.rb' + +# Offense count: 29 Metrics/AbcSize: Max: 56 # Offense count: 1 # Configuration parameters: CountComments. Metrics/BlockLength: - Max: 30 + Max: 29 # Offense count: 3 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 247 + Max: 254 -# Offense count: 10 +# Offense count: 12 Metrics/CyclomaticComplexity: - Max: 15 + Max: 14 -# Offense count: 803 +# Offense count: 133 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives. # URISchemes: http, https Metrics/LineLength: - Max: 160 + Max: 159 -# Offense count: 34 +# Offense count: 35 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 101 -# Offense count: 5 +# Offense count: 7 Metrics/PerceivedComplexity: - Max: 16 + Max: 15 # Offense count: 3 Style/ClassVars: @@ -58,20 +64,13 @@ Style/FileName: - 'spec/swagger_v2/api_swagger_v2_type-format_spec.rb' - 'spec/swagger_v2/grape-swagger_spec.rb' -# Offense count: 94 -# Cop supports --auto-correct. -# Configuration parameters: EnforcedStyle, SupportedStyles. -# SupportedStyles: when_needed, always -Style/FrozenStringLiteralComment: - Enabled: false - # Offense count: 1 # Cop supports --auto-correct. Style/MultilineIfModifier: Exclude: - 'lib/grape-swagger/grape/route.rb' -# Offense count: 4 +# Offense count: 5 # Cop supports --auto-correct. # Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes. # SupportedStyles: slashes, percent_r, mixed diff --git a/CHANGELOG.md b/CHANGELOG.md index 41bc5995..8b1af330 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * [#534](https://github.com/ruby-grape/grape-swagger/pull/534): Allows to overwrite defaults status codes - [@LeFnord](https://github.com/LeFnord). +* [#535](https://github.com/ruby-grape/grape-swagger/pull/535): Add support for grape version cascading - [@qinix](https://github.com/qinix). * Your contribution here. #### Fixes diff --git a/lib/grape-swagger/doc_methods/path_string.rb b/lib/grape-swagger/doc_methods/path_string.rb index b1fccd55..b43c15dc 100644 --- a/lib/grape-swagger/doc_methods/path_string.rb +++ b/lib/grape-swagger/doc_methods/path_string.rb @@ -15,7 +15,14 @@ def build(route, options = {}) item = path.gsub(%r{/{(.+?)}}, '').split('/').last.singularize.underscore.camelize || 'Item' if route.version && options[:add_version] - path.sub!('{version}', route.version.to_s) + version = route.version + # for grape version 0.14.0..0.16.2, the version can be a string like '[:v1, :v2]' + # for grape version bigger than 0.16.2, the version can be a array like [:v1, :v2] + version = version.first while version.is_a?(Array) + # eval('[:v1, :v2]') for grape lower than 0.17 + version = eval(version) if version.is_a?(String) && version.start_with?('[') && version.end_with?(']') + version = version.first while version.is_a?(Array) + path.sub!('{version}', version.to_s) else path.sub!('/{version}', '') end diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index da260b65..f330ae0e 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -223,7 +223,17 @@ def apply_success_codes(route) end def tag_object(route) - Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first) + version = route.version + # for grape version 0.14.0..0.16.2, the version can be a string like '[:v1, :v2]' + # for grape version bigger than 0.16.2, the version can be a array like [:v1, :v2] + # eval('[:v1, :v2]') for grape lower than 0.17 + version = eval(version) if version.is_a?(String) && version.start_with?('[') && version.end_with?(']') + version = [version] unless version.is_a?(Array) + [route.path.split('{')[0] + .split('/').reject(&:empty?) + .delete_if do |i| + ((i == route.prefix.to_s) || version.map(&:to_s).include?(i)) + end.first] end private diff --git a/spec/swagger_v2/namespace_tags_prefix_spec.rb b/spec/swagger_v2/namespace_tags_prefix_spec.rb index 1c3ea687..2bb39d25 100644 --- a/spec/swagger_v2/namespace_tags_prefix_spec.rb +++ b/spec/swagger_v2/namespace_tags_prefix_spec.rb @@ -6,14 +6,34 @@ before :all do module TheApi class NamespaceApi < Grape::API - version :v1 + version [:v1, :v2] + end + + class CascadingVersionApi < Grape::API + version :v2 + + namespace :hudson do + desc 'Document root' + get '/' do + end + end + + namespace :colorado do + desc 'This gets something.', + notes: '_test_' + + get '/simple' do + { bla: 'something' } + end + end end end class TagApi < Grape::API prefix :api + mount TheApi::CascadingVersionApi mount TheApi::NamespaceApi - add_swagger_documentation version: 'v1' + add_swagger_documentation end end @@ -43,6 +63,8 @@ def app expect(subject['paths']['/api/v1/thames/simple_with_headers']['get']['tags']).to eql(['thames']) expect(subject['paths']['/api/v1/niles/items']['post']['tags']).to eql(['niles']) expect(subject['paths']['/api/v1/niles/custom']['get']['tags']).to eql(['niles']) + expect(subject['paths']['/api/v2/hudson']['get']['tags']).to eql(['hudson']) + expect(subject['paths']['/api/v2/colorado/simple']['get']['tags']).to eql(['colorado']) end end