From aab9d26d7f548fbd8afeb72632a78484425f4de6 Mon Sep 17 00:00:00 2001 From: Jason Amerine Date: Wed, 27 Sep 2023 18:28:56 -0400 Subject: [PATCH] added configuration for path-based overrides --- lib/rspec/openapi.rb | 7 +++++++ lib/rspec/openapi/result_recorder.rb | 6 +++++- lib/rspec/openapi/rspec_hooks.rb | 7 +++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/rspec/openapi.rb b/lib/rspec/openapi.rb index c91b054e..5e70a774 100644 --- a/lib/rspec/openapi.rb +++ b/lib/rspec/openapi.rb @@ -22,6 +22,9 @@ module RSpec::OpenAPI @application_version = '1.0.0' @request_headers = [] @servers = [] + @config_filename = 'rspec_openapi.rb' + @path_servers = [] + @path_server_records = Hash.new @security_schemes = [] @example_types = %i[request] @response_headers = [] @@ -36,6 +39,10 @@ class << self :application_version, :request_headers, :servers, + :config_filename, + :schema_filename, + :path_servers, + :path_server_records, :security_schemes, :example_types, :response_headers, diff --git a/lib/rspec/openapi/result_recorder.rb b/lib/rspec/openapi/result_recorder.rb index 1ff6b560..82df282c 100644 --- a/lib/rspec/openapi/result_recorder.rb +++ b/lib/rspec/openapi/result_recorder.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true class RSpec::OpenAPI::ResultRecorder - def initialize(path_records) + def initialize(path_records, path_server_records) @path_records = path_records + @path_server_records = path_server_records @error_records = {} end @@ -10,6 +11,9 @@ def record_results! title = File.basename(Dir.pwd) @path_records.each do |path, records| RSpec::OpenAPI::SchemaFile.new(path).edit do |spec| + RSpec::OpenAPI.servers = @path_server_records[path].to_a if @path_server_records[path].present? + dir = path.match(/^(.*[\\\/])/)[0] + eval(File.read("#{dir}#{RSpec::OpenAPI.config_filename}")) if File.exist?("#{dir}#{RSpec::OpenAPI.config_filename}") schema = RSpec::OpenAPI::DefaultSchema.build(title) schema[:info].merge!(RSpec::OpenAPI.info) RSpec::OpenAPI::SchemaMerger.merge!(spec, schema) diff --git a/lib/rspec/openapi/rspec_hooks.rb b/lib/rspec/openapi/rspec_hooks.rb index 1ec7cb6f..47d8dbaa 100644 --- a/lib/rspec/openapi/rspec_hooks.rb +++ b/lib/rspec/openapi/rspec_hooks.rb @@ -7,15 +7,18 @@ path = RSpec::OpenAPI.path.yield_self { |p| p.is_a?(Proc) ? p.call(example) : p } record = RSpec::OpenAPI::RecordBuilder.build(self, example: example) RSpec::OpenAPI.path_records[path] << record if record + path_servers = RSpec::OpenAPI.path_servers.yield_self { |p| p.is_a?(Proc) ? p.call(example) : p } + RSpec::OpenAPI.path_server_records[path] = Set.new unless RSpec::OpenAPI.path_server_records[path] + RSpec::OpenAPI.path_server_records[path] += path_servers if path_servers end end RSpec.configuration.after(:suite) do - result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records) + result_recorder = RSpec::OpenAPI::ResultRecorder.new(RSpec::OpenAPI.path_records, RSpec::OpenAPI.path_server_records) result_recorder.record_results! if result_recorder.errors? error_message = result_recorder.error_message colorizer = RSpec::Core::Formatters::ConsoleCodes RSpec.configuration.reporter.message colorizer.wrap(error_message, :failure) end -end +end \ No newline at end of file