diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c931cbb..dffc5810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,9 +22,12 @@ ### Removed - Removed deprecated methods which produced a warning since 2.0.0. - Removed internally used `Test::Coverage.current_run, .plans, .install, .uninstall`. If you are using these, use `OpenapiFirst::Test.setup` instead. +- Removed `OpenapiFirst::Configuration#clone`. Use `#child` instead. +- It's not possible to remove locally added hooks. But you can restart with a blank list of hooks by calling `OpenapiFirst.configure` ### Fixed - Update dependency `openapi_parameters` to >= 0.7.0, because that version supports unpacking parameters the use `style: deepObject` with `explode: true`. +- Make `OpenapiFirst::Test.setup` more robust by adding `OpenapiFirst::Configuration#child` so it does not matter if you load our OAD before callig `OpenapiFirst::Test.setup`. ## 2.11.1 diff --git a/lib/openapi_first/configuration.rb b/lib/openapi_first/configuration.rb index 497f4084..dbdfff82 100644 --- a/lib/openapi_first/configuration.rb +++ b/lib/openapi_first/configuration.rb @@ -25,10 +25,16 @@ def register(path_or_definition, as: :default) attr_reader :request_validation_error_response, :hooks attr_accessor :request_validation_raise_error, :response_validation_raise_error, :path + # Return a child configuration that still receives updates of global hooks. def child ChildConfiguration.new(parent: self) end + # @visibility private + def clone + raise NoMethodError, 'OpenapiFirst::Configuration#clone was removed. You want to call #child instead' + end + HOOKS.each do |hook| define_method(hook) do |&block| return hooks[hook] if block.nil? diff --git a/lib/openapi_first/definition.rb b/lib/openapi_first/definition.rb index 36fb4bbd..dada7c64 100644 --- a/lib/openapi_first/definition.rb +++ b/lib/openapi_first/definition.rb @@ -22,7 +22,7 @@ class Definition # @param filepath [String] The file path of the OpenAPI document. def initialize(contents, filepath = nil) @filepath = filepath - @config = OpenapiFirst.configuration.clone + @config = OpenapiFirst.configuration.child yield @config if block_given? @config.freeze @router = Builder.build_router(contents, filepath:, config:) diff --git a/spec/hooks_spec.rb b/spec/hooks_spec.rb index 61e3ac2a..07e07463 100644 --- a/spec/hooks_spec.rb +++ b/spec/hooks_spec.rb @@ -5,7 +5,7 @@ def build_request(path, method: 'GET', body: nil) Rack::Request.new(Rack::MockRequest.env_for(path, method:, input: body, 'CONTENT_TYPE' => 'application/json')) end - describe 'adding and removing a hook' do + describe 'adding a hook' do specify do called = [] myproc = proc do |request| @@ -16,8 +16,6 @@ def build_request(path, method: 'GET', body: nil) config.after_request_validation(&myproc) end definition.validate_request(build_request('/pets?limit=24')) - definition.config.after_request_validation.delete(myproc) - definition.validate_request(build_request('/pets?limit=24')) expect(called).to eq([['listPets', true]]) end