More useful error messages in rswag-specs

This commit is contained in:
richie
2016-10-13 16:56:44 -07:00
parent f1850bc6d0
commit b81b2927be
10 changed files with 187 additions and 132 deletions

View File

@@ -0,0 +1,37 @@
module Rswag
module Specs
class Configuration
def initialize(rspec_config)
@rspec_config = rspec_config
end
def swagger_root
@swagger_root ||= begin
if @rspec_config.swagger_root.nil?
raise ConfigurationError, 'No swagger_root provided. See swagger_helper.rb'
end
@rspec_config.swagger_root
end
end
def swagger_docs
@swagger_docs ||= begin
if @rspec_config.swagger_docs.nil? || @rspec_config.swagger_docs.empty?
raise ConfigurationError, 'No swagger_docs defined. See swagger_helper.rb'
end
@rspec_config.swagger_docs
end
end
def get_swagger_doc(name)
return swagger_docs.values.first if name.nil?
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
swagger_docs[name]
end
end
class ConfigurationError < StandardError; end
end
end

View File

@@ -6,7 +6,7 @@ module Rswag
module ExampleHelpers
def submit_request(api_metadata)
factory = RequestFactory.new(api_metadata, global_metadata(api_metadata[:swagger_doc]))
factory = RequestFactory.new(api_metadata, config.get_swagger_doc(api_metadata[:swagger_doc]))
if RAILS_VERSION < 5
send(
@@ -28,15 +28,14 @@ module Rswag
end
def assert_response_matches_metadata(api_metadata)
validator = ResponseValidator.new(api_metadata, global_metadata(api_metadata[:swagger_doc]))
validator = ResponseValidator.new(api_metadata, config.get_swagger_doc(api_metadata[:swagger_doc]))
validator.validate!(response)
end
private
def global_metadata(swagger_doc)
swagger_docs = ::RSpec.configuration.swagger_docs
swagger_doc.nil? ? swagger_docs.values.first : swagger_docs[swagger_doc]
def config
::Rswag::Specs.config
end
end
end

View File

@@ -1,5 +1,4 @@
require 'active_support/core_ext/hash/deep_merge'
require 'rspec/core/formatters/base_text_formatter'
require 'swagger_helper'
module Rswag
@@ -11,12 +10,9 @@ module Rswag
::RSpec::Core::Formatters.register self, :example_group_finished, :stop
end
def initialize(output)
def initialize(output, config = Rswag::Specs.config)
@output = output
@swagger_root = ::RSpec.configuration.swagger_root
raise ConfigurationError, 'Missing swagger_root. See swagger_helper.rb' if @swagger_root.nil?
@swagger_docs = ::RSpec.configuration.swagger_docs || []
raise ConfigurationError, 'Missing swagger_docs. See swagger_helper.rb' if @swagger_docs.empty?
@config = config
@output.puts 'Generating Swagger docs ...'
end
@@ -30,13 +26,13 @@ module Rswag
end
return unless metadata.has_key?(:response)
swagger_doc = get_swagger_doc(metadata[:swagger_doc])
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
end
def stop(notification=nil)
@swagger_docs.each do |url_path, doc|
file_path = File.join(@swagger_root, url_path)
@config.swagger_docs.each do |url_path, doc|
file_path = File.join(@config.swagger_root, url_path)
dirname = File.dirname(file_path)
FileUtils.mkdir_p dirname unless File.exists?(dirname)
@@ -50,12 +46,6 @@ module Rswag
private
def get_swagger_doc(tag)
return @swagger_docs.values.first if tag.nil?
raise ConfigurationError, "Unknown swagger_doc '#{tag}'" unless @swagger_docs.has_key?(tag)
@swagger_docs[tag]
end
def metadata_to_swagger(metadata)
response_code = metadata[:response][:code]
response = metadata[:response].reject { |k,v| k == :code }
@@ -73,7 +63,5 @@ module Rswag
}
end
end
class ConfigurationError < StandardError; end
end
end