Skip to content

Commit 676efe2

Browse files
Merge pull request #40 from boocx2/master
new :hide_format option for add_swagger_documentation
2 parents a67d595 + 07da873 commit 676efe2

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

lib/grape-swagger.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def self.setup(options)
4040
:base_path => nil,
4141
:api_version => '0.1',
4242
:markdown => false,
43-
:hide_documentation_path => false
43+
:hide_documentation_path => false,
44+
:hide_format => false
4445
}
4546
options = defaults.merge(options)
4647

@@ -49,6 +50,7 @@ def self.setup(options)
4950
@@class_name = options[:class_name] || options[:mount_path].gsub('/','')
5051
@@markdown = options[:markdown]
5152
@@hide_documentation_path = options[:hide_documentation_path]
53+
@@hide_format = options[:hide_format]
5254
api_version = options[:api_version]
5355
base_path = options[:base_path]
5456

@@ -63,7 +65,7 @@ def self.setup(options)
6365
end
6466

6567
routes_array = routes.keys.map do |local_route|
66-
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}.{format}" }
68+
{ :path => "#{parse_path(route.route_path.gsub('(.:format)', ''),route.route_version)}/#{local_route}#{@@hide_format ? '' : '.{format}'}" }
6769
end
6870
{
6971
apiVersion: api_version,
@@ -158,17 +160,17 @@ def parse_header_params(params)
158160

159161
def parse_path(path, version)
160162
# adapt format to swagger format
161-
parsed_path = path.gsub('(.:format)', '.{format}')
163+
parsed_path = path.gsub '(.:format)', ( @@hide_format ? '' : '.{format}')
162164
# This is attempting to emulate the behavior of
163165
# Rack::Mount::Strexp. We cannot use Strexp directly because
164166
# all it does is generate regular expressions for parsing URLs.
165167
# TODO: Implement a Racc tokenizer to properly generate the
166168
# parsed path.
167169
parsed_path = parsed_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
168170
# add the version
169-
parsed_path = parsed_path.gsub('{version}', version) if version
170-
parsed_path
171+
version ? parsed_path.gsub('{version}', version) : parsed_path
171172
end
173+
172174
def parse_http_codes codes
173175
codes ||= {}
174176
codes.collect do |k, v|

spec/non_default_api_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,27 @@ def app; SimpleApiWithProtection; end
229229
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"https://example.org:80\", :resourcePath=>\"\", :apis=>[{:path=>\"/something.{format}\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
230230
end
231231
end
232+
233+
context ":hide_format" do
234+
before(:all) do
235+
class HidePathsApi < Grape::API
236+
desc 'this gets something'
237+
get '/something' do
238+
{:bla => 'something'}
239+
end
240+
end
241+
242+
class SimpleApiWithHiddenPaths < Grape::API
243+
mount ProtectedApi
244+
add_swagger_documentation :hide_format => true
245+
end
246+
end
247+
248+
def app; SimpleApiWithHiddenPaths; end
249+
250+
it "has no formats" do
251+
get '/swagger_doc/something'
252+
last_response.body.should == "{:apiVersion=>\"0.1\", :swaggerVersion=>\"1.1\", :basePath=>\"http://example.org\", :resourcePath=>\"\", :apis=>[{:path=>\"/something\", :operations=>[{:notes=>nil, :summary=>\"this gets something\", :nickname=>\"GET-something---format-\", :httpMethod=>\"GET\", :parameters=>[]}]}]}"
253+
end
254+
end
232255
end

0 commit comments

Comments
 (0)