Skip to content

Commit 463b289

Browse files
tomregelinkpeter scholz
authored and
peter scholz
committed
Allow endpoints to define security requirements (#492)
1 parent 053713c commit 463b289

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Metrics/AbcSize:
2828
# Offense count: 3
2929
# Configuration parameters: CountComments.
3030
Metrics/ClassLength:
31-
Max: 226
31+
Max: 231
3232

3333
# Offense count: 10
3434
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#492](https://github.com/ruby-grape/grape/pull/492): Define security requirements on endpoint methods - [@tomregelink](https://github.com/tomregelink).
56
* Your contribution here.
67

78
#### Fixes

lib/grape-swagger/endpoint.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def method_object(route, options, path)
108108
method[:produces] = produces_object(route, options[:produces] || options[:format])
109109
method[:consumes] = consumes_object(route, options[:format])
110110
method[:parameters] = params_object(route)
111+
method[:security] = security_object(route)
111112
method[:responses] = response_object(route, options[:markdown])
112113
method[:tags] = tag_object(route)
113114
method[:operationId] = GrapeSwagger::DocMethods::OperationId.build(route, path)
@@ -116,6 +117,10 @@ def method_object(route, options, path)
116117
[route.request_method.downcase.to_sym, method]
117118
end
118119

120+
def security_object(route)
121+
route.options[:security] if route.options.key?(:security)
122+
end
123+
119124
def summary_object(route)
120125
summary = route.options[:desc] if route.options.key?(:desc)
121126
summary = route.description if route.description.present?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe 'security requirement on endpoint method' do
4+
def app
5+
Class.new(Grape::API) do
6+
desc 'Endpoint with security requirement', security: [oauth_pets: ['read:pets', 'write:pets']]
7+
get '/with_security' do
8+
{ foo: 'bar' }
9+
end
10+
11+
add_swagger_documentation
12+
end
13+
end
14+
15+
subject do
16+
get '/swagger_doc.json'
17+
JSON.parse(last_response.body)
18+
end
19+
20+
it 'defines the security requirement on the endpoint method' do
21+
expect(subject['paths']['/with_security']['get']['security']).to eql ['oauth_pets' => ['read:pets', 'write:pets']]
22+
end
23+
end

0 commit comments

Comments
 (0)