Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor oapi fetch task #846

Merged
merged 8 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-06-05 12:13:59 +0000 using RuboCop version 0.71.0.
# on 2022-01-14 10:22:29 UTC using RuboCop version 1.24.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequireMFA:
Exclude:
- 'grape-swagger.gemspec'

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'grape-swagger.gemspec'

# Offense count: 30
# Offense count: 31
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 59

# Offense count: 10
Metrics/CyclomaticComplexity:
Max: 13
Max: 56

# Offense count: 22
# Configuration parameters: CountComments, ExcludedMethods.
# Offense count: 30
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 45
Max: 28

# Offense count: 7
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 16

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

# Offense count: 22
# Offense count: 23
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false

# Offense count: 43
Style/OpenStructUse:
Exclude:
- 'spec/lib/endpoint_spec.rb'
- 'spec/lib/version_spec.rb'
- 'spec/support/mock_parser.rb'
- 'spec/support/model_parsers/mock_parser.rb'
- 'spec/swagger_v2/api_swagger_v2_hide_documentation_path_spec.rb'
- 'spec/swagger_v2/api_swagger_v2_mounted_spec.rb'
- 'spec/swagger_v2/api_swagger_v2_spec.rb'
- 'spec/swagger_v2/errors_spec.rb'
- 'spec/swagger_v2/reference_entity_spec.rb'
LeFnord marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 2 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
### Next

#### Features

* Your contribution here.
### 1.4.3 (January 5, 2022)

#### Fixes

* [#850](https://github.com/ruby-grape/grape-swagger/pull/850): Fix value of `enum` to be `Array` - [@takahashim](https://github.com/takahashim)
* Your contribution here.

* [#846] (https://github.com/ruby-grape/grape-swagger/pull/846): Fixes oapi rake tasks, allows generating sepcs for different API versions.

### 1.4.2 (October 22, 2021)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1718,9 +1718,10 @@ GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
```
rake oapi:fetch
params:
- store={ true | file_name } – save as JSON (optional)
- store={ true | file_name.json } – save as JSON (optional)
- resource=resource_name – get only for this one (optional)
```
For mutliversion API it creates several files with following naming: file_name_`API_VERSION`.json

#### OpenApi/Swagger Validation

Expand Down
54 changes: 37 additions & 17 deletions lib/grape-swagger/rake/oapi_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ def fetch
resource - if given only for that it would be generated (optional)'
task fetch: :environment do
# :nocov:
make_request
urls_for(api_class).each do |url|
make_request(url)

save_to_file? ? File.write(file(url), @oapi) : $stdout.print(@oapi)
end

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

output = system "swagger-cli validate #{file}"
urls_for(api_class).each do |url|
@output = system "swagger-cli validate #{file(url)}"

FileUtils.rm(
file(url)
)
end

$stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if output.nil?
FileUtils.rm(file)
$stdout.puts 'install swagger-cli with `npm install swagger-cli -g`' if @output.nil?
# :nocov:
end
end

# helper methods
#
# rubocop:disable Style/StringConcatenation
def make_request
get url_for
def make_request(url)
get url

@oapi = JSON.pretty_generate(
JSON.parse(
last_response.body, symolize_names: true
)
JSON.parse(last_response.body, symolize_names: true)
) + "\n"
end
# rubocop:enable Style/StringConcatenation

def url_for
oapi_route = api_class.routes[-2]
path = oapi_route.path.sub(/\(\.\w+\)$/, '').sub(/\(\.:\w+\)$/, '')
path.sub!(':version', oapi_route.version.to_s)
def urls_for(api_class)
api_class.routes
.map(&:path)
.select { |e| e.include?('doc') }
.reject { |e| e.include?(':name') }
.map { |e| format_path(e) }
.map { |e| [e, ENV['resource']].join('/').chomp('/') }
end

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

def save_to_file?
Expand All @@ -102,8 +115,15 @@ def error?
JSON.parse(@oapi).keys.first == 'error'
end

def file
name = ENV['store'] == 'true' || ENV['store'].blank? ? 'swagger_doc.json' : ENV['store']
def file(url)
api_version = url.split('/').last

name = if ENV['store'] == 'true' || ENV['store'].blank?
"swagger_doc_#{api_version}.json"
else
ENV['store'].sub('.json', "_#{api_version}.json")
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a breaking change to me, which made it into v1.5.0


File.join(Dir.getwd, name)
end

Expand Down
25 changes: 16 additions & 9 deletions spec/lib/oapi_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Base < Grape::API

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

let(:api_class) { subject.send(:api_class) }
let(:docs_url) { subject.send(:urls_for, api_class).first }

describe '.new' do
it 'accepts class name as a constant' do
expect(described_class.new(::Api::Base).send(:api_class)).to eq(Api::Base)
Expand All @@ -38,7 +41,7 @@ class Base < Grape::API
describe '#make_request' do
describe 'complete documentation' do
before do
subject.send(:make_request)
subject.send(:make_request, docs_url)
end

describe 'not storing' do
Expand All @@ -51,7 +54,7 @@ class Base < Grape::API
end

it 'requests doc url' do
expect(subject.send(:url_for)).to eql '/api/swagger_doc'
expect(docs_url).to eql '/api/swagger_doc'
end
end

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

let(:response) { JSON.parse(subject.send(:make_request)) }
let(:response) do
JSON.parse(
subject.send(:make_request, docs_url)
)
end

after { ENV.delete('resource') }

Expand All @@ -83,7 +90,7 @@ class Base < Grape::API
end

it 'requests doc url' do
expect(subject.send(:url_for)).to eql "/api/swagger_doc/#{resource}"
expect(docs_url).to eql "/api/swagger_doc/#{resource}"
end

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

describe 'call it' do
before do
subject.send(:make_request)
subject.send(:make_request, docs_url)
end
specify do
expect(subject).to respond_to :oapi
Expand All @@ -128,7 +135,7 @@ class Base < Grape::API
describe '#file' do
describe 'no store given' do
it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with 'swagger_doc.json'
expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
end
end

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

it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with 'swagger_doc.json'
expect(subject.send(:file, docs_url)).to end_with 'swagger_doc.json'
end
end

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

it 'returns swagger_doc.json' do
expect(subject.send(:file)).to end_with name
expect(subject.send(:file, docs_url)).to include(name.split('.')[0])
end
end
end
Expand Down