Skip to content

Add support for grape version cascading #535

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

Closed
Closed
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
37 changes: 18 additions & 19 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/grape-swagger/doc_methods/path_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions spec/swagger_v2/namespace_tags_prefix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down