Class: Rswag::Specs::SwaggerFormatter
- Inherits:
-
RSpec::Core::Formatters::BaseTextFormatter
- Object
- RSpec::Core::Formatters::BaseTextFormatter
- Rswag::Specs::SwaggerFormatter
- Defined in:
- lib/rswag/specs/swagger_formatter.rb
Instance Method Summary collapse
- #example_group_finished(notification) ⇒ Object
-
#initialize(output, config = Rswag::Specs.config) ⇒ SwaggerFormatter
constructor
A new instance of SwaggerFormatter.
- #stop(_notification = nil) ⇒ Object
Constructor Details
#initialize(output, config = Rswag::Specs.config) ⇒ SwaggerFormatter
Returns a new instance of SwaggerFormatter.
14 15 16 17 18 19 |
# File 'lib/rswag/specs/swagger_formatter.rb', line 14 def initialize(output, config = Rswag::Specs.config) super(output) @config = config @output.puts 'Generating Swagger docs ...' end |
Instance Method Details
#example_group_finished(notification) ⇒ Object
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 |
# File 'lib/rswag/specs/swagger_formatter.rb', line 21 def example_group_finished(notification) = if RSPEC_VERSION > 2 notification.group. else notification. end # !metadata[:document] won't work, since nil means we should generate # docs. return if [:document] == false return unless .key?(:response) swagger_doc = @config.get_openapi_spec([:openapi_spec] || [:swagger_doc]) unless doc_version(swagger_doc).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!() upgrade_servers!(swagger_doc) upgrade_oauth!(swagger_doc) upgrade_response_produces!(swagger_doc, ) end swagger_doc.deep_merge!(()) end |
#stop(_notification = nil) ⇒ Object
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 91 92 93 94 95 96 97 98 99 |
# File 'lib/rswag/specs/swagger_formatter.rb', line 48 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] value[:requestBody][:description] = schema_param[:description] if schema_param[:description] examples = value.dig(:request_examples) mime_list.each do |mime| value[:requestBody][:content][mime] = { schema: schema_param[:schema] } if examples value[:requestBody][:content][mime][:examples] ||= {} examples.map do |example| value[:requestBody][:content][mime][:examples][example[:name]] = { summary: example[:summary] || value[:summary], value: example[:value] } end end end end enum_param = value.dig(:parameters).find{|p| p[:enum]} if enum_param && enum_param.is_a?(Hash) enum_param[:description] = generate_enum_description(enum_param) end value[:parameters].reject! { |p| p[:in] == :body || p[:in] == :formData } end remove_invalid_operation_keys!(value) end end end 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 end |