Skip to content

Commit

Permalink
Fix for tags for path versioned endpoints (ruby-grape#447)
Browse files Browse the repository at this point in the history
* Test for tags in a versioned endpoint

* Use the version from route when tagging endpoints

* Updated the CHANGELOG
  • Loading branch information
anakinj authored and LeFnord committed Jun 8, 2016
1 parent 64d8159 commit 9b1458c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#### Fixes

* [#450](https://github.com/ruby-grape/grape-swagger/pull/438): Do not add :description to definitions if :description is missing on path - [@texpert](https://github.com/texpert).
* Your contribution here.
* [#447](https://github.com/ruby-grape/grape-swagger/pull/447): Version part of the url is now ignored when generating tags for endpoint - [@anakinj](https://github.com/anakinj).

### 0.21.0 (June 1, 2016)

Expand Down
6 changes: 3 additions & 3 deletions lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def method_object(route, options, path)
method[:consumes] = consumes_object(route, options[:format])
method[:parameters] = params_object(route)
method[:responses] = response_object(route, options[:markdown])
method[:tags] = tag_object(route, options[:version].to_s)
method[:tags] = tag_object(route)
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
method.delete_if { |_, value| value.blank? }

Expand Down Expand Up @@ -194,8 +194,8 @@ def response_object(route, markdown)
end
end

def tag_object(route, version)
Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == version)) }.first)
def tag_object(route)
Array(route.path.split('{')[0].split('/').reject(&:empty?).delete_if { |i| ((i == route.prefix.to_s) || (i == route.version)) }.first)
end

private
Expand Down
37 changes: 19 additions & 18 deletions spec/swagger_v2/endpoint_versioned_path_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
require 'spec_helper'

describe 'Grape::Endpoint#path_and_definitions' do
before do
module API
module V1
class Item < Grape::API
version 'v1', using: :path
let(:api) do
item = Class.new(Grape::API) do
version 'v1', using: :path

resource :item do
get '/'
end
end
end

class Root < Grape::API
mount API::V1::Item
add_swagger_documentation add_version: true
resource :item do
get '/'
end
end

@options = { add_version: true }
@target_routes = API::Root.combined_namespace_routes
Class.new(Grape::API) do
mount item
add_swagger_documentation add_version: true
end
end

let(:options) { { add_version: true } }
let(:target_routes) { api.combined_namespace_routes }

subject { api.endpoints[0].path_and_definition_objects(target_routes, options) }

it 'is returning a versioned path' do
expect(API::V1::Item.endpoints[0]
.path_and_definition_objects(@target_routes, @options)[0].keys[0]).to eql '/v1/item'
expect(subject[0].keys[0]).to eq '/v1/item'
end

it 'tags the endpoint with the resource name' do
expect(subject.first['/v1/item'][:get][:tags]).to eq ['item']
end
end

0 comments on commit 9b1458c

Please sign in to comment.