-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathrswag_override.rb
90 lines (78 loc) · 3.55 KB
/
rswag_override.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
module Rswag
module Specs
class SwaggerFormatter < ::RSpec::Core::Formatters::BaseTextFormatter
def example_group_finished(notification)
metadata = if RSPEC_VERSION > 2
notification.group.metadata
else
notification.metadata
end
# !metadata[:document] won't work, since nil means we should generate
# docs.
return if metadata[ENV['DOCUMENTATION_ENVIRONMENT']&.to_sym] == false
return if metadata[:document] == false
return unless metadata.key?(:response)
openapi_spec = @config.get_openapi_spec(metadata[:openapi_spec])
puts "metadata[:swagger_doc] => #{metadata[:swagger_doc]}" if metadata[:swagger_doc].present?
unless doc_version(openapi_spec).start_with?('2')
# This is called multiple times per file!
# metadata[:operation] is also re-used between examples within file
# therefore be careful NOT to modify its content here.
upgrade_request_type!(metadata)
upgrade_servers!(openapi_spec)
upgrade_oauth!(openapi_spec)
upgrade_response_produces!(openapi_spec, metadata)
end
openapi_spec.deep_merge!(metadata_to_swagger(metadata))
end
# rubocop:disable Metrics/BlockNesting, Layout/LineLength, Style/CommentedKeyword, Metrics/MethodLength
def stop(_notification = nil)
@config.openapi_specs.each do |url_path, doc|
unless doc_version(doc).start_with?('2')
doc[:paths]&.each_pair do |_k, v|
v.each_pair do |_verb, value|
is_hash = value.is_a?(Hash)
if is_hash && value[:parameters]
schema_param = value[:parameters]&.find { |p| (p[:in] == :body || p[:in] == :formData) && p[:schema] }
mime_list = value[:consumes] || doc[:consumes]
if value && schema_param && mime_list
value[:requestBody] = { content: {} } unless value.dig(:requestBody, :content)
value[:requestBody][:required] = true if schema_param[:required]
mime_list.each do |mime|
value[:requestBody][:content][mime] = { schema: schema_param[:schema] }.merge(request_examples(value)) # Changed line
end
end
value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData }
end
remove_invalid_operation_keys!(value)
end
end
end
if relevant_path?(url_path) # Added conditional
file_path = File.join(@config.openapi_root, url_path)
dirname = File.dirname(file_path)
FileUtils.mkdir_p dirname unless File.exist?(dirname)
File.open(file_path, 'w') do |file|
file.write(pretty_generate(doc))
end
@output.puts "Swagger doc generated at #{file_path}"
end # Added conditional
end
end
# rubocop:enable Metrics/BlockNesting, Layout/LineLength, Style/CommentedKeyword, Metrics/MethodLength
private # Added methods
def request_examples(value)
examples = value[:parameters]&.find { |p| (p[:in] == :body || p[:in] == :formData) && p[:examples] }
if examples && examples[:examples]
{ examples: examples[:examples] }
else
{}
end
end
def relevant_path?(url_path)
url_path.include?(ENV.fetch('RAILS_MODULE'))
end
end
end
end