Allow passing class name as a string for rake task initializer (#782)
It may be possible that API classes are not yet loaded when the oapi rake tasks are initialized in Rakefile. Add a possibility to pass class name as a string to not mess with class loader configuration. Co-authored-by: Mikhail Doronin <mikhail.doronin@capitainetrain.com>
This commit is contained in:
parent
2d471d0b77
commit
05939a068a
|
@ -3,6 +3,8 @@
|
|||
#### Features
|
||||
|
||||
* Your contribution here.
|
||||
* [#782](https://github.com/ruby-grape/grape-swagger/pull/782): Allow passing class name as string for rake task initializer - [@misdoro](https://github.com/misdoro).
|
||||
|
||||
|
||||
#### Fixes
|
||||
|
||||
|
|
|
@ -1486,13 +1486,19 @@ end
|
|||
|
||||
## Rake Tasks <a name="rake"></a>
|
||||
|
||||
Add these lines to your Rakefile, and initialize the Task class with your Api class – be sure your Api class is available.
|
||||
Add these lines to your Rakefile, and initialize the Task class with your Api class.
|
||||
|
||||
```ruby
|
||||
require 'grape-swagger/rake/oapi_tasks'
|
||||
GrapeSwagger::Rake::OapiTasks.new(::Api::Base)
|
||||
```
|
||||
|
||||
You may initialize with the class name as a string if the class is not yet loaded at the time Rakefile is parsed:
|
||||
```ruby
|
||||
require 'grape-swagger/rake/oapi_tasks'
|
||||
GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
|
||||
```
|
||||
|
||||
#### OpenApi/Swagger Documentation
|
||||
|
||||
```
|
||||
|
|
|
@ -10,17 +10,25 @@ module GrapeSwagger
|
|||
include Rack::Test::Methods
|
||||
|
||||
attr_reader :oapi
|
||||
attr_reader :api_class
|
||||
|
||||
def initialize(api_class)
|
||||
super()
|
||||
|
||||
if api_class.is_a? String
|
||||
@api_class_name = api_class
|
||||
else
|
||||
@api_class = api_class
|
||||
end
|
||||
|
||||
define_tasks
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def api_class
|
||||
@api_class ||= @api_class_name.constantize
|
||||
end
|
||||
|
||||
def define_tasks
|
||||
namespace :oapi do
|
||||
fetch
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
require 'spec_helper'
|
||||
|
||||
RSpec.describe GrapeSwagger::Rake::OapiTasks do
|
||||
let(:api) do
|
||||
item = Class.new(Grape::API) do
|
||||
module Api
|
||||
class Item < Grape::API
|
||||
version 'v1', using: :path
|
||||
|
||||
namespace :item do
|
||||
|
@ -16,14 +16,24 @@ RSpec.describe GrapeSwagger::Rake::OapiTasks do
|
|||
end
|
||||
end
|
||||
|
||||
Class.new(Grape::API) do
|
||||
class Base < Grape::API
|
||||
prefix :api
|
||||
mount item
|
||||
mount Api::Item
|
||||
add_swagger_documentation add_version: true
|
||||
end
|
||||
end
|
||||
|
||||
subject { described_class.new(api) }
|
||||
subject { described_class.new(Api::Base) }
|
||||
|
||||
describe '.new' do
|
||||
it 'accepts class name as a constant' do
|
||||
expect(described_class.new(::Api::Base).send(:api_class)).to eq(Api::Base)
|
||||
end
|
||||
|
||||
it 'accepts class name as a string' do
|
||||
expect(described_class.new('::Api::Base').send(:api_class)).to eq(Api::Base)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#make_request' do
|
||||
describe 'complete documentation' do
|
||||
|
|
Loading…
Reference in New Issue