From 9b1458c560e00bbfee0936f992066d3b8eaced27 Mon Sep 17 00:00:00 2001 From: Joakim Antman Date: Wed, 8 Jun 2016 10:41:47 +0300 Subject: [PATCH] Fix for tags for path versioned endpoints (#447) * Test for tags in a versioned endpoint * Use the version from route when tagging endpoints * Updated the CHANGELOG --- CHANGELOG.md | 2 +- lib/grape-swagger/endpoint.rb | 6 +-- .../endpoint_versioned_path_spec.rb | 37 ++++++++++--------- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16713e79..106ca103 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 81284056..497f88a9 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -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? } @@ -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 diff --git a/spec/swagger_v2/endpoint_versioned_path_spec.rb b/spec/swagger_v2/endpoint_versioned_path_spec.rb index e8a3f41a..51061c5d 100644 --- a/spec/swagger_v2/endpoint_versioned_path_spec.rb +++ b/spec/swagger_v2/endpoint_versioned_path_spec.rb @@ -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