Skip to content

Commit db76f42

Browse files
authored
Refactor oapi fetch task (#846)
* oapi rake task generate specs for different API versions * Refactor oapi:fetch rake task generate specs per file * Fixing specs * CHANGELOG.md * Rubocop auto gen config * Update README.md * Satisfy rubocop
1 parent eb94a31 commit db76f42

File tree

5 files changed

+88
-45
lines changed

5 files changed

+88
-45
lines changed

.rubocop_todo.yml

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2019-06-05 12:13:59 +0000 using RuboCop version 0.71.0.
3+
# on 2022-01-14 10:22:29 UTC using RuboCop version 1.24.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9+
# Offense count: 1
10+
# Cop supports --auto-correct.
11+
# Configuration parameters: Include.
12+
# Include: **/*.gemspec
13+
Gemspec/RequireMFA:
14+
Exclude:
15+
- 'grape-swagger.gemspec'
16+
917
# Offense count: 1
1018
# Configuration parameters: Include.
1119
# Include: **/*.gemspec
1220
Gemspec/RequiredRubyVersion:
1321
Exclude:
1422
- 'grape-swagger.gemspec'
1523

16-
# Offense count: 30
24+
# Offense count: 31
25+
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
1726
Metrics/AbcSize:
18-
Max: 59
19-
20-
# Offense count: 10
21-
Metrics/CyclomaticComplexity:
22-
Max: 13
27+
Max: 56
2328

24-
# Offense count: 22
25-
# Configuration parameters: CountComments, ExcludedMethods.
29+
# Offense count: 30
30+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
2631
Metrics/MethodLength:
27-
Max: 45
32+
Max: 28
2833

2934
# Offense count: 7
35+
# Configuration parameters: IgnoredMethods.
3036
Metrics/PerceivedComplexity:
3137
Max: 16
3238

@@ -35,6 +41,20 @@ Style/ClassVars:
3541
Exclude:
3642
- 'lib/grape-swagger/doc_methods.rb'
3743

38-
# Offense count: 22
44+
# Offense count: 23
45+
# Configuration parameters: AllowedConstants.
3946
Style/Documentation:
4047
Enabled: false
48+
49+
# Offense count: 43
50+
Style/OpenStructUse:
51+
Exclude:
52+
- 'spec/lib/endpoint_spec.rb'
53+
- 'spec/lib/version_spec.rb'
54+
- 'spec/support/mock_parser.rb'
55+
- 'spec/support/model_parsers/mock_parser.rb'
56+
- 'spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb'
57+
- 'spec/swagger_v2/api_swagger_v2_mounted_spec.rb'
58+
- 'spec/swagger_v2/api_swagger_v2_spec.rb'
59+
- 'spec/swagger_v2/errors_spec.rb'
60+
- 'spec/swagger_v2/reference_entity_spec.rb'

CHANGELOG.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
### Next
2-
3-
#### Features
4-
5-
* Your contribution here.
1+
### 1.4.3 (January 5, 2022)
62

73
#### Fixes
84

95
* [#850](https://github.com/ruby-grape/grape-swagger/pull/850): Fix value of `enum` to be `Array` - [@takahashim](https://github.com/takahashim)
10-
* Your contribution here.
11-
6+
* [#846] (https://github.com/ruby-grape/grape-swagger/pull/846): Fixes oapi rake tasks, allows generating sepcs for different API versions.
127

138
### 1.4.2 (October 22, 2021)
149

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,9 +1718,10 @@ GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
17181718
```
17191719
rake oapi:fetch
17201720
params:
1721-
- store={ true | file_name } – save as JSON (optional)
1721+
- store={ true | file_name.json } – save as JSON (optional)
17221722
- resource=resource_name – get only for this one (optional)
17231723
```
1724+
For mutliversion API it creates several files with following naming: file_name_`API_VERSION`.json
17241725

17251726
#### OpenApi/Swagger Validation
17261727

lib/grape-swagger/rake/oapi_tasks.rb

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ def fetch
4646
resource - if given only for that it would be generated (optional)'
4747
task fetch: :environment do
4848
# :nocov:
49-
make_request
49+
urls_for(api_class).each do |url|
50+
make_request(url)
51+
52+
save_to_file? ? File.write(file(url), @oapi) : $stdout.print(@oapi)
53+
end
5054

51-
save_to_file? ? File.write(file, @oapi) : $stdout.print(@oapi)
5255
# :nocov:
5356
end
5457
end
@@ -64,34 +67,44 @@ def validate
6467
::Rake::Task['oapi:fetch'].invoke
6568
exit if error?
6669

67-
output = system "swagger-cli validate #{file}"
70+
urls_for(api_class).each do |url|
71+
@output = system "swagger-cli validate #{file(url)}"
72+
73+
FileUtils.rm(
74+
file(url)
75+
)
76+
end
6877

69-
$stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if output.nil?
70-
FileUtils.rm(file)
78+
$stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if @output.nil?
7179
# :nocov:
7280
end
7381
end
7482

7583
# helper methods
7684
#
7785
# rubocop:disable Style/StringConcatenation
78-
def make_request
79-
get url_for
86+
def make_request(url)
87+
get url
8088

8189
@oapi = JSON.pretty_generate(
82-
JSON.parse(
83-
last_response.body, symolize_names: true
84-
)
90+
JSON.parse(last_response.body, symolize_names: true)
8591
) + "\n"
8692
end
8793
# rubocop:enable Style/StringConcatenation
8894

89-
def url_for
90-
oapi_route = api_class.routes[-2]
91-
path = oapi_route.path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
92-
path.sub!(':version', oapi_route.version.to_s)
95+
def urls_for(api_class)
96+
api_class.routes
97+
.map(&:path)
98+
.select { |e| e.include?('doc') }
99+
.reject { |e| e.include?(':name') }
100+
.map { |e| format_path(e) }
101+
.map { |e| [e, ENV['resource']].join('/').chomp('/') }
102+
end
93103

94-
[path, ENV['resource']].join('/').chomp('/')
104+
def format_path(path)
105+
oapi_route = api_class.routes.select { |e| e.path == path }.first
106+
path = path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
107+
path.sub(':version', oapi_route.version.to_s)
95108
end
96109

97110
def save_to_file?
@@ -102,8 +115,15 @@ def error?
102115
JSON.parse(@oapi).keys.first == 'error'
103116
end
104117

105-
def file
106-
name = ENV['store'] == 'true' || ENV['store'].blank? ? 'swagger_doc.json' : ENV['store']
118+
def file(url)
119+
api_version = url.split('/').last
120+
121+
name = if ENV['store'] == 'true' || ENV['store'].blank?
122+
"swagger_doc_#{api_version}.json"
123+
else
124+
ENV['store'].sub('.json', "_#{api_version}.json")
125+
end
126+
107127
File.join(Dir.getwd, name)
108128
end
109129

spec/lib/oapi_tasks_spec.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class Base < Grape::API
2525

2626
subject { described_class.new(Api::Base) }
2727

28+
let(:api_class) { subject.send(:api_class) }
29+
let(:docs_url) { subject.send(:urls_for, api_class).first }
30+
2831
describe '.new' do
2932
it 'accepts class name as a constant' do
3033
expect(described_class.new(::Api::Base).send(:api_class)).to eq(Api::Base)
@@ -38,7 +41,7 @@ class Base < Grape::API
3841
describe '#make_request' do
3942
describe 'complete documentation' do
4043
before do
41-
subject.send(:make_request)
44+
subject.send(:make_request, docs_url)
4245
end
4346

4447
describe 'not storing' do
@@ -51,7 +54,7 @@ class Base < Grape::API
5154
end
5255

5356
it 'requests doc url' do
54-
expect(subject.send(:url_for)).to eql '/api/swagger_doc'
57+
expect(docs_url).to eql '/api/swagger_doc'
5558
end
5659
end
5760

@@ -68,10 +71,14 @@ class Base < Grape::API
6871
describe 'documentation for resource' do
6972
before do
7073
ENV['resource'] = resource
71-
subject.send(:make_request)
74+
subject.send(:make_request, docs_url)
7275
end
7376

74-
let(:response) { JSON.parse(subject.send(:make_request)) }
77+
let(:response) do
78+
JSON.parse(
79+
subject.send(:make_request, docs_url)
80+
)
81+
end
7582

7683
after { ENV.delete('resource') }
7784

@@ -83,7 +90,7 @@ class Base < Grape::API
8390
end
8491

8592
it 'requests doc url' do
86-
expect(subject.send(:url_for)).to eql "/api/swagger_doc/#{resource}"
93+
expect(docs_url).to eql "/api/swagger_doc/#{resource}"
8794
end
8895

8996
it 'has only one resource path' do
@@ -115,7 +122,7 @@ class Base < Grape::API
115122

116123
describe 'call it' do
117124
before do
118-
subject.send(:make_request)
125+
subject.send(:make_request, docs_url)
119126
end
120127
specify do
121128
expect(subject).to respond_to :oapi
@@ -128,7 +135,7 @@ class Base < Grape::API
128135
describe '#file' do
129136
describe 'no store given' do
130137
it 'returns swagger_doc.json' do
131-
expect(subject.send(:file)).to end_with 'swagger_doc.json'
138+
expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
132139
end
133140
end
134141

@@ -139,7 +146,7 @@ class Base < Grape::API
139146
before { ENV['store'] = 'true' }
140147

141148
it 'returns swagger_doc.json' do
142-
expect(subject.send(:file)).to end_with 'swagger_doc.json'
149+
expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
143150
end
144151
end
145152

@@ -148,7 +155,7 @@ class Base < Grape::API
148155
before { ENV['store'] = name }
149156

150157
it 'returns swagger_doc.json' do
151-
expect(subject.send(:file)).to end_with name
158+
expect(subject.send(:file, docs_url)).to include(name.split('.')[0])
152159
end
153160
end
154161
end

0 commit comments

Comments
 (0)