Class: OpenApi::Rswag::Specs::SwaggerFormatter
- Inherits:
-
Object
- Object
- OpenApi::Rswag::Specs::SwaggerFormatter
- Defined in:
- lib/open_api/rswag/specs/swagger_formatter.rb
Instance Method Summary collapse
- #example_group_finished(notification) ⇒ Object
-
#initialize(output, config = ::OpenApi::Rswag::Specs.config) ⇒ SwaggerFormatter
constructor
A new instance of SwaggerFormatter.
- #stop(_notification = nil) ⇒ Object
Constructor Details
#initialize(output, config = ::OpenApi::Rswag::Specs.config) ⇒ SwaggerFormatter
Returns a new instance of SwaggerFormatter.
15 16 17 18 19 20 |
# File 'lib/open_api/rswag/specs/swagger_formatter.rb', line 15 def initialize(output, config = ::OpenApi::Rswag::Specs.config) @output = output @config = config @output.puts 'Generating Swagger docs ...' end |
Instance Method Details
#example_group_finished(notification) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/open_api/rswag/specs/swagger_formatter.rb', line 22 def example_group_finished(notification) # NOTE: rspec 2.x support = if RSPEC_VERSION > 2 notification.group. else notification. end return unless .key?(:response) swagger_doc = @config.get_swagger_doc([:swagger_doc]) swagger_doc.deep_merge!(()) end |
#stop(_notification = nil) ⇒ Object
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 |
# File 'lib/open_api/rswag/specs/swagger_formatter.rb', line 36 def stop(_notification = nil) @config.swagger_docs.each do |url_path, doc| # remove 2.0 parameters doc[:paths]&.each_pair do |_k, v| v.each_pair do |_verb, value| is_hash = value.is_a?(Hash) if is_hash && value.dig(:parameters) schema_param = value&.dig(:parameters)&.find{|p| p[:in] == :body && p[:schema] } if value && schema_param && value&.dig(:requestBody, :content, 'application/json') value[:requestBody][:content]['application/json'].merge!(schema: schema_param[:schema]) end value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData } value[:parameters].each { |p| p.delete(:type) } value[:headers].each { |p| p.delete(:type)} if value[:headers] end value.delete(:consumes) if is_hash && value.dig(:consumes) value.delete(:produces) if is_hash && value.dig(:produces) end end file_path = File.join(@config.swagger_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(JSON.pretty_generate(doc)) end @output.puts "Swagger doc generated at #{file_path}" end end |