lib/rspec/rails/api/open_api_renderer.rb in rspec-rails-api-0.3.1 vs lib/rspec/rails/api/open_api_renderer.rb in rspec-rails-api-0.3.2
- old
+ new
@@ -37,14 +37,16 @@
def write_files(path = nil, only: %i[yaml json])
content = prepare_metadata
path ||= ::Rails.root.join('tmp', 'rspec_api_rails')
+ file_types = %i[yaml json]
+
only.each do |type|
- next unless %i[yaml json].include? type
+ next unless file_types.include? type
- File.write "#{path}.#{type}", content.send("to_#{type}")
+ File.write "#{path}.#{type}", JSON.parse(JSON.pretty_generate(content)).send("to_#{type}")
end
end
def prepare_metadata
# Example: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v3.0/petstore-expanded.yaml
@@ -87,17 +89,18 @@
process_resource resource: resource_key, resource_config: resource
end
end
def process_resource(resource: nil, resource_config: nil) # rubocop:disable Metrics/MethodLength
+ http_verbs = %i[get post put patch delete]
resource_config[:paths].each do |path_key, path|
url = path_with_params path_key.to_s
actions = {}
parameters = path.key?(:path_params) ? process_path_params(path[:path_params]) : []
path[:actions].each_key do |action|
- next unless %i[get post put patch delete].include? action
+ next unless http_verbs.include? action
actions[action] = process_action resource: resource,
path: path_key,
path_config: path,
action_config: action,
@@ -115,11 +118,11 @@
end
parameters
end
- def process_path_param(name, param) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
+ def process_path_param(name, param) # rubocop:disable Metrics/MethodLength
parameter = {
name: name.to_s,
description: param[:description],
required: param[:required] || true,
in: param[:scope].to_s,
@@ -136,17 +139,16 @@
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def process_action(resource: nil, path: nil, path_config: nil, action_config: nil, parameters: nil)
responses = {}
request_body = nil
- if %i[post put patch].include? action_config
- if path_config[:actions][action_config][:params].keys.count.positive?
- schema = path_config[:actions][action_config][:params]
- schema_ref = escape_operation_id("#{action_config}_#{path}")
- examples = process_examples(path_config[:actions][action_config][:statuses])
- request_body = process_request_body schema: schema, ref: schema_ref, examples: examples
- end
+ if %i[post put
+ patch].include?(action_config) && path_config[:actions][action_config][:params].keys.count.positive?
+ schema = path_config[:actions][action_config][:params]
+ schema_ref = escape_operation_id("#{action_config}_#{path}")
+ examples = process_examples(path_config[:actions][action_config][:statuses])
+ request_body = process_request_body schema: schema, ref: schema_ref, examples: examples
end
path_config[:actions][action_config][:statuses].each do |status_key, status|
content = status[:example][:response]
responses[status_key] = process_response status: status_key, status_config: status, content: content
@@ -188,11 +190,11 @@
return response if status.to_s == '204' && content # No content
response[:content] = {
'application/json': {
- examples: { default: { value: JSON.pretty_generate(JSON.parse(content)) } },
+ examples: { default: { value: JSON.parse(content) } },
},
}
response
end
@@ -218,10 +220,10 @@
def escape_operation_id(string)
string.downcase.gsub(/[^\w]+/, '_')
end
- def api_infos # rubocop:disable Metrics/CyclomaticComplexity
+ def api_infos
@api_infos = {
title: @api_title || 'Some sample app',
version: @api_version || '1.0',
}
@api_infos[:description] = @api_description if @api_description