From 9c0878f2df61c602f2ddbf433492cb28343f99a5 Mon Sep 17 00:00:00 2001 From: Mercedes Bernard Date: Thu, 30 Nov 2023 16:40:27 -0600 Subject: [PATCH] add ability to configure which path params to ignore --- README.md | 5 +++++ lib/rspec/openapi.rb | 4 +++- lib/rspec/openapi/record_builder.rb | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3869fe2e..e73eb06c 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,11 @@ RSpec::OpenAPI.description_builder = -> (example) { example.description } # Change the example type(s) that will generate schema RSpec::OpenAPI.example_types = %i[request] + +# Configure which path params to ignore +# :controller and :action always exist. :format is added when routes is configured as such. +RSpec::OpenAPI.ignored_path_params = %i[controller action format] + ``` ### Can I use rspec-openapi with `$ref` to minimize duplication of schema? diff --git a/lib/rspec/openapi.rb b/lib/rspec/openapi.rb index c91b054e..edd70ae7 100644 --- a/lib/rspec/openapi.rb +++ b/lib/rspec/openapi.rb @@ -26,6 +26,7 @@ module RSpec::OpenAPI @example_types = %i[request] @response_headers = [] @path_records = Hash.new { |h, k| h[k] = [] } + @ignored_path_params = %i[controller action format] class << self attr_accessor :path, @@ -39,6 +40,7 @@ class << self :security_schemes, :example_types, :response_headers, - :path_records + :path_records, + :ignored_path_params end end diff --git a/lib/rspec/openapi/record_builder.rb b/lib/rspec/openapi/record_builder.rb index 751de5f6..f4686b23 100644 --- a/lib/rspec/openapi/record_builder.rb +++ b/lib/rspec/openapi/record_builder.rb @@ -83,7 +83,7 @@ def extract_request_attributes(request, example) tags ||= [route.requirements[:controller]&.classify].compact # :controller and :action always exist. :format is added when routes is configured as such. # TODO: Use .except(:controller, :action, :format) when we drop support for Ruby 2.x - raw_path_params = raw_path_params.slice(*(raw_path_params.keys - %i[controller action format])) + raw_path_params = raw_path_params.slice(*(raw_path_params.keys - RSpec::OpenAPI.ignored_path_params)) end summary ||= "#{request.method} #{path}" [path, summary, tags, operation_id, required_request_params, raw_path_params, description, security]