Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes header params optional and handle empty files gracefully #236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/rspec/openapi/extractors/hanami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/openapi/extractors/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/openapi/extractors/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/rspec/openapi/schema_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down