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|
|
||||||
@ -25,7 +27,7 @@ RSpec.configure do |config|
|
|||||||
url: 'https://{defaultHost}',
|
url: 'https://{defaultHost}',
|
||||||
variables: {
|
variables: {
|
||||||
defaultHost: {
|
defaultHost: {
|
||||||
default: 'www.example.com'
|
default: 'www.example.com'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,20 +1,21 @@
|
|||||||
|
# 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)
|
||||||
end
|
end
|
||||||
|
|
||||||
[ :get, :post, :patch, :put, :delete, :head, :options, :trace ].each do |verb|
|
[:get, :post, :patch, :put, :delete, :head, :options, :trace].each do |verb|
|
||||||
define_method(verb) do |summary, &block|
|
define_method(verb) do |summary, &block|
|
||||||
api_metadata = { operation: { verb: verb, summary: summary } }
|
api_metadata = { operation: { verb: verb, summary: summary } }
|
||||||
describe(verb, api_metadata, &block)
|
describe(verb, api_metadata, &block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
[ :operationId, :deprecated, :security ].each do |attr_name|
|
[:operationId, :deprecated, :security].each do |attr_name|
|
||||||
define_method(attr_name) do |value|
|
define_method(attr_name) do |value|
|
||||||
metadata[:operation][attr_name] = value
|
metadata[:operation][attr_name] = value
|
||||||
end
|
end
|
||||||
@ -23,13 +24,14 @@ module Rswag
|
|||||||
# NOTE: 'description' requires special treatment because ExampleGroup already
|
# NOTE: 'description' requires special treatment because ExampleGroup already
|
||||||
# defines a method with that name. Provide an override that supports the existing
|
# defines a method with that name. Provide an override that supports the existing
|
||||||
# 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
|
||||||
|
|
||||||
# These are array properties - note the splat operator
|
# These are array properties - note the splat operator
|
||||||
[ :tags, :consumes, :produces, :schemes ].each do |attr_name|
|
[:tags, :consumes, :produces, :schemes].each do |attr_name|
|
||||||
define_method(attr_name) do |*value|
|
define_method(attr_name) do |*value|
|
||||||
metadata[:operation][attr_name] = value
|
metadata[:operation][attr_name] = 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
|
||||||
@ -140,7 +141,7 @@ module Rswag
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def response(code, description, metadata={}, &block)
|
def response(code, description, metadata = {}, &block)
|
||||||
metadata[:response] = { code: code, description: description }
|
metadata[:response] = { code: code, description: description }
|
||||||
context(description, metadata, &block)
|
context(description, metadata, &block)
|
||||||
end
|
end
|
||||||
@ -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
|
||||||
@ -139,43 +138,43 @@ module Rswag
|
|||||||
def add_headers(request, metadata, swagger_doc, parameters, example)
|
def add_headers(request, metadata, swagger_doc, parameters, example)
|
||||||
tuples = parameters
|
tuples = parameters
|
||||||
.select { |p| p[:in] == :header }
|
.select { |p| p[:in] == :header }
|
||||||
.map { |p| [ p[:name], example.send(p[:name]).to_s ] }
|
.map { |p| [p[:name], example.send(p[:name]).to_s] }
|
||||||
|
|
||||||
# Accept header
|
# Accept header
|
||||||
produces = metadata[:operation][:produces] || swagger_doc[:produces]
|
produces = metadata[:operation][:produces] || swagger_doc[:produces]
|
||||||
if produces
|
if produces
|
||||||
accept = example.respond_to?(:Accept) ? example.send(:Accept) : produces.first
|
accept = example.respond_to?(:Accept) ? example.send(:Accept) : produces.first
|
||||||
tuples << [ 'Accept', accept ]
|
tuples << ['Accept', accept]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Content-Type header
|
# Content-Type header
|
||||||
consumes = metadata[:operation][:consumes] || swagger_doc[:consumes]
|
consumes = metadata[:operation][:consumes] || swagger_doc[:consumes]
|
||||||
if consumes
|
if consumes
|
||||||
content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first
|
content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first
|
||||||
tuples << [ 'Content-Type', content_type ]
|
tuples << ['Content-Type', content_type]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Rails test infrastructure requires rackified headers
|
# Rails test infrastructure requires rackified headers
|
||||||
rackified_tuples = tuples.map do |pair|
|
rackified_tuples = tuples.map do |pair|
|
||||||
[
|
[
|
||||||
case pair[0]
|
case pair[0]
|
||||||
when 'Accept' then 'HTTP_ACCEPT'
|
when 'Accept' then 'HTTP_ACCEPT'
|
||||||
when 'Content-Type' then 'CONTENT_TYPE'
|
when 'Content-Type' then 'CONTENT_TYPE'
|
||||||
when 'Authorization' then 'HTTP_AUTHORIZATION'
|
when 'Authorization' then 'HTTP_AUTHORIZATION'
|
||||||
else pair[0]
|
else pair[0]
|
||||||
end,
|
end,
|
||||||
pair[1]
|
pair[1]
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
request[:headers] = Hash[ rackified_tuples ]
|
request[:headers] = Hash[rackified_tuples]
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_payload(request, parameters, example)
|
def add_payload(request, parameters, example)
|
||||||
content_type = request[:headers]['CONTENT_TYPE']
|
content_type = request[:headers]['CONTENT_TYPE']
|
||||||
return if content_type.nil?
|
return if content_type.nil?
|
||||||
|
|
||||||
if [ 'application/x-www-form-urlencoded', 'multipart/form-data' ].include?(content_type)
|
if ['application/x-www-form-urlencoded', 'multipart/form-data'].include?(content_type)
|
||||||
request[:payload] = build_form_payload(parameters, example)
|
request[:payload] = build_form_payload(parameters, example)
|
||||||
else
|
else
|
||||||
request[:payload] = build_json_payload(parameters, example)
|
request[:payload] = build_json_payload(parameters, example)
|
||||||
@ -189,7 +188,7 @@ module Rswag
|
|||||||
# PROS: simple to implement, CONS: serialization/deserialization is bypassed in test
|
# PROS: simple to implement, CONS: serialization/deserialization is bypassed in test
|
||||||
tuples = parameters
|
tuples = parameters
|
||||||
.select { |p| p[:in] == :formData }
|
.select { |p| p[:in] == :formData }
|
||||||
.map { |p| [ p[:name], example.send(p[:name]) ] }
|
.map { |p| [p[:name], example.send(p[:name])] }
|
||||||
Hash[tuples]
|
Hash[tuples]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
||||||
@ -43,7 +42,7 @@ module Rswag
|
|||||||
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
swagger_doc.deep_merge!(metadata_to_swagger(metadata))
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop(_notification=nil)
|
def stop(_notification = nil)
|
||||||
@config.swagger_docs.each do |url_path, doc|
|
@config.swagger_docs.each do |url_path, doc|
|
||||||
## OA3
|
## OA3
|
||||||
# # remove 2.0 parameters
|
# # remove 2.0 parameters
|
||||||
@ -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,31 +140,31 @@ 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)')
|
|
||||||
|
|
||||||
swagger_doc[:servers] = { urls: [] }
|
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[:schemes].each do |scheme|
|
|
||||||
swagger_doc[:servers][:urls] << scheme + '://' + swagger_doc[:host] + swagger_doc[:basePath]
|
|
||||||
end
|
|
||||||
|
|
||||||
swagger_doc.delete(:schemes)
|
swagger_doc[:servers] = { urls: [] }
|
||||||
swagger_doc.delete(:host)
|
swagger_doc[:schemes].each do |scheme|
|
||||||
swagger_doc.delete(:basePath)
|
swagger_doc[:servers][:urls] << scheme + '://' + swagger_doc[:host] + swagger_doc[:basePath]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
swagger_doc.delete(:schemes)
|
||||||
|
swagger_doc.delete(:host)
|
||||||
|
swagger_doc.delete(:basePath)
|
||||||
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|
|
|
||||||
if v.has_key?(:flow)
|
schemes.each do |name, v|
|
||||||
ActiveSupport::Deprecation.warn("Rswag::Specs: WARNING: securityDefinitions flow is replaced in OpenAPI3! Rename to components/securitySchemes/#{name}/flows[] (in swagger_helper.rb)")
|
next unless v.key?(:flow)
|
||||||
flow = swagger_doc[:components][:securitySchemes][name].delete(:flow)
|
|
||||||
swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow])
|
ActiveSupport::Deprecation.warn("Rswag::Specs: WARNING: securityDefinitions flow is replaced in OpenAPI3! Rename to components/securitySchemes/#{name}/flows[] (in swagger_helper.rb)")
|
||||||
end
|
flow = swagger_doc[:components][:securitySchemes][name].delete(:flow)
|
||||||
end
|
swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow])
|
||||||
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(
|
||||||
@ -12,12 +13,12 @@ namespace :rswag do
|
|||||||
|
|
||||||
# NOTE: rspec 2.x support
|
# NOTE: rspec 2.x support
|
||||||
if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run
|
if Rswag::Specs::RSPEC_VERSION > 2 && Rswag::Specs.config.swagger_dry_run
|
||||||
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined' ]
|
t.rspec_opts = ['--format Rswag::Specs::SwaggerFormatter', '--dry-run', '--order defined']
|
||||||
else
|
else
|
||||||
t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--order defined' ]
|
t.rspec_opts = ['--format Rswag::Specs::SwaggerFormatter', '--order defined']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
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|
|
||||||
@ -13,7 +13,7 @@ Gem::Specification.new do |s|
|
|||||||
s.description = 'Simplify API integration testing with a succinct rspec DSL and generate Swagger files directly from your rspecs'
|
s.description = 'Simplify API integration testing with a succinct rspec DSL and generate Swagger files directly from your rspecs'
|
||||||
s.license = 'MIT'
|
s.license = 'MIT'
|
||||||
|
|
||||||
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile' ]
|
s.files = Dir['{lib}/**/*'] + ['MIT-LICENSE', 'Rakefile']
|
||||||
|
|
||||||
s.add_dependency 'activesupport', '>= 3.1', '< 7.0'
|
s.add_dependency 'activesupport', '>= 3.1', '< 7.0'
|
||||||
s.add_dependency 'railties', '>= 3.1', '< 7.0'
|
s.add_dependency 'railties', '>= 3.1', '< 7.0'
|
||||||
|
|||||||
@ -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') }
|
||||||
|
|
||||||
@ -50,12 +49,12 @@ module Rswag
|
|||||||
|
|
||||||
it "adds to the 'operation' metadata" do
|
it "adds to the 'operation' metadata" do
|
||||||
expect(api_metadata[:operation]).to match(
|
expect(api_metadata[:operation]).to match(
|
||||||
tags: [ 'Blogs', 'Admin' ],
|
tags: ['Blogs', 'Admin'],
|
||||||
description: 'Some description',
|
description: 'Some description',
|
||||||
operationId: 'createBlog',
|
operationId: 'createBlog',
|
||||||
consumes: [ 'application/json', 'application/xml' ],
|
consumes: ['application/json', 'application/xml'],
|
||||||
produces: [ 'application/json', 'application/xml' ],
|
produces: ['application/json', 'application/xml'],
|
||||||
schemes: [ 'http', 'https' ],
|
schemes: ['http', 'https'],
|
||||||
deprecated: true
|
deprecated: true
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@ -76,12 +75,12 @@ module Rswag
|
|||||||
|
|
||||||
it "adds to the 'operation' metadata" do
|
it "adds to the 'operation' metadata" do
|
||||||
expect(api_metadata[:operation]).to match(
|
expect(api_metadata[:operation]).to match(
|
||||||
tags: [ 'Blogs', 'Admin' ],
|
tags: ['Blogs', 'Admin'],
|
||||||
description: 'Some description',
|
description: 'Some description',
|
||||||
operationId: 'createBlog',
|
operationId: 'createBlog',
|
||||||
consumes: [ 'application/json', 'application/xml' ],
|
consumes: ['application/json', 'application/xml'],
|
||||||
produces: [ 'application/json', 'application/xml' ],
|
produces: ['application/json', 'application/xml'],
|
||||||
schemes: [ 'http', 'https' ],
|
schemes: ['http', 'https'],
|
||||||
deprecated: true,
|
deprecated: true,
|
||||||
security: { api_key: [] }
|
security: { api_key: [] }
|
||||||
)
|
)
|
||||||
@ -123,14 +122,13 @@ 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
|
||||||
|
|
||||||
it "adds to the 'path_item parameters' metadata" do
|
it "adds to the 'path_item parameters' metadata" do
|
||||||
expect(api_metadata[:path_item][:parameters]).to match(
|
expect(api_metadata[:path_item][:parameters]).to match(
|
||||||
[ name: :blog, in: :body, schema: { type: 'object' } ]
|
[name: :blog, in: :body, schema: { type: 'object' }]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -141,7 +139,7 @@ module Rswag
|
|||||||
|
|
||||||
it "adds to the 'operation parameters' metadata" do
|
it "adds to the 'operation parameters' metadata" do
|
||||||
expect(api_metadata[:operation][:parameters]).to match(
|
expect(api_metadata[:operation][:parameters]).to match(
|
||||||
[ name: :blog, in: :body, schema: { type: 'object' } ]
|
[name: :blog, in: :body, schema: { type: 'object' }]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -152,7 +150,7 @@ module Rswag
|
|||||||
|
|
||||||
it "automatically sets the 'required' flag" do
|
it "automatically sets the 'required' flag" do
|
||||||
expect(api_metadata[:operation][:parameters]).to match(
|
expect(api_metadata[:operation][:parameters]).to match(
|
||||||
[ name: :id, in: :path, required: true ]
|
[name: :id, in: :path, required: true]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -162,7 +160,7 @@ module Rswag
|
|||||||
let(:api_metadata) { { operation: {} } }
|
let(:api_metadata) { { operation: {} } }
|
||||||
|
|
||||||
it "does not require the 'in' parameter key" do
|
it "does not require the 'in' parameter key" do
|
||||||
expect(api_metadata[:operation][:parameters]).to match([ name: :id ])
|
expect(api_metadata[:operation][:parameters]).to match([name: :id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -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') }
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ module Rswag
|
|||||||
operation: {
|
operation: {
|
||||||
verb: :put,
|
verb: :put,
|
||||||
summary: 'Updates a blog',
|
summary: 'Updates a blog',
|
||||||
consumes: [ 'application/json' ],
|
consumes: ['application/json'],
|
||||||
parameters: [
|
parameters: [
|
||||||
{ name: :blog_id, in: :path, type: 'integer' },
|
{ name: :blog_id, in: :path, type: 'integer' },
|
||||||
{ name: 'id', in: :path, type: 'integer' },
|
{ name: 'id', in: :path, type: 'integer' },
|
||||||
|
|||||||
@ -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
|
||||||
@ -65,40 +64,40 @@ module Rswag
|
|||||||
metadata[:operation][:parameters] = [
|
metadata[:operation][:parameters] = [
|
||||||
{ name: 'things', in: :query, type: :array, collectionFormat: collection_format }
|
{ name: 'things', in: :query, type: :array, collectionFormat: collection_format }
|
||||||
]
|
]
|
||||||
allow(example).to receive(:things).and_return([ 'foo', 'bar' ])
|
allow(example).to receive(:things).and_return(['foo', 'bar'])
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
@ -106,7 +105,7 @@ module Rswag
|
|||||||
|
|
||||||
context "'header' parameters" do
|
context "'header' parameters" do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:parameters] = [ { name: 'Api-Key', in: :header, type: :string } ]
|
metadata[:operation][:parameters] = [{ name: 'Api-Key', in: :header, type: :string }]
|
||||||
allow(example).to receive(:'Api-Key').and_return('foobar')
|
allow(example).to receive(:'Api-Key').and_return('foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -129,9 +128,9 @@ 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
|
||||||
|
|
||||||
context "no 'Content-Type' provided" do
|
context "no 'Content-Type' provided" do
|
||||||
@ -152,18 +151,18 @@ module Rswag
|
|||||||
|
|
||||||
context 'JSON payload' do
|
context 'JSON payload' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:parameters] = [ { name: 'comment', in: :body, schema: { type: 'object' } } ]
|
metadata[:operation][:parameters] = [{ name: 'comment', in: :body, schema: { type: 'object' } }]
|
||||||
allow(example).to receive(:comment).and_return(text: 'Some comment')
|
allow(example).to receive(:comment).and_return(text: 'Some comment')
|
||||||
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
|
||||||
|
|
||||||
context 'form payload' do
|
context 'form payload' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:consumes] = [ 'multipart/form-data' ]
|
metadata[:operation][:consumes] = ['multipart/form-data']
|
||||||
metadata[:operation][:parameters] = [
|
metadata[:operation][:parameters] = [
|
||||||
{ name: 'f1', in: :formData, type: :string },
|
{ name: 'f1', in: :formData, type: :string },
|
||||||
{ name: 'f2', in: :formData, type: :string }
|
{ name: 'f2', in: :formData, type: :string }
|
||||||
@ -183,7 +182,7 @@ module Rswag
|
|||||||
|
|
||||||
context 'produces content' do
|
context 'produces content' do
|
||||||
before do
|
before do
|
||||||
metadata[:operation][:produces] = [ 'application/json', 'application/xml' ]
|
metadata[:operation][:produces] = ['application/json', 'application/xml']
|
||||||
end
|
end
|
||||||
|
|
||||||
context "no 'Accept' value provided" do
|
context "no 'Accept' value provided" do
|
||||||
@ -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
|
||||||
@ -207,7 +206,7 @@ module Rswag
|
|||||||
context 'swagger 2.0' do
|
context 'swagger 2.0' do
|
||||||
before do
|
before do
|
||||||
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
|
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
|
||||||
metadata[:operation][:security] = [ basic: [] ]
|
metadata[:operation][:security] = [basic: []]
|
||||||
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -220,7 +219,7 @@ module Rswag
|
|||||||
let(:swagger_doc) { { openapi: '3.0.1' } }
|
let(:swagger_doc) { { openapi: '3.0.1' } }
|
||||||
before do
|
before do
|
||||||
swagger_doc[:components] = { securitySchemes: { basic: { type: :basic } } }
|
swagger_doc[:components] = { securitySchemes: { basic: { type: :basic } } }
|
||||||
metadata[:operation][:security] = [ basic: [] ]
|
metadata[:operation][:security] = [basic: []]
|
||||||
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -234,7 +233,7 @@ module Rswag
|
|||||||
before do
|
before do
|
||||||
allow(ActiveSupport::Deprecation).to receive(:warn)
|
allow(ActiveSupport::Deprecation).to receive(:warn)
|
||||||
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
|
swagger_doc[:securityDefinitions] = { basic: { type: :basic } }
|
||||||
metadata[:operation][:security] = [ basic: [] ]
|
metadata[:operation][:security] = [basic: []]
|
||||||
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -254,7 +253,7 @@ module Rswag
|
|||||||
# swagger_doc[:components] = { securitySchemes: {
|
# swagger_doc[:components] = { securitySchemes: {
|
||||||
# apiKey: { type: :apiKey, name: 'api_key', in: key_location }
|
# apiKey: { type: :apiKey, name: 'api_key', in: key_location }
|
||||||
# } }
|
# } }
|
||||||
metadata[:operation][:security] = [ apiKey: [] ]
|
metadata[:operation][:security] = [apiKey: []]
|
||||||
allow(example).to receive(:api_key).and_return('foobar')
|
allow(example).to receive(:api_key).and_return('foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -294,12 +293,12 @@ module Rswag
|
|||||||
|
|
||||||
context 'oauth2' do
|
context 'oauth2' do
|
||||||
before do
|
before do
|
||||||
swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: [ 'read:blogs' ] } }
|
swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: ['read:blogs'] } }
|
||||||
## OA3
|
## OA3
|
||||||
# swagger_doc[:components] = { securitySchemes: {
|
# swagger_doc[:components] = { securitySchemes: {
|
||||||
# oauth2: { type: :oauth2, scopes: ['read:blogs'] }
|
# oauth2: { type: :oauth2, scopes: ['read:blogs'] }
|
||||||
# } }
|
# } }
|
||||||
metadata[:operation][:security] = [ oauth2: [ 'read:blogs' ] ]
|
metadata[:operation][:security] = [oauth2: ['read:blogs']]
|
||||||
allow(example).to receive(:Authorization).and_return('Bearer foobar')
|
allow(example).to receive(:Authorization).and_return('Bearer foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -319,26 +318,26 @@ module Rswag
|
|||||||
# basic: { type: :basic },
|
# basic: { type: :basic },
|
||||||
# api_key: { type: :apiKey, name: 'api_key', in: :query }
|
# api_key: { type: :apiKey, name: 'api_key', in: :query }
|
||||||
# } }
|
# } }
|
||||||
metadata[:operation][:security] = [ { basic: [], api_key: [] } ]
|
metadata[:operation][:security] = [{ basic: [], api_key: [] }]
|
||||||
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
allow(example).to receive(:Authorization).and_return('Basic foobar')
|
||||||
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 }]
|
||||||
allow(example).to receive(:q1).and_return('foo')
|
allow(example).to receive(:q1).and_return('foo')
|
||||||
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
|
||||||
@ -347,7 +346,7 @@ module Rswag
|
|||||||
context 'swagger 2.0' do
|
context 'swagger 2.0' do
|
||||||
before do
|
before do
|
||||||
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
|
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
|
||||||
metadata[:operation][:parameters] = [ { '$ref' => '#/parameters/q1' } ]
|
metadata[:operation][:parameters] = [{ '$ref' => '#/parameters/q1' }]
|
||||||
allow(example).to receive(:q1).and_return('foo')
|
allow(example).to receive(:q1).and_return('foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -360,7 +359,7 @@ module Rswag
|
|||||||
let(:swagger_doc) { { openapi: '3.0.1' } }
|
let(:swagger_doc) { { openapi: '3.0.1' } }
|
||||||
before do
|
before do
|
||||||
swagger_doc[:components] = { parameters: { q1: { name: 'q1', in: :query, type: :string } } }
|
swagger_doc[:components] = { parameters: { q1: { name: 'q1', in: :query, type: :string } } }
|
||||||
metadata[:operation][:parameters] = [ { '$ref' => '#/components/parameters/q1' } ]
|
metadata[:operation][:parameters] = [{ '$ref' => '#/components/parameters/q1' }]
|
||||||
allow(example).to receive(:q1).and_return('foo')
|
allow(example).to receive(:q1).and_return('foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -374,7 +373,7 @@ module Rswag
|
|||||||
before do
|
before do
|
||||||
allow(ActiveSupport::Deprecation).to receive(:warn)
|
allow(ActiveSupport::Deprecation).to receive(:warn)
|
||||||
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
|
swagger_doc[:parameters] = { q1: { name: 'q1', in: :query, type: :string } }
|
||||||
metadata[:operation][:parameters] = [ { '$ref' => '#/parameters/q1' } ]
|
metadata[:operation][:parameters] = [{ '$ref' => '#/parameters/q1' }]
|
||||||
allow(example).to receive(:q1).and_return('foo')
|
allow(example).to receive(:q1).and_return('foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -396,20 +395,20 @@ 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
|
||||||
expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml')
|
expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml')
|
||||||
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
|
||||||
# swagger_doc[:components] = { securitySchemes: { apiKey: { type: :apiKey, name: 'api_key', in: :query } } }
|
# swagger_doc[:components] = { securitySchemes: { apiKey: { type: :apiKey, name: 'api_key', in: :query } } }
|
||||||
swagger_doc[:security] = [ apiKey: [] ]
|
swagger_doc[:security] = [apiKey: []]
|
||||||
allow(example).to receive(:api_key).and_return('foobar')
|
allow(example).to receive(:api_key).and_return('foobar')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -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) }
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ module Rswag
|
|||||||
schema: {
|
schema: {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: { text: { type: :string } },
|
properties: { text: { type: :string } },
|
||||||
required: [ 'text' ]
|
required: ['text']
|
||||||
}
|
}
|
||||||
## OA3
|
## OA3
|
||||||
# content: {
|
# content: {
|
||||||
@ -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
|
||||||
@ -75,7 +74,7 @@ module Rswag
|
|||||||
'blog' => {
|
'blog' => {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: { foo: { type: :string } },
|
properties: { foo: { type: :string } },
|
||||||
required: [ 'foo' ]
|
required: ['foo']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
|
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
|
||||||
@ -96,7 +95,7 @@ module Rswag
|
|||||||
'blog' => {
|
'blog' => {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: { foo: { type: :string } },
|
properties: { foo: { type: :string } },
|
||||||
required: [ 'foo' ]
|
required: ['foo']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +115,7 @@ module Rswag
|
|||||||
'blog' => {
|
'blog' => {
|
||||||
type: :object,
|
type: :object,
|
||||||
properties: { foo: { type: :string } },
|
properties: { foo: { type: :string } },
|
||||||
required: [ 'foo' ]
|
required: ['foo']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
|
metadata[:response][:schema] = { '$ref' => '#/definitions/blog' }
|
||||||
|
|||||||
@ -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,19 +68,21 @@ 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',
|
{
|
||||||
basePath: '/foo',
|
openapi: '3.0.1',
|
||||||
schemes: ['http', 'https'],
|
basePath: '/foo',
|
||||||
host: 'api.example.com',
|
schemes: ['http', 'https'],
|
||||||
components: {
|
host: 'api.example.com',
|
||||||
securitySchemes: {
|
components: {
|
||||||
my_oauth: {
|
securitySchemes: {
|
||||||
flow: :anything
|
my_oauth: {
|
||||||
|
flow: :anything
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} }
|
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