diff --git a/README.md b/README.md index d0b86ae..f6db531 100644 --- a/README.md +++ b/README.md @@ -284,6 +284,22 @@ describe 'Blogs API' do end ``` +### Enable generation examples from responses ### + +To enable examples generation from responses add callback above run_test! like: +```ruby +after do |example| + example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) } +end +``` +You need to disable --dry-run option for Rspec > 3 + +Add to application.rb: +```ruby +RSpec.configure do |config| + config.swagger_dry_run = false +end +``` ### Route Prefix for Swagger JSON Endpoints ### The functionality to expose Swagger files, such as those generated by rswag-specs, as JSON endpoints is implemented as a Rails Engine. As with any Engine, you can change it's mount prefix in _routes.rb_: diff --git a/rswag-specs/lib/rswag/specs.rb b/rswag-specs/lib/rswag/specs.rb index e4f2e1b..8aeb768 100644 --- a/rswag-specs/lib/rswag/specs.rb +++ b/rswag-specs/lib/rswag/specs.rb @@ -12,6 +12,7 @@ module Rswag ::RSpec.configure do |c| c.add_setting :swagger_root c.add_setting :swagger_docs + c.add_setting :swagger_dry_run c.extend ExampleGroupHelpers, type: :request c.include ExampleHelpers, type: :request end diff --git a/rswag-specs/lib/rswag/specs/configuration.rb b/rswag-specs/lib/rswag/specs/configuration.rb index 121caf0..4adf33c 100644 --- a/rswag-specs/lib/rswag/specs/configuration.rb +++ b/rswag-specs/lib/rswag/specs/configuration.rb @@ -25,6 +25,12 @@ module Rswag end end + def swagger_dry_run + @swagger_dry_run ||= begin + @rspec_config.swagger_dry_run.nil? || @rspec_config.swagger_dry_run + end + end + def get_swagger_doc(name) return swagger_docs.values.first if name.nil? raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name] diff --git a/rswag-specs/lib/tasks/rswag-specs_tasks.rake b/rswag-specs/lib/tasks/rswag-specs_tasks.rake index 646af02..adc128c 100644 --- a/rswag-specs/lib/tasks/rswag-specs_tasks.rake +++ b/rswag-specs/lib/tasks/rswag-specs_tasks.rake @@ -8,7 +8,7 @@ namespace :rswag do t.pattern = 'spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb' # NOTE: rspec 2.x support - if Rswag::Specs::RSPEC_VERSION > 2 + if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined' ] else t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--order defined' ]