diff --git a/lib/rspec/openapi/extractors/hanami.rb b/lib/rspec/openapi/extractors/hanami.rb index 3f1155d..b100ae8 100644 --- a/lib/rspec/openapi/extractors/hanami.rb +++ b/lib/rspec/openapi/extractors/hanami.rb @@ -61,6 +61,7 @@ def request_attributes(request, example) tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example) operation_id = metadata[:operation_id] required_request_params = metadata[:required_request_params] || [] + optional_headers = metadata[:optional_headers] || [] security = metadata[:security] description = metadata[:description] || RSpec::OpenAPI.description_builder.call(example) deprecated = metadata[:deprecated] @@ -76,7 +77,7 @@ def request_attributes(request, example) raw_path_params = raw_path_params.slice(*(raw_path_params.keys - RSpec::OpenAPI.ignored_path_params)) - [path, summary, tags, operation_id, required_request_params, raw_path_params, description, security, deprecated] + [path, summary, tags, operation_id, required_request_params, optional_headers, raw_path_params, description, security, deprecated] end # @param [RSpec::ExampleGroups::*] context diff --git a/lib/rspec/openapi/extractors/rack.rb b/lib/rspec/openapi/extractors/rack.rb index 21b9752..bae1f6a 100644 --- a/lib/rspec/openapi/extractors/rack.rb +++ b/lib/rspec/openapi/extractors/rack.rb @@ -11,13 +11,14 @@ def request_attributes(request, example) tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example) operation_id = metadata[:operation_id] required_request_params = metadata[:required_request_params] || [] + optional_headers = metadata[:optional_headers] || [] security = metadata[:security] description = metadata[:description] || RSpec::OpenAPI.description_builder.call(example) deprecated = metadata[:deprecated] raw_path_params = request.path_parameters path = request.path summary ||= "#{request.method} #{path}" - [path, summary, tags, operation_id, required_request_params, raw_path_params, description, security, deprecated] + [path, summary, tags, operation_id, required_request_params, optional_headers, raw_path_params, description, security, deprecated] end # @param [RSpec::ExampleGroups::*] context diff --git a/lib/rspec/openapi/extractors/rails.rb b/lib/rspec/openapi/extractors/rails.rb index b5d1309..0ad829c 100644 --- a/lib/rspec/openapi/extractors/rails.rb +++ b/lib/rspec/openapi/extractors/rails.rb @@ -21,6 +21,7 @@ def request_attributes(request, example) tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example) operation_id = metadata[:operation_id] required_request_params = metadata[:required_request_params] || [] + optional_headers = metadata[:optional_headers] || [] security = metadata[:security] description = metadata[:description] || RSpec::OpenAPI.description_builder.call(example) deprecated = metadata[:deprecated] @@ -34,7 +35,7 @@ def request_attributes(request, example) summary ||= "#{request.method} #{path}" - [path, summary, tags, operation_id, required_request_params, raw_path_params, description, security, deprecated] + [path, summary, tags, operation_id, required_request_params, optional_headers, raw_path_params, description, security, deprecated] end # @param [RSpec::ExampleGroups::*] context diff --git a/lib/rspec/openapi/record.rb b/lib/rspec/openapi/record.rb index d336fdc..7fe7184 100644 --- a/lib/rspec/openapi/record.rb +++ b/lib/rspec/openapi/record.rb @@ -9,6 +9,7 @@ :required_request_params, # @param [Array] - ["param1", "param2"] :request_content_type, # @param [String] - "application/json" :request_headers, # @param [Array] - [["header_key1", "header_value1"], ["header_key2", "header_value2"]] + :optional_headers, # @param [Array] - ["header1", "header2"] :summary, # @param [String] - "v1/statuses #show" :tags, # @param [Array] - ["Status"] :operation_id, # @param [String] - "request-1234" diff --git a/lib/rspec/openapi/record_builder.rb b/lib/rspec/openapi/record_builder.rb index 32adf97..fad429f 100644 --- a/lib/rspec/openapi/record_builder.rb +++ b/lib/rspec/openapi/record_builder.rb @@ -11,7 +11,7 @@ def build(context, example:, extractor:) request, response = extractor.request_response(context) return if request.nil? - path, summary, tags, operation_id, required_request_params, raw_path_params, description, security, deprecated = + path, summary, tags, operation_id, required_request_params, optional_headers, raw_path_params, description, security, deprecated = extractor.request_attributes(request, example) return if RSpec::OpenAPI.ignored_paths.any? { |ignored_path| path.match?(ignored_path) } @@ -25,6 +25,7 @@ def build(context, example:, extractor:) query_params: request.query_parameters, request_params: raw_request_params(request), required_request_params: required_request_params, + optional_headers: optional_headers, request_content_type: request.media_type, request_headers: request_headers, summary: summary, diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index 49b5272..46583f4 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -88,7 +88,7 @@ def build_parameters(record) { name: build_parameter_name(key, value), in: 'header', - required: true, + required: record.optional_headers.exclude?(key), schema: build_property(try_cast(value)), example: (try_cast(value) if example_enabled?), }.compact diff --git a/lib/rspec/openapi/schema_file.rb b/lib/rspec/openapi/schema_file.rb index 53f14f9..8c1f62e 100644 --- a/lib/rspec/openapi/schema_file.rb +++ b/lib/rspec/openapi/schema_file.rb @@ -24,7 +24,11 @@ def edit(&block) def read return {} unless File.exist?(@path) - RSpec::OpenAPI::KeyTransformer.symbolize(YAML.safe_load(File.read(@path))) # this can also parse JSON + content = YAML.safe_load(File.read(@path)) # This can also parse JSON + + return {} if content.nil? + + RSpec::OpenAPI::KeyTransformer.symbolize(content) end # @param [Hash] spec