Skip to content

Commit 8f12400

Browse files
committed
adds getting of specified resource
1 parent 3e855dd commit 8f12400

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

5+
* [#501](https://github.com/ruby-grape/grape-swagger/pull/501): Adds getting of a specified resource for Rake Tasks - [@LeFnord](https://github.com/LeFnord).
56
* [#500](https://github.com/ruby-grape/grape-swagger/pull/500): Adds Rake tasks to get and validate OAPI/Swagger documentation - [@LeFnord](https://github.com/LeFnord).
67
* [#493](https://github.com/ruby-grape/grape-swagger/pull/493): Swagger UI endpoint authorization. - [@texpert](https://github.com/texpert).
78
* [#492](https://github.com/ruby-grape/grape/pull/492): Define security requirements on endpoint methods - [@tomregelink](https://github.com/tomregelink).

README.md

+16-11
Original file line numberDiff line numberDiff line change
@@ -803,10 +803,10 @@ end
803803
804804
The Swagger UI on Grape could be secured from unauthorized access using any middleware, which provides certain methods:
805805
806-
- a *before* method to be run in the Grape controller for authorization purpose;
806+
- a *before* method to be run in the Grape controller for authorization purpose;
807807
- some guard method, which could receive as argument a string or an array of authorization scopes;
808808
- a method which processes and returns the access token received in the HTTP request headers (usually in the 'HTTP_AUTHORIZATION' header).
809-
809+
810810
Below are some examples of securing the Swagger UI on Grape installed along with Ruby on Rails:
811811
812812
- The WineBouncer and Doorkeeper gems are used in the examples;
@@ -828,9 +828,9 @@ This is how to configure the grape_swagger documentation:
828828
829829
The guard method should inject the Security Requirement Object into the endpoint's route settings (see Grape::DSL::Settings.route_setting method).
830830

831-
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. it
831+
The 'oauth2 false' added to swagger_documentation is making the main Swagger endpoint protected with OAuth, i.e. it
832832
is retreiving the access_token from the HTTP request, but the 'false' scope is for skipping authorization and showing
833-
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
833+
the UI for everyone. If the scope would be set to something else, like 'oauth2 admin', for example, than the UI
834834
wouldn't be displayed at all to unauthorized users.
835835
836836
Further on, the guard could be used, where necessary, for endpoint access protection. Put it prior to the endpoint's method:
@@ -841,20 +841,20 @@ Further on, the guard could be used, where necessary, for endpoint access protec
841841
get do
842842
render_users
843843
end
844-
844+
845845
oauth2 'admin'
846846
post do
847847
User.create!...
848848
end
849849
end
850850
```
851851

852-
And, finally, if you want to not only restrict the access, but to completely hide the endpoint from unauthorized
852+
And, finally, if you want to not only restrict the access, but to completely hide the endpoint from unauthorized
853853
users, you could pass a lambda to the :hidden key of a endpoint's description:
854-
854+
855855
```ruby
856856
not_admins = lambda { |token=nil| token.nil? || !User.find(token.resource_owner_id).admin? }
857-
857+
858858
resource :users do
859859
desc 'Create user', hidden: not_admins
860860
oauth2 'admin'
@@ -864,8 +864,8 @@ users, you could pass a lambda to the :hidden key of a endpoint's description:
864864
end
865865
```
866866
867-
The lambda is checking whether the user is authenticated (if not, the token is nil by default), and has the admin
868-
role - only admins can see this endpoint.
867+
The lambda is checking whether the user is authenticated (if not, the token is nil by default), and has the admin
868+
role - only admins can see this endpoint.
869869
870870
<a name="md_usage" />
871871
## Markdown in Detail
@@ -1122,15 +1122,20 @@ GrapeSwagger::Rake::OapiTasks.new(::Api::Base)
11221122
11231123
```
11241124
rake oapi:fetch
1125-
rake oapi:fetch store=true # writes to swagger_doc.json
1125+
params:
1126+
- store={ true | file_name } – save as JSON (optional)
1127+
- resource=resource_name – get only for this one (optional)
11261128
```
11271129
11281130
#### OpenApi/Swagger Validation
11291131
11301132
**requires**: `npm` and `swagger-cli` to be installed
11311133
1134+
11321135
```
11331136
rake oapi:validate
1137+
params:
1138+
- resource=resource_name – get only for this one (optional)
11341139
```
11351140
11361141

lib/grape-swagger/rake/oapi_tasks.rb

+25-9
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,26 @@ def define_tasks
2929
#
3030
# get swagger/OpenAPI documentation
3131
def fetch
32-
desc 'generates OpenApi documentation (`store=true`, stores to FS)'
32+
desc 'generates OpenApi documentation …
33+
params (usage: key=value):
34+
store – save as JSON file, default: false (optional)
35+
resource - if given only for that it would be generated (optional)'
3336
task fetch: :environment do
3437
make_request
35-
ENV['store'] ? File.write(file, @oapi) : print(@oapi)
38+
39+
save_to_file? ? File.write(file, @oapi) : $stdout.print(@oapi)
3640
end
3741
end
3842

3943
# validates swagger/OpenAPI documentation
4044
def validate
41-
desc 'validates the generated OpenApi file'
45+
desc 'validates the generated OpenApi file …
46+
params (usage: key=value):
47+
resource - if given only for that it would be generated (optional)'
4248
task validate: :environment do
4349
ENV['store'] = 'true'
4450
::Rake::Task['oapi:fetch'].invoke
51+
exit if error?
4552

4653
output = system "swagger validate #{file}"
4754

@@ -59,19 +66,28 @@ def make_request
5966
JSON.parse(
6067
last_response.body, symolize_names: true
6168
)
62-
)
69+
) + "\n"
6370
end
6471

6572
def url_for
6673
oapi_route = api_class.routes[-2]
67-
url = '/swagger_doc'
68-
url = "/#{oapi_route.version}#{url}" if oapi_route.version
69-
url = "/#{oapi_route.prefix}#{url}" if oapi_route.prefix
70-
url
74+
path = oapi_route.pattern.origin
75+
path.sub!(':version', oapi_route.version.to_s)
76+
77+
[path, ENV['resource']].join('/').chomp('/')
78+
end
79+
80+
def save_to_file?
81+
ENV['store'] && !error?
82+
end
83+
84+
def error?
85+
JSON.parse(@oapi).keys.first == 'error'
7186
end
7287

7388
def file
74-
File.join(Dir.getwd, 'swagger_doc.json')
89+
name = ENV['store'] == 'true' || ENV['store'].blank? ? 'swagger_doc.json' : ENV['store']
90+
File.join(Dir.getwd, name)
7591
end
7692

7793
def app

0 commit comments

Comments
 (0)