mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-22 22:06:43 +00:00
Basic rubocops
This commit is contained in:
parent
5060697761
commit
6b4f49aacb
@ -20,8 +20,4 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|||||||
rdoc.rdoc_files.include('lib/**/*.rb')
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Bundler::GemHelper.install_tasks
|
Bundler::GemHelper.install_tasks
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rswag/route_parser'
|
require 'rswag/route_parser'
|
||||||
require 'rails/generators'
|
require 'rails/generators'
|
||||||
|
|
||||||
module Rspec
|
module Rspec
|
||||||
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
||||||
source_root File.expand_path('../templates', __FILE__)
|
source_root File.expand_path('templates', __dir__)
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@routes = Rswag::RouteParser.new(controller_path).routes
|
@routes = Rswag::RouteParser.new(controller_path).routes
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rails/generators'
|
require 'rails/generators'
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
class InstallGenerator < Rails::Generators::Base
|
class InstallGenerator < Rails::Generators::Base
|
||||||
source_root File.expand_path('../templates', __FILE__)
|
source_root File.expand_path('templates', __dir__)
|
||||||
|
|
||||||
def add_swagger_helper
|
def add_swagger_helper
|
||||||
template('swagger_helper.rb', 'spec/swagger_helper.rb')
|
template('swagger_helper.rb', 'spec/swagger_helper.rb')
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
RSpec.configure do |config|
|
RSpec.configure do |config|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
class RouteParser
|
class RouteParser
|
||||||
attr_reader :controller
|
attr_reader :controller
|
||||||
@ -9,7 +11,7 @@ module Rswag
|
|||||||
def routes
|
def routes
|
||||||
::Rails.application.routes.routes.select do |route|
|
::Rails.application.routes.routes.select do |route|
|
||||||
route.defaults[:controller] == controller
|
route.defaults[:controller] == controller
|
||||||
end.reduce({}) do |tree, route|
|
end.each_with_object({}) do |tree, route|
|
||||||
path = path_from(route)
|
path = path_from(route)
|
||||||
verb = verb_from(route)
|
verb = verb_from(route)
|
||||||
tree[path] ||= { params: params_from(route), actions: {} }
|
tree[path] ||= { params: params_from(route), actions: {} }
|
||||||
@ -28,7 +30,7 @@ module Rswag
|
|||||||
|
|
||||||
def verb_from(route)
|
def verb_from(route)
|
||||||
verb = route.verb
|
verb = route.verb
|
||||||
if verb.kind_of? String
|
if verb.is_a? String
|
||||||
verb.downcase
|
verb.downcase
|
||||||
else
|
else
|
||||||
verb.source.gsub(/[$^]/, '').downcase
|
verb.source.gsub(/[$^]/, '').downcase
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rspec/core'
|
require 'rspec/core'
|
||||||
require 'rswag/specs/example_group_helpers'
|
require 'rswag/specs/example_group_helpers'
|
||||||
require 'rswag/specs/example_helpers'
|
require 'rswag/specs/example_helpers'
|
||||||
@ -6,7 +8,6 @@ require 'rswag/specs/railtie' if defined?(Rails::Railtie)
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
# Extend RSpec with a swagger-based DSL
|
# Extend RSpec with a swagger-based DSL
|
||||||
::RSpec.configure do |c|
|
::RSpec.configure do |c|
|
||||||
c.add_setting :swagger_root
|
c.add_setting :swagger_root
|
||||||
|
|||||||
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
class Configuration
|
class Configuration
|
||||||
|
|
||||||
def initialize(rspec_config)
|
def initialize(rspec_config)
|
||||||
@rspec_config = rspec_config
|
@rspec_config = rspec_config
|
||||||
end
|
end
|
||||||
@ -14,6 +12,7 @@ module Rswag
|
|||||||
if @rspec_config.swagger_root.nil?
|
if @rspec_config.swagger_root.nil?
|
||||||
raise ConfigurationError, 'No swagger_root provided. See swagger_helper.rb'
|
raise ConfigurationError, 'No swagger_root provided. See swagger_helper.rb'
|
||||||
end
|
end
|
||||||
|
|
||||||
@rspec_config.swagger_root
|
@rspec_config.swagger_root
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -23,6 +22,7 @@ module Rswag
|
|||||||
if @rspec_config.swagger_docs.nil? || @rspec_config.swagger_docs.empty?
|
if @rspec_config.swagger_docs.nil? || @rspec_config.swagger_docs.empty?
|
||||||
raise ConfigurationError, 'No swagger_docs defined. See swagger_helper.rb'
|
raise ConfigurationError, 'No swagger_docs defined. See swagger_helper.rb'
|
||||||
end
|
end
|
||||||
|
|
||||||
@rspec_config.swagger_docs
|
@rspec_config.swagger_docs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -37,6 +37,7 @@ module Rswag
|
|||||||
@swagger_format ||= begin
|
@swagger_format ||= begin
|
||||||
@rspec_config.swagger_format = :json if @rspec_config.swagger_format.nil? || @rspec_config.swagger_format.empty?
|
@rspec_config.swagger_format = :json if @rspec_config.swagger_format.nil? || @rspec_config.swagger_format.empty?
|
||||||
raise ConfigurationError, "Unknown swagger_format '#{@rspec_config.swagger_format}'" unless [:json, :yaml].include?(@rspec_config.swagger_format)
|
raise ConfigurationError, "Unknown swagger_format '#{@rspec_config.swagger_format}'" unless [:json, :yaml].include?(@rspec_config.swagger_format)
|
||||||
|
|
||||||
@rspec_config.swagger_format
|
@rspec_config.swagger_format
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -44,6 +45,7 @@ module Rswag
|
|||||||
def get_swagger_doc(name)
|
def get_swagger_doc(name)
|
||||||
return swagger_docs.values.first if name.nil?
|
return swagger_docs.values.first if name.nil?
|
||||||
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
|
raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name]
|
||||||
|
|
||||||
swagger_docs[name]
|
swagger_docs[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
module ExampleGroupHelpers
|
module ExampleGroupHelpers
|
||||||
|
|
||||||
def path(template, metadata = {}, &block)
|
def path(template, metadata = {}, &block)
|
||||||
metadata[:path_item] = { template: template }
|
metadata[:path_item] = { template: template }
|
||||||
describe(template, metadata, &block)
|
describe(template, metadata, &block)
|
||||||
@ -25,6 +26,7 @@ module Rswag
|
|||||||
# functionality while also setting the appropriate metadata if applicable
|
# functionality while also setting the appropriate metadata if applicable
|
||||||
def description(value = nil)
|
def description(value = nil)
|
||||||
return super() if value.nil?
|
return super() if value.nil?
|
||||||
|
|
||||||
metadata[:operation][:description] = value
|
metadata[:operation][:description] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -125,13 +127,12 @@ module Rswag
|
|||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
|
||||||
def parameter(attributes)
|
def parameter(attributes)
|
||||||
if attributes[:in] && attributes[:in].to_sym == :path
|
if attributes[:in] && attributes[:in].to_sym == :path
|
||||||
attributes[:required] = true
|
attributes[:required] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
if metadata.has_key?(:operation)
|
if metadata.key?(:operation)
|
||||||
metadata[:operation][:parameters] ||= []
|
metadata[:operation][:parameters] ||= []
|
||||||
metadata[:operation][:parameters] << attributes
|
metadata[:operation][:parameters] << attributes
|
||||||
else
|
else
|
||||||
@ -165,6 +166,7 @@ module Rswag
|
|||||||
# rspec-core ExampleGroup
|
# rspec-core ExampleGroup
|
||||||
def examples(example = nil)
|
def examples(example = nil)
|
||||||
return super() if example.nil?
|
return super() if example.nil?
|
||||||
|
|
||||||
metadata[:response][:examples] = example
|
metadata[:response][:examples] = example
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,6 @@ require 'json-schema'
|
|||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
class ExtendedSchema < JSON::Schema::Draft4
|
class ExtendedSchema < JSON::Schema::Draft4
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
super
|
super
|
||||||
@attributes['type'] = ExtendedTypeAttribute
|
@attributes['type'] = ExtendedTypeAttribute
|
||||||
@ -15,11 +14,11 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
class ExtendedTypeAttribute < JSON::Schema::TypeV4Attribute
|
class ExtendedTypeAttribute < JSON::Schema::TypeV4Attribute
|
||||||
|
|
||||||
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
||||||
## OA3
|
## OA3
|
||||||
# return if data.nil? && (current_schema.schema['nullable'] == true || current_schema.schema['x-nullable'] == true)
|
# return if data.nil? && (current_schema.schema['nullable'] == true || current_schema.schema['x-nullable'] == true)
|
||||||
return if data.nil? && current_schema.schema['x-nullable'] == true
|
return if data.nil? && current_schema.schema['x-nullable'] == true
|
||||||
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,9 +3,8 @@
|
|||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
class Railtie < ::Rails::Railtie
|
class Railtie < ::Rails::Railtie
|
||||||
|
|
||||||
rake_tasks do
|
rake_tasks do
|
||||||
load File.expand_path('../../../tasks/rswag-specs_tasks.rake', __FILE__)
|
load File.expand_path('../../tasks/rswag-specs_tasks.rake', __dir__)
|
||||||
end
|
end
|
||||||
|
|
||||||
generators do
|
generators do
|
||||||
|
|||||||
@ -8,7 +8,6 @@ require 'byebug'
|
|||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
class RequestFactory
|
class RequestFactory
|
||||||
|
|
||||||
def initialize(config = ::Rswag::Specs.config)
|
def initialize(config = ::Rswag::Specs.config)
|
||||||
@config = config
|
@config = config
|
||||||
end
|
end
|
||||||
@ -54,7 +53,7 @@ module Rswag
|
|||||||
if doc_version(swagger_doc).start_with?('2')
|
if doc_version(swagger_doc).start_with?('2')
|
||||||
(swagger_doc[:securityDefinitions] || {}).slice(*scheme_names).values
|
(swagger_doc[:securityDefinitions] || {}).slice(*scheme_names).values
|
||||||
else # Openapi3
|
else # Openapi3
|
||||||
if swagger_doc.has_key?(:securityDefinitions)
|
if swagger_doc.key?(:securityDefinitions)
|
||||||
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: securityDefinitions is replaced in OpenAPI3! Rename to components/securitySchemes (in swagger_helper.rb)')
|
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: securityDefinitions is replaced in OpenAPI3! Rename to components/securitySchemes (in swagger_helper.rb)')
|
||||||
swagger_doc[:components] ||= { securitySchemes: swagger_doc[:securityDefinitions] }
|
swagger_doc[:components] ||= { securitySchemes: swagger_doc[:securityDefinitions] }
|
||||||
swagger_doc.delete(:securityDefinitions)
|
swagger_doc.delete(:securityDefinitions)
|
||||||
@ -67,8 +66,8 @@ module Rswag
|
|||||||
def resolve_parameter(ref, swagger_doc)
|
def resolve_parameter(ref, swagger_doc)
|
||||||
key = key_version(ref, swagger_doc)
|
key = key_version(ref, swagger_doc)
|
||||||
definitions = definition_version(swagger_doc)
|
definitions = definition_version(swagger_doc)
|
||||||
|
|
||||||
raise "Referenced parameter '#{ref}' must be defined" unless definitions && definitions[key]
|
raise "Referenced parameter '#{ref}' must be defined" unless definitions && definitions[key]
|
||||||
|
|
||||||
definitions[key]
|
definitions[key]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -89,7 +88,7 @@ module Rswag
|
|||||||
if doc_version(swagger_doc).start_with?('2')
|
if doc_version(swagger_doc).start_with?('2')
|
||||||
swagger_doc[:parameters]
|
swagger_doc[:parameters]
|
||||||
else # Openapi3
|
else # Openapi3
|
||||||
if swagger_doc.has_key?(:parameters)
|
if swagger_doc.key?(:parameters)
|
||||||
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: parameters is replaced in OpenAPI3! Rename to components/parameters (in swagger_helper.rb)')
|
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: parameters is replaced in OpenAPI3! Rename to components/parameters (in swagger_helper.rb)')
|
||||||
swagger_doc[:parameters]
|
swagger_doc[:parameters]
|
||||||
else
|
else
|
||||||
@ -106,21 +105,21 @@ module Rswag
|
|||||||
def add_path(request, metadata, swagger_doc, parameters, example)
|
def add_path(request, metadata, swagger_doc, parameters, example)
|
||||||
template = (swagger_doc[:basePath] || '') + metadata[:path_item][:template]
|
template = (swagger_doc[:basePath] || '') + metadata[:path_item][:template]
|
||||||
|
|
||||||
request[:path] = template.tap do |template|
|
request[:path] = template.tap do |path_template|
|
||||||
parameters.select { |p| p[:in] == :path }.each do |p|
|
parameters.select { |p| p[:in] == :path }.each do |p|
|
||||||
template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s)
|
path_template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
parameters.select { |p| p[:in] == :query }.each_with_index do |p, i|
|
parameters.select { |p| p[:in] == :query }.each_with_index do |p, i|
|
||||||
template.concat(i == 0 ? '?' : '&')
|
path_template.concat(i.zero? ? '?' : '&')
|
||||||
template.concat(build_query_string_part(p, example.send(p[:name])))
|
path_template.concat(build_query_string_part(p, example.send(p[:name])))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_query_string_part(param, value)
|
def build_query_string_part(param, value)
|
||||||
name = param[:name]
|
name = param[:name]
|
||||||
return "#{name}=#{value.to_s}" unless param[:type].to_sym == :array
|
return "#{name}=#{value}" unless param[:type].to_sym == :array
|
||||||
|
|
||||||
case param[:collectionFormat]
|
case param[:collectionFormat]
|
||||||
when :ssv
|
when :ssv
|
||||||
|
|||||||
@ -69,12 +69,12 @@ module Rswag
|
|||||||
if version.start_with?('2')
|
if version.start_with?('2')
|
||||||
swagger_doc.slice(:definitions)
|
swagger_doc.slice(:definitions)
|
||||||
else # Openapi3
|
else # Openapi3
|
||||||
if swagger_doc.has_key?(:definitions)
|
if swagger_doc.key?(:definitions)
|
||||||
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: definitions is replaced in OpenAPI3! Rename to components/schemas (in swagger_helper.rb)')
|
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: definitions is replaced in OpenAPI3! Rename to components/schemas (in swagger_helper.rb)')
|
||||||
return swagger_doc.slice(:definitions)
|
swagger_doc.slice(:definitions)
|
||||||
else
|
else
|
||||||
components = swagger_doc[:components] || {}
|
components = swagger_doc[:components] || {}
|
||||||
return { components: { schemas: components[:schemas] } }
|
{ components: { schemas: components[:schemas] } }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -6,7 +6,6 @@ require 'swagger_helper'
|
|||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
class SwaggerFormatter
|
class SwaggerFormatter
|
||||||
|
|
||||||
# NOTE: rspec 2.x support
|
# NOTE: rspec 2.x support
|
||||||
if RSPEC_VERSION > 2
|
if RSPEC_VERSION > 2
|
||||||
::RSpec::Core::Formatters.register self, :example_group_finished, :stop
|
::RSpec::Core::Formatters.register self, :example_group_finished, :stop
|
||||||
@ -30,11 +29,11 @@ module Rswag
|
|||||||
# !metadata[:document] won't work, since nil means we should generate
|
# !metadata[:document] won't work, since nil means we should generate
|
||||||
# docs.
|
# docs.
|
||||||
return if metadata[:document] == false
|
return if metadata[:document] == false
|
||||||
return unless metadata.has_key?(:response)
|
return unless metadata.key?(:response)
|
||||||
|
|
||||||
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
|
swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])
|
||||||
|
|
||||||
if !doc_version(swagger_doc).start_with?('2')
|
unless doc_version(swagger_doc).start_with?('2')
|
||||||
upgrade_request_type!(metadata)
|
upgrade_request_type!(metadata)
|
||||||
upgrade_servers!(swagger_doc)
|
upgrade_servers!(swagger_doc)
|
||||||
upgrade_oauth!(swagger_doc)
|
upgrade_oauth!(swagger_doc)
|
||||||
@ -68,7 +67,7 @@ module Rswag
|
|||||||
|
|
||||||
file_path = File.join(@config.swagger_root, url_path)
|
file_path = File.join(@config.swagger_root, url_path)
|
||||||
dirname = File.dirname(file_path)
|
dirname = File.dirname(file_path)
|
||||||
FileUtils.mkdir_p dirname unless File.exists?(dirname)
|
FileUtils.mkdir_p dirname unless File.exist?(dirname)
|
||||||
|
|
||||||
File.open(file_path, 'w') do |file|
|
File.open(file_path, 'w') do |file|
|
||||||
file.write(pretty_generate(doc))
|
file.write(pretty_generate(doc))
|
||||||
@ -96,7 +95,7 @@ module Rswag
|
|||||||
|
|
||||||
def metadata_to_swagger(metadata)
|
def metadata_to_swagger(metadata)
|
||||||
response_code = metadata[:response][:code]
|
response_code = metadata[:response][:code]
|
||||||
response = metadata[:response].reject { |k,v| k == :code }
|
response = metadata[:response].reject { |k, _v| k == :code }
|
||||||
## OA3
|
## OA3
|
||||||
# content_type = metadata[:response][:content].present? ? metadata[:response][:content].keys.first : 'application/json'
|
# content_type = metadata[:response][:content].present? ? metadata[:response][:content].keys.first : 'application/json'
|
||||||
# # need to merge in to response
|
# # need to merge in to response
|
||||||
@ -111,12 +110,12 @@ module Rswag
|
|||||||
|
|
||||||
verb = metadata[:operation][:verb]
|
verb = metadata[:operation][:verb]
|
||||||
operation = metadata[:operation]
|
operation = metadata[:operation]
|
||||||
.reject { |k,v| k == :verb }
|
.reject { |k, _v| k == :verb }
|
||||||
.merge(responses: { response_code => response })
|
.merge(responses: { response_code => response })
|
||||||
|
|
||||||
path_template = metadata[:path_item][:template]
|
path_template = metadata[:path_item][:template]
|
||||||
path_item = metadata[:path_item]
|
path_item = metadata[:path_item]
|
||||||
.reject { |k,v| k == :template }
|
.reject { |k, _v| k == :template }
|
||||||
.merge(verb => operation)
|
.merge(verb => operation)
|
||||||
|
|
||||||
{ paths: { path_template => path_item } }
|
{ paths: { path_template => path_item } }
|
||||||
@ -141,7 +140,8 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
def upgrade_servers!(swagger_doc)
|
def upgrade_servers!(swagger_doc)
|
||||||
if swagger_doc[:servers].nil? && swagger_doc.has_key?(:schemes)
|
return unless swagger_doc[:servers].nil? && swagger_doc.key?(:schemes)
|
||||||
|
|
||||||
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: schemes, host, and basePath are replaced in OpenAPI3! Rename to array of servers[{url}] (in swagger_helper.rb)')
|
ActiveSupport::Deprecation.warn('Rswag::Specs: WARNING: schemes, host, and basePath are replaced in OpenAPI3! Rename to array of servers[{url}] (in swagger_helper.rb)')
|
||||||
|
|
||||||
swagger_doc[:servers] = { urls: [] }
|
swagger_doc[:servers] = { urls: [] }
|
||||||
@ -153,14 +153,15 @@ module Rswag
|
|||||||
swagger_doc.delete(:host)
|
swagger_doc.delete(:host)
|
||||||
swagger_doc.delete(:basePath)
|
swagger_doc.delete(:basePath)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def upgrade_oauth!(swagger_doc)
|
def upgrade_oauth!(swagger_doc)
|
||||||
# find flow in securitySchemes (securityDefinitions will have been re-written)
|
# find flow in securitySchemes (securityDefinitions will have been re-written)
|
||||||
schemes = swagger_doc.dig(:components, :securitySchemes)
|
schemes = swagger_doc.dig(:components, :securitySchemes)
|
||||||
if schemes && schemes.any?{ |_k, v| v.has_key?(:flow) }
|
return unless schemes&.any? { |_k, v| v.key?(:flow) }
|
||||||
|
|
||||||
schemes.each do |name, v|
|
schemes.each do |name, v|
|
||||||
if v.has_key?(:flow)
|
next unless v.key?(:flow)
|
||||||
|
|
||||||
ActiveSupport::Deprecation.warn("Rswag::Specs: WARNING: securityDefinitions flow is replaced in OpenAPI3! Rename to components/securitySchemes/#{name}/flows[] (in swagger_helper.rb)")
|
ActiveSupport::Deprecation.warn("Rswag::Specs: WARNING: securityDefinitions flow is replaced in OpenAPI3! Rename to components/securitySchemes/#{name}/flows[] (in swagger_helper.rb)")
|
||||||
flow = swagger_doc[:components][:securitySchemes][name].delete(:flow)
|
flow = swagger_doc[:components][:securitySchemes][name].delete(:flow)
|
||||||
swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow])
|
swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow])
|
||||||
@ -169,5 +170,3 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'rspec/core/rake_task'
|
require 'rspec/core/rake_task'
|
||||||
|
|
||||||
namespace :rswag do
|
namespace :rswag do
|
||||||
namespace :specs do
|
namespace :specs do
|
||||||
|
|
||||||
desc 'Generate Swagger JSON files from integration specs'
|
desc 'Generate Swagger JSON files from integration specs'
|
||||||
RSpec::Core::RakeTask.new('swaggerize') do |t|
|
RSpec::Core::RakeTask.new('swaggerize') do |t|
|
||||||
t.pattern = ENV.fetch(
|
t.pattern = ENV.fetch(
|
||||||
@ -20,4 +21,4 @@ namespace :rswag do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
task :rswag => ['rswag:specs:swaggerize']
|
task rswag: ['rswag:specs:swaggerize']
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
||||||
|
|
||||||
# Describe your gem and declare its dependencies:
|
# Describe your gem and declare its dependencies:
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'generator_spec'
|
require 'generator_spec'
|
||||||
require 'generators/rspec/swagger_generator'
|
require 'generators/rspec/swagger_generator'
|
||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
@ -9,12 +11,11 @@ module Rspec
|
|||||||
|
|
||||||
before(:all) do
|
before(:all) do
|
||||||
prepare_destination
|
prepare_destination
|
||||||
fixtures_dir = File.expand_path('../fixtures', __FILE__)
|
fixtures_dir = File.expand_path('fixtures', __dir__)
|
||||||
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
|
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
|
||||||
end
|
end
|
||||||
|
|
||||||
after(:all) do
|
after(:all) do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'installs the swagger_helper for rspec' do
|
it 'installs the swagger_helper for rspec' do
|
||||||
@ -31,11 +32,11 @@ module Rspec
|
|||||||
|
|
||||||
def fake_routes
|
def fake_routes
|
||||||
{
|
{
|
||||||
"/posts/{post_id}/comments/{id}" => {
|
'/posts/{post_id}/comments/{id}' => {
|
||||||
:params => ["post_id", "id"],
|
params: ['post_id', 'id'],
|
||||||
:actions => {
|
actions: {
|
||||||
"get" => { :summary=>"show comment" },
|
'get' => { summary: 'show comment' },
|
||||||
"patch" => { :summary=>"update_comments comment" }
|
'patch' => { summary: 'update_comments comment' }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,17 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'generator_spec'
|
require 'generator_spec'
|
||||||
require 'generators/rswag/specs/install/install_generator'
|
require 'generators/rswag/specs/install/install_generator'
|
||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe InstallGenerator do
|
RSpec.describe InstallGenerator do
|
||||||
include GeneratorSpec::TestCase
|
include GeneratorSpec::TestCase
|
||||||
destination File.expand_path('../tmp', __FILE__)
|
destination File.expand_path('tmp', __dir__)
|
||||||
|
|
||||||
before(:all) do
|
before(:all) do
|
||||||
prepare_destination
|
prepare_destination
|
||||||
fixtures_dir = File.expand_path('../fixtures', __FILE__)
|
fixtures_dir = File.expand_path('fixtures', __dir__)
|
||||||
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
|
FileUtils.cp_r("#{fixtures_dir}/spec", destination_root)
|
||||||
|
|
||||||
run_generator
|
run_generator
|
||||||
|
|||||||
@ -4,7 +4,6 @@ require 'rswag/specs/configuration'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe Configuration do
|
RSpec.describe Configuration do
|
||||||
subject { described_class.new(rspec_config) }
|
subject { described_class.new(rspec_config) }
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ require 'rswag/specs/example_group_helpers'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe ExampleGroupHelpers do
|
RSpec.describe ExampleGroupHelpers do
|
||||||
subject { double('example_group') }
|
subject { double('example_group') }
|
||||||
|
|
||||||
@ -123,7 +122,6 @@ module Rswag
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
describe '#parameter(attributes)' do
|
describe '#parameter(attributes)' do
|
||||||
|
|
||||||
context "when called at the 'path' level" do
|
context "when called at the 'path' level" do
|
||||||
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
|
before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) }
|
||||||
let(:api_metadata) { { path_item: {} } } # i.e. operation not defined yet
|
let(:api_metadata) { { path_item: {} } } # i.e. operation not defined yet
|
||||||
|
|||||||
@ -4,7 +4,6 @@ require 'rswag/specs/example_helpers'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe ExampleHelpers do
|
RSpec.describe ExampleHelpers do
|
||||||
subject { double('example') }
|
subject { double('example') }
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ require 'rswag/specs/request_factory'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe RequestFactory do
|
RSpec.describe RequestFactory do
|
||||||
subject { RequestFactory.new(config) }
|
subject { RequestFactory.new(config) }
|
||||||
|
|
||||||
@ -55,7 +54,7 @@ module Rswag
|
|||||||
allow(example).to receive(:q2).and_return('bar')
|
allow(example).to receive(:q2).and_return('bar')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "builds the query string from example values" do
|
it 'builds the query string from example values' do
|
||||||
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
|
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -70,35 +69,35 @@ module Rswag
|
|||||||
|
|
||||||
context 'collectionFormat = csv' do
|
context 'collectionFormat = csv' do
|
||||||
let(:collection_format) { :csv }
|
let(:collection_format) { :csv }
|
||||||
it "formats as comma separated values" do
|
it 'formats as comma separated values' do
|
||||||
expect(request[:path]).to eq('/blogs?things=foo,bar')
|
expect(request[:path]).to eq('/blogs?things=foo,bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'collectionFormat = ssv' do
|
context 'collectionFormat = ssv' do
|
||||||
let(:collection_format) { :ssv }
|
let(:collection_format) { :ssv }
|
||||||
it "formats as space separated values" do
|
it 'formats as space separated values' do
|
||||||
expect(request[:path]).to eq('/blogs?things=foo bar')
|
expect(request[:path]).to eq('/blogs?things=foo bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'collectionFormat = tsv' do
|
context 'collectionFormat = tsv' do
|
||||||
let(:collection_format) { :tsv }
|
let(:collection_format) { :tsv }
|
||||||
it "formats as tab separated values" do
|
it 'formats as tab separated values' do
|
||||||
expect(request[:path]).to eq('/blogs?things=foo\tbar')
|
expect(request[:path]).to eq('/blogs?things=foo\tbar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'collectionFormat = pipes' do
|
context 'collectionFormat = pipes' do
|
||||||
let(:collection_format) { :pipes }
|
let(:collection_format) { :pipes }
|
||||||
it "formats as pipe separated values" do
|
it 'formats as pipe separated values' do
|
||||||
expect(request[:path]).to eq('/blogs?things=foo|bar')
|
expect(request[:path]).to eq('/blogs?things=foo|bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'collectionFormat = multi' do
|
context 'collectionFormat = multi' do
|
||||||
let(:collection_format) { :multi }
|
let(:collection_format) { :multi }
|
||||||
it "formats as multiple parameter instances" do
|
it 'formats as multiple parameter instances' do
|
||||||
expect(request[:path]).to eq('/blogs?things=foo&things=bar')
|
expect(request[:path]).to eq('/blogs?things=foo&things=bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -129,7 +128,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "consumes content" do
|
context 'consumes content' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:consumes] = ['application/json', 'application/xml']
|
metadata[:operation][:consumes] = ['application/json', 'application/xml']
|
||||||
end
|
end
|
||||||
@ -157,7 +156,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "serializes first 'body' parameter to JSON string" do
|
it "serializes first 'body' parameter to JSON string" do
|
||||||
expect(request[:payload]).to eq("{\"text\":\"Some comment\"}")
|
expect(request[:payload]).to eq('{"text":"Some comment"}')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -194,7 +193,7 @@ module Rswag
|
|||||||
|
|
||||||
context "explicit 'Accept' value provided" do
|
context "explicit 'Accept' value provided" do
|
||||||
before do
|
before do
|
||||||
allow(example).to receive(:'Accept').and_return('application/xml')
|
allow(example).to receive(:Accept).and_return('application/xml')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets 'HTTP_ACCEPT' header to example value" do
|
it "sets 'HTTP_ACCEPT' header to example value" do
|
||||||
@ -324,13 +323,13 @@ module Rswag
|
|||||||
allow(example).to receive(:api_key).and_return('foobar')
|
allow(example).to receive(:api_key).and_return('foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets both params to example values" do
|
it 'sets both params to example values' do
|
||||||
expect(request[:headers]).to eq('HTTP_AUTHORIZATION' => 'Basic foobar')
|
expect(request[:headers]).to eq('HTTP_AUTHORIZATION' => 'Basic foobar')
|
||||||
expect(request[:path]).to eq('/blogs?api_key=foobar')
|
expect(request[:path]).to eq('/blogs?api_key=foobar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "path-level parameters" do
|
context 'path-level parameters' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:parameters] = [{ name: 'q1', in: :query, type: :string }]
|
metadata[:operation][:parameters] = [{ name: 'q1', in: :query, type: :string }]
|
||||||
metadata[:path_item][:parameters] = [{ name: 'q2', in: :query, type: :string }]
|
metadata[:path_item][:parameters] = [{ name: 'q2', in: :query, type: :string }]
|
||||||
@ -338,7 +337,7 @@ module Rswag
|
|||||||
allow(example).to receive(:q2).and_return('bar')
|
allow(example).to receive(:q2).and_return('bar')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "populates operation and path level parameters " do
|
it 'populates operation and path level parameters' do
|
||||||
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
|
expect(request[:path]).to eq('/blogs?q1=foo&q2=bar')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -396,7 +395,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "global consumes" do
|
context 'global consumes' do
|
||||||
before { swagger_doc[:consumes] = ['application/xml'] }
|
before { swagger_doc[:consumes] = ['application/xml'] }
|
||||||
|
|
||||||
it "defaults 'CONTENT_TYPE' to global value(s)" do
|
it "defaults 'CONTENT_TYPE' to global value(s)" do
|
||||||
@ -404,7 +403,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "global security requirements" do
|
context 'global security requirements' do
|
||||||
before do
|
before do
|
||||||
swagger_doc[:securityDefinitions] = { apiKey: { type: :apiKey, name: 'api_key', in: :query } }
|
swagger_doc[:securityDefinitions] = { apiKey: { type: :apiKey, name: 'api_key', in: :query } }
|
||||||
## OA3
|
## OA3
|
||||||
|
|||||||
@ -4,7 +4,6 @@ require 'rswag/specs/response_validator'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe ResponseValidator do
|
RSpec.describe ResponseValidator do
|
||||||
subject { ResponseValidator.new(config) }
|
subject { ResponseValidator.new(config) }
|
||||||
|
|
||||||
@ -49,21 +48,21 @@ module Rswag
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response matches metadata" do
|
context 'response matches metadata' do
|
||||||
it { expect { call }.to_not raise_error }
|
it { expect { call }.to_not raise_error }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response code differs from metadata" do
|
context 'response code differs from metadata' do
|
||||||
before { response.code = '400' }
|
before { response.code = '400' }
|
||||||
it { expect { call }.to raise_error(/Expected response code/) }
|
it { expect { call }.to raise_error(/Expected response code/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response headers differ from metadata" do
|
context 'response headers differ from metadata' do
|
||||||
before { response.headers = {} }
|
before { response.headers = {} }
|
||||||
it { expect { call }.to raise_error(/Expected response header/) }
|
it { expect { call }.to raise_error(/Expected response header/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context "response body differs from metadata" do
|
context 'response body differs from metadata' do
|
||||||
before { response.body = '{"foo":"Some comment"}' }
|
before { response.body = '{"foo":"Some comment"}' }
|
||||||
it { expect { call }.to raise_error(/Expected response body/) }
|
it { expect { call }.to raise_error(/Expected response body/) }
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,7 +5,6 @@ require 'ostruct'
|
|||||||
|
|
||||||
module Rswag
|
module Rswag
|
||||||
module Specs
|
module Specs
|
||||||
|
|
||||||
RSpec.describe SwaggerFormatter do
|
RSpec.describe SwaggerFormatter do
|
||||||
subject { described_class.new(output, config) }
|
subject { described_class.new(output, config) }
|
||||||
|
|
||||||
@ -15,7 +14,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
let(:config) { double('config') }
|
let(:config) { double('config') }
|
||||||
let(:output) { double('output').as_null_object }
|
let(:output) { double('output').as_null_object }
|
||||||
let(:swagger_root) { File.expand_path('../tmp/swagger', __FILE__) }
|
let(:swagger_root) { File.expand_path('tmp/swagger', __dir__) }
|
||||||
|
|
||||||
describe '#example_group_finished(notification)' do
|
describe '#example_group_finished(notification)' do
|
||||||
before do
|
before do
|
||||||
@ -69,7 +68,8 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'with metadata upgrades for 3.0' do
|
context 'with metadata upgrades for 3.0' do
|
||||||
let(:swagger_doc) { {
|
let(:swagger_doc) do
|
||||||
|
{
|
||||||
openapi: '3.0.1',
|
openapi: '3.0.1',
|
||||||
basePath: '/foo',
|
basePath: '/foo',
|
||||||
schemes: ['http', 'https'],
|
schemes: ['http', 'https'],
|
||||||
@ -81,7 +81,8 @@ module Rswag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} }
|
}
|
||||||
|
end
|
||||||
let(:document) { nil }
|
let(:document) { nil }
|
||||||
|
|
||||||
it 'converts query and path params, type: to schema: { type: }' do
|
it 'converts query and path params, type: to schema: { type: }' do
|
||||||
@ -128,7 +129,7 @@ module Rswag
|
|||||||
|
|
||||||
describe '#stop' do
|
describe '#stop' do
|
||||||
before do
|
before do
|
||||||
FileUtils.rm_r(swagger_root) if File.exists?(swagger_root)
|
FileUtils.rm_r(swagger_root) if File.exist?(swagger_root)
|
||||||
allow(config).to receive(:swagger_docs).and_return(
|
allow(config).to receive(:swagger_docs).and_return(
|
||||||
'v1/swagger.json' => { info: { version: 'v1' } },
|
'v1/swagger.json' => { info: { version: 'v1' } },
|
||||||
'v2/swagger.json' => { info: { version: 'v2' } }
|
'v2/swagger.json' => { info: { version: 'v2' } }
|
||||||
@ -160,7 +161,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
FileUtils.rm_r(swagger_root) if File.exists?(swagger_root)
|
FileUtils.rm_r(swagger_root) if File.exist?(swagger_root)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Rails
|
module Rails
|
||||||
module VERSION
|
module VERSION
|
||||||
MAJOR = 3
|
MAJOR = 3
|
||||||
|
|||||||
@ -1,2 +1,4 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
# NOTE: For the specs in this gem, all configuration is completely mocked out
|
# NOTE: For the specs in this gem, all configuration is completely mocked out
|
||||||
# The file just needs to be present because it gets required by the swagger_formatter
|
# The file just needs to be present because it gets required by the swagger_formatter
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user