From 6c5e386a3242fa0feeaf7099d4bebbd0b51c410c Mon Sep 17 00:00:00 2001 From: peter scholz Date: Tue, 27 Dec 2016 12:27:24 +0100 Subject: [PATCH] Version Cascading including dependency updates (#558) * Grape version cascading (#27) - Add support for grape version cascading - make rubocop happy, fix test - fix test failure for grape lower than 0.17 - refactors getting version from route * updates dependencies - drops grape <0.16.2 support, including code only for grape <0.16.2 - adds ruby 2.4.0 support - adds grape 0.19.0 support - adds changelog entry - updates version matrix - adapts rubocop todo --- .rubocop.yml | 4 +++ .rubocop_todo.yml | 29 +++++++------------ .travis.yml | 29 ++++++++++--------- CHANGELOG.md | 3 ++ Gemfile | 6 ++-- README.md | 2 ++ Rakefile | 2 +- grape-swagger.gemspec | 3 +- lib/grape-swagger.rb | 2 -- lib/grape-swagger/doc_methods.rb | 1 + lib/grape-swagger/doc_methods/path_string.rb | 4 ++- lib/grape-swagger/doc_methods/version.rb | 19 ++++++++++++ lib/grape-swagger/endpoint.rb | 4 ++- lib/grape-swagger/grape/route.rb | 17 ----------- spec/swagger_v2/namespace_tags_prefix_spec.rb | 26 +++++++++++++++-- 15 files changed, 92 insertions(+), 59 deletions(-) create mode 100644 lib/grape-swagger/doc_methods/version.rb delete mode 100644 lib/grape-swagger/grape/route.rb diff --git a/.rubocop.yml b/.rubocop.yml index 06d3b69..55d6891 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -9,5 +9,9 @@ Metrics/LineLength: Exclude: - spec/**/* +Metrics/MethodLength: + Exclude: + - spec/**/* + Style/IndentHash: EnforcedStyle: consistent diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index aa0e618..4a432b4 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,43 +1,43 @@ # 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-24 18:42:08 +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: 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: 265 -# Offense count: 10 +# Offense count: 12 Metrics/CyclomaticComplexity: - Max: 15 + Max: 14 -# Offense count: 803 +# Offense count: 129 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives. # URISchemes: http, https Metrics/LineLength: Max: 120 -# Offense count: 34 +# Offense count: 35 # Configuration parameters: CountComments. Metrics/MethodLength: - Max: 101 + Max: 40 -# Offense count: 5 +# Offense count: 7 Metrics/PerceivedComplexity: - Max: 16 + Max: 15 # Offense count: 3 Style/ClassVars: @@ -58,20 +58,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/.travis.yml b/.travis.yml index afeebb0..7b6d80f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,26 +6,29 @@ before_install: matrix: include: - - rvm: 2.3.3 + - rvm: 2.4.0 script: - bundle exec danger + - rvm: 2.4.0 + env: MODEL_PARSER=grape-swagger-entity + - rvm: 2.4.0 + env: MODEL_PARSER=grape-swagger-representable + - rvm: 2.4.0 + env: GRAPE_VERSION=0.16.2 + - rvm: 2.4.0 + env: GRAPE_VERSION=0.17.0 + - rvm: 2.4.0 + env: GRAPE_VERSION=0.18.0 + - rvm: 2.4.0 + env: GRAPE_VERSION=0.19.0 + - rvm: 2.4.0 + env: GRAPE_VERSION=HEAD - rvm: 2.3.3 env: MODEL_PARSER=grape-swagger-entity - rvm: 2.3.3 env: MODEL_PARSER=grape-swagger-representable - rvm: 2.3.3 - env: GRAPE_VERSION=0.14.0 - - rvm: 2.3.3 - env: GRAPE_VERSION=0.15.0 - - rvm: 2.3.3 - env: GRAPE_VERSION=0.16.2 - - rvm: 2.3.3 - env: GRAPE_VERSION=0.17.0 - - rvm: 2.3.3 - env: GRAPE_VERSION=0.18.0 - - rvm: 2.3.3 - env: GRAPE_VERSION=HEAD - - rvm: 2.2 + - rvm: 2.2.6 - rvm: ruby-head - rvm: jruby-9.1.6.0 - rvm: jruby-head diff --git a/CHANGELOG.md b/CHANGELOG.md index a00f766..802624d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ #### Features +* [#558](https://github.com/ruby-grape/grape-swagger/pull/558): Version cascading including dependency updates (includes: [LeFnord#27](https://github.com/LeFnord/grape-swagger/pull/27)) - [@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/Gemfile b/Gemfile index 153c818..49cd775 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ ruby RUBY_VERSION gemspec -gem 'grape', case version = ENV['GRAPE_VERSION'] || '~> 0.18' +gem 'grape', case version = ENV['GRAPE_VERSION'] || '~> 0.19' when 'HEAD' { github: 'ruby-grape/grape' } else @@ -23,7 +23,7 @@ group :development, :test do gem 'rack-test' gem 'rake' gem 'rdoc' - gem 'redcarpet', platforms: [:mri] + gem 'redcarpet', '< 3.4', platforms: [:mri] gem 'rouge', platforms: [:mri] gem 'rspec', '~> 3.0' gem 'rubocop', '~> 0.40' @@ -32,5 +32,5 @@ end group :test do gem 'grape-entity' gem 'grape-swagger-entity' - gem 'ruby-grape-danger', '~> 0.1.0', require: false + gem 'ruby-grape-danger', '~> 0.1.1', require: false end diff --git a/README.md b/README.md index 476380d..a1d5eef 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ grape-swagger | swagger spec | grape | grape-entity | represen 0.23.0 | 2.0 | >= 0.12.0 ... <= 0.17.0 | <= 0.5.1 | >= 2.4.1 | 0.24.0 | 2.0 | >= 0.12.0 ... <= 0.18.0 | <= 0.5.1 | >= 2.4.1 | 0.25.0 | 2.0 | >= 0.14.0 ... <= 0.18.0 | <= 0.5.2 | >= 2.4.1 | +0.25.2 | 2.0 | >= 0.14.0 ... <= 0.18.0 | <= 0.6.0 | >= 2.4.1 | +> 0.25.2 | 2.0 | >= 0.16.2 | <= 0.6.0 | >= 2.4.1 | ## Swagger-Spec diff --git a/Rakefile b/Rakefile index 86fa412..d37a305 100644 --- a/Rakefile +++ b/Rakefile @@ -17,4 +17,4 @@ RSpec::Core::RakeTask.new(:spec) require 'rubocop/rake_task' RuboCop::RakeTask.new(:rubocop) -task default: [:rubocop, :spec] +task default: [:spec, :rubocop] diff --git a/grape-swagger.gemspec b/grape-swagger.gemspec index 73d418e..4f5f02e 100644 --- a/grape-swagger.gemspec +++ b/grape-swagger.gemspec @@ -11,7 +11,8 @@ Gem::Specification.new do |s| s.summary = 'Add auto generated documentation to your Grape API that can be displayed with Swagger.' s.license = 'MIT' - s.add_runtime_dependency 'grape', '>= 0.12.0' + s.required_ruby_version = '>= 2.2.6' + s.add_runtime_dependency 'grape', '>= 0.16.2' s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec}/*`.split("\n") diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 726b4f1..5cf6976 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -1,7 +1,5 @@ require 'grape' -require 'grape-swagger/grape/route' - require 'grape-swagger/version' require 'grape-swagger/endpoint' require 'grape-swagger/errors' diff --git a/lib/grape-swagger/doc_methods.rb b/lib/grape-swagger/doc_methods.rb index 7888047..4361f64 100644 --- a/lib/grape-swagger/doc_methods.rb +++ b/lib/grape-swagger/doc_methods.rb @@ -10,6 +10,7 @@ require 'grape-swagger/doc_methods/parse_params' require 'grape-swagger/doc_methods/move_params' require 'grape-swagger/doc_methods/headers' require 'grape-swagger/doc_methods/build_model_definition' +require 'grape-swagger/doc_methods/version' module GrapeSwagger module DocMethods diff --git a/lib/grape-swagger/doc_methods/path_string.rb b/lib/grape-swagger/doc_methods/path_string.rb index 4d3e11d..955a56f 100644 --- a/lib/grape-swagger/doc_methods/path_string.rb +++ b/lib/grape-swagger/doc_methods/path_string.rb @@ -15,7 +15,9 @@ module GrapeSwagger item = path.gsub(%r{/{(.+?)}}, '').split('/').last.singularize.underscore.camelize || 'Item' if route.version && options[:add_version] - path.sub!('{version}', route.version.to_s) + version = GrapeSwagger::DocMethods::Version.get(route) + 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/doc_methods/version.rb b/lib/grape-swagger/doc_methods/version.rb new file mode 100644 index 0000000..c4aec4c --- /dev/null +++ b/lib/grape-swagger/doc_methods/version.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +module GrapeSwagger + module DocMethods + class Version + class << self + def get(route) + version = route.version + # for grape version 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] + if version.is_a?(String) && version.start_with?('[') && version.end_with?(']') + instance_eval(version) + else + version + end + end + end + end + end +end diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 189de8b..5c1fa18 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -228,9 +228,11 @@ module Grape end def tag_object(route) + version = GrapeSwagger::DocMethods::Version.get(route) + version = [version] unless version.is_a?(Array) Array( route.path.split('{')[0].split('/').reject(&:empty?).delete_if do |i| - i == route.prefix.to_s || i == route.version + i == route.prefix.to_s || version.map(&:to_s).include?(i) end.first ) end diff --git a/lib/grape-swagger/grape/route.rb b/lib/grape-swagger/grape/route.rb deleted file mode 100644 index e84b6c9..0000000 --- a/lib/grape-swagger/grape/route.rb +++ /dev/null @@ -1,17 +0,0 @@ -# backwards compatibility for Grape < 0.16.0 -module Grape - class Route - [:path, :prefix, :entity, :description, :settings, :params, :headers, :http_codes, :version] - .each do |m| - define_method m do - send "route_#{m}" - end - end - - def request_method - route_method - end - - attr_reader :options - end -end if defined?(Grape::VERSION) && Gem::Version.new(::Grape::VERSION) < Gem::Version.new('0.16.0') diff --git a/spec/swagger_v2/namespace_tags_prefix_spec.rb b/spec/swagger_v2/namespace_tags_prefix_spec.rb index 1c3ea68..2bb39d2 100644 --- a/spec/swagger_v2/namespace_tags_prefix_spec.rb +++ b/spec/swagger_v2/namespace_tags_prefix_spec.rb @@ -6,14 +6,34 @@ describe 'namespace tags check while using prefix and version' do 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 @@ describe 'namespace tags check while using prefix and version' do 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