Skip to content

Commit 9656e95

Browse files
committed
allow token_owner to be passed to the hidden proc for parameters as well
1 parent 11d29c4 commit 9656e95

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* Your contribution here.
66

7+
* [#775](https://github.com/ruby-grape/grape-swagger/pull/775): Add in token_owner support to param hidden procs - [@urkle](https://github.com/urkle).
8+
79
#### Fixes
810

911
### 0.34.2 (January 20, 2020)

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,12 @@ end
743743
Exclude single optional parameter from the documentation
744744

745745
```ruby
746+
not_admins = lambda { |token_owner = nil| token_owner.nil? || !token_owner.admin? }
747+
746748
params do
747749
optional :one, documentation: { hidden: true }
748-
optional :two, documentation: { hidden: -> { true } }
750+
optional :two, documentation: { hidden: -> { |t=nil| true } }
751+
optional :three, documentation: { hidden: not_admins }
749752
end
750753
post :act do
751754
...

lib/grape-swagger/endpoint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def partition_params(route, settings)
301301
default_type(required)
302302

303303
request_params = unless declared_params.nil? && route.headers.nil?
304-
GrapeSwagger::Endpoint::ParamsParser.parse_request_params(required, settings)
304+
GrapeSwagger::Endpoint::ParamsParser.parse_request_params(required, settings, self)
305305
end || {}
306306

307307
request_params.empty? ? required : request_params

lib/grape-swagger/endpoint/params_parser.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
module GrapeSwagger
44
module Endpoint
55
class ParamsParser
6-
attr_reader :params, :settings
6+
attr_reader :params, :settings, :endpoint
77

8-
def self.parse_request_params(params, settings)
9-
new(params, settings).parse_request_params
8+
def self.parse_request_params(params, settings, endpoint)
9+
new(params, settings, endpoint).parse_request_params
1010
end
1111

12-
def initialize(params, settings)
12+
def initialize(params, settings, endpoint)
1313
@params = params
1414
@settings = settings
15+
@endpoint = endpoint
1516
end
1617

1718
def parse_request_params
@@ -55,7 +56,9 @@ def public_parameter?(param)
5556
return true unless param_options.key?(:documentation) && !param_options[:required]
5657

5758
param_hidden = param_options[:documentation].fetch(:hidden, false)
58-
param_hidden = param_hidden.call if param_hidden.is_a?(Proc)
59+
if param_hidden.is_a?(Proc)
60+
param_hidden = settings[:token_owner] ? param_hidden.call(endpoint.send(settings[:token_owner].to_sym)) : param_hidden.call
61+
end
5962
!param_hidden
6063
end
6164

spec/lib/endpoint_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
describe 'parse_request_params' do
5050
let(:subject) { GrapeSwagger::Endpoint::ParamsParser }
5151
before do
52-
subject.send(:parse_request_params, params, {})
52+
subject.send(:parse_request_params, params, {}, nil)
5353
end
5454

5555
context 'when params do not contain an array' do

spec/swagger_v2/api_swagger_v2_hide_param_spec.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
before :all do
88
module TheApi
99
class HideParamsApi < Grape::API
10+
helpers do
11+
def resource_owner
12+
'123'
13+
end
14+
end
15+
1016
namespace :flat_params_endpoint do
1117
desc 'This is a endpoint with a flat parameter hierarchy'
1218
params do
1319
requires :name, type: String, documentation: { desc: 'name' }
1420
optional :favourite_color, type: String, documentation: { desc: 'I should not be anywhere', hidden: true }
15-
optional :proc_param, type: String, documentation: { desc: 'I should not be anywhere', hidden: -> { true } }
21+
optional :proc_param, type: String, documentation: { desc: 'I should not be anywhere', hidden: proc { true } }
22+
optional :proc_with_token, type: String, documentation: { desc: 'I may be somewhere', hidden: proc { |token_owner = nil| token_owner.nil? } }
1623
end
1724

1825
post do
@@ -50,7 +57,7 @@ class HideParamsApi < Grape::API
5057
end
5158
end
5259

53-
add_swagger_documentation
60+
add_swagger_documentation token_owner: 'resource_owner'
5461
end
5562
end
5663
end
@@ -63,9 +70,13 @@ class HideParamsApi < Grape::API
6370
JSON.parse(last_response.body)
6471
end
6572

66-
specify do
73+
it 'ignores parameters that are explicitly hidden' do
6774
expect(subject['paths']['/flat_params_endpoint']['post']['parameters'].map { |p| p['name'] }).not_to include('favourite_color', 'proc_param')
6875
end
76+
77+
it 'allows procs to consult the token_owner' do
78+
expect(subject['paths']['/flat_params_endpoint']['post']['parameters'].map { |p| p['name'] }).to include('proc_with_token')
79+
end
6980
end
7081

7182
describe 'nested parameter hierarchy' do

0 commit comments

Comments
 (0)