Module: OpenapiRuby::Adapters::RSpec

Defined in:
lib/openapi_ruby/adapters/rspec.rb

Defined Under Namespace

Modules: ExampleGroupHelpers, ExampleHelpers

Class Method Summary collapse

Class Method Details

.install!Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/openapi_ruby/adapters/rspec.rb', line 496

def self.install!
  ::RSpec.configure do |config|
    config.extend ExampleGroupHelpers, type: :openapi
    config.include ExampleHelpers, type: :openapi

    if defined?(::RSpec::Rails)
      config.include ::RSpec::Rails::RequestExampleGroup, type: :openapi
    elsif OpenapiRuby.hanami_host?
      require "openapi_ruby/hanami"
      OpenapiRuby::Hanami.install_rspec!(config)
    else
      install_rack_test!(config)
    end

    # Schema writing is handled by the rake task (openapi_ruby:generate),
    # not by test runs. The rake task loads spec files to register DSL
    # contexts, then calls SchemaWriter.generate_all! directly.
    # This prevents partial schema overwrites when running a subset of specs.
  end
end

.install_rack_test!(config, app_module: nil) ⇒ Object

Every host but Rails drives requests through rack-test. Hanami also gets a default app; elsewhere (Sinatra, Roda, bare Rack) there is no convention for which app is under test, so the suite defines it.



485
486
487
488
489
490
491
492
493
494
# File 'lib/openapi_ruby/adapters/rspec.rb', line 485

def self.install_rack_test!(config, app_module: nil)
  require "rack/test"

  config.include ::Rack::Test::Methods, type: :openapi
  config.include app_module, type: :openapi if app_module
rescue LoadError
  # No rack-test in the bundle. Testing::Transport raises with setup
  # instructions if a spec then tries to issue a request.
  nil
end

.validation_document_for(schema_name) ⇒ Object

Build the OpenAPI document hash for a given schema name and cache it. Used by response body validation so $ref schemas can be resolved.



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/openapi_ruby/adapters/rspec.rb', line 461

def self.validation_document_for(schema_name)
  return nil unless schema_name

  key = schema_name.to_sym
  @validation_documents ||= {}
  @validation_documents[key] ||= begin
    config = OpenapiRuby.configuration
    schema_config = config.schemas[key] || config.schemas[schema_name.to_s]
    return nil unless schema_config

    builder = OpenapiRuby::Core::DocumentBuilder.new(schema_config)
    OpenapiRuby::DSL::MetadataStore.contexts_for(schema_name).each do |context|
      builder.add_path(context.path_template, context.to_openapi)
    end
    scope = schema_config[:component_scope]
    loader = OpenapiRuby::Components::Loader.new(scope: scope)
    builder.merge_components(loader.to_openapi_hash)
    builder.build.data
  end
end