From 6b4f49aacb3edaaef17f52666e2ebe5e2c419763 Mon Sep 17 00:00:00 2001 From: Greg Myers Date: Tue, 24 Mar 2020 16:02:13 +0000 Subject: [PATCH] Basic rubocops --- rswag-specs/Rakefile | 4 -- .../lib/generators/rspec/swagger_generator.rb | 4 +- .../rswag/specs/install/install_generator.rb | 5 +- .../specs/install/templates/swagger_helper.rb | 4 +- rswag-specs/lib/rswag/route_parser.rb | 6 +- rswag-specs/lib/rswag/specs.rb | 3 +- rswag-specs/lib/rswag/specs/configuration.rb | 6 +- .../lib/rswag/specs/example_group_helpers.rb | 20 +++--- .../lib/rswag/specs/extended_schema.rb | 5 +- rswag-specs/lib/rswag/specs/railtie.rb | 3 +- .../lib/rswag/specs/request_factory.rb | 37 +++++----- .../lib/rswag/specs/response_validator.rb | 6 +- .../lib/rswag/specs/swagger_formatter.rb | 49 +++++++------ rswag-specs/lib/tasks/rswag-specs_tasks.rake | 9 +-- rswag-specs/rswag-specs.gemspec | 4 +- .../rspec/swagger_generator_spec.rb | 15 ++-- .../rswag/specs/install_generator_spec.rb | 7 +- .../spec/rswag/specs/configuration_spec.rb | 1 - .../rswag/specs/example_group_helpers_spec.rb | 26 ++++--- .../spec/rswag/specs/example_helpers_spec.rb | 3 +- .../spec/rswag/specs/request_factory_spec.rb | 69 +++++++++---------- .../rswag/specs/response_validator_spec.rb | 17 +++-- .../rswag/specs/swagger_formatter_spec.rb | 29 ++++---- rswag-specs/spec/spec_helper.rb | 2 + rswag-specs/spec/swagger_helper.rb | 2 + 25 files changed, 171 insertions(+), 165 deletions(-) diff --git a/rswag-specs/Rakefile b/rswag-specs/Rakefile index 2cbae8a..2d528b1 100644 --- a/rswag-specs/Rakefile +++ b/rswag-specs/Rakefile @@ -20,8 +20,4 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end - - - Bundler::GemHelper.install_tasks - diff --git a/rswag-specs/lib/generators/rspec/swagger_generator.rb b/rswag-specs/lib/generators/rspec/swagger_generator.rb index ddb862c..7299176 100644 --- a/rswag-specs/lib/generators/rspec/swagger_generator.rb +++ b/rswag-specs/lib/generators/rspec/swagger_generator.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + require 'rswag/route_parser' require 'rails/generators' module Rspec class SwaggerGenerator < ::Rails::Generators::NamedBase - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path('templates', __dir__) def setup @routes = Rswag::RouteParser.new(controller_path).routes diff --git a/rswag-specs/lib/generators/rswag/specs/install/install_generator.rb b/rswag-specs/lib/generators/rswag/specs/install/install_generator.rb index 050c57b..92f9dd8 100644 --- a/rswag-specs/lib/generators/rswag/specs/install/install_generator.rb +++ b/rswag-specs/lib/generators/rswag/specs/install/install_generator.rb @@ -1,10 +1,11 @@ +# frozen_string_literal: true + require 'rails/generators' module Rswag module Specs - class InstallGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path('templates', __dir__) def add_swagger_helper template('swagger_helper.rb', 'spec/swagger_helper.rb') diff --git a/rswag-specs/lib/generators/rswag/specs/install/templates/swagger_helper.rb b/rswag-specs/lib/generators/rswag/specs/install/templates/swagger_helper.rb index 8a484b7..8f71560 100644 --- a/rswag-specs/lib/generators/rswag/specs/install/templates/swagger_helper.rb +++ b/rswag-specs/lib/generators/rswag/specs/install/templates/swagger_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.configure do |config| @@ -25,7 +27,7 @@ RSpec.configure do |config| url: 'https://{defaultHost}', variables: { defaultHost: { - default: 'www.example.com' + default: 'www.example.com' } } } diff --git a/rswag-specs/lib/rswag/route_parser.rb b/rswag-specs/lib/rswag/route_parser.rb index 523b36b..03470ee 100644 --- a/rswag-specs/lib/rswag/route_parser.rb +++ b/rswag-specs/lib/rswag/route_parser.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rswag class RouteParser attr_reader :controller @@ -9,7 +11,7 @@ module Rswag def routes ::Rails.application.routes.routes.select do |route| route.defaults[:controller] == controller - end.reduce({}) do |tree, route| + end.each_with_object({}) do |tree, route| path = path_from(route) verb = verb_from(route) tree[path] ||= { params: params_from(route), actions: {} } @@ -28,7 +30,7 @@ module Rswag def verb_from(route) verb = route.verb - if verb.kind_of? String + if verb.is_a? String verb.downcase else verb.source.gsub(/[$^]/, '').downcase diff --git a/rswag-specs/lib/rswag/specs.rb b/rswag-specs/lib/rswag/specs.rb index a3f0c16..1db62a5 100644 --- a/rswag-specs/lib/rswag/specs.rb +++ b/rswag-specs/lib/rswag/specs.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rspec/core' require 'rswag/specs/example_group_helpers' require 'rswag/specs/example_helpers' @@ -6,7 +8,6 @@ require 'rswag/specs/railtie' if defined?(Rails::Railtie) module Rswag module Specs - # Extend RSpec with a swagger-based DSL ::RSpec.configure do |c| c.add_setting :swagger_root diff --git a/rswag-specs/lib/rswag/specs/configuration.rb b/rswag-specs/lib/rswag/specs/configuration.rb index 900c33e..b9dca6b 100644 --- a/rswag-specs/lib/rswag/specs/configuration.rb +++ b/rswag-specs/lib/rswag/specs/configuration.rb @@ -2,9 +2,7 @@ module Rswag module Specs - class Configuration - def initialize(rspec_config) @rspec_config = rspec_config end @@ -14,6 +12,7 @@ module Rswag if @rspec_config.swagger_root.nil? raise ConfigurationError, 'No swagger_root provided. See swagger_helper.rb' end + @rspec_config.swagger_root end end @@ -23,6 +22,7 @@ module Rswag if @rspec_config.swagger_docs.nil? || @rspec_config.swagger_docs.empty? raise ConfigurationError, 'No swagger_docs defined. See swagger_helper.rb' end + @rspec_config.swagger_docs end end @@ -37,6 +37,7 @@ module Rswag @swagger_format ||= begin @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) + @rspec_config.swagger_format end end @@ -44,6 +45,7 @@ module Rswag def get_swagger_doc(name) return swagger_docs.values.first if name.nil? raise ConfigurationError, "Unknown swagger_doc '#{name}'" unless swagger_docs[name] + swagger_docs[name] end diff --git a/rswag-specs/lib/rswag/specs/example_group_helpers.rb b/rswag-specs/lib/rswag/specs/example_group_helpers.rb index 227b4ed..a20e823 100644 --- a/rswag-specs/lib/rswag/specs/example_group_helpers.rb +++ b/rswag-specs/lib/rswag/specs/example_group_helpers.rb @@ -1,20 +1,21 @@ +# frozen_string_literal: true + module Rswag module Specs module ExampleGroupHelpers - - def path(template, metadata={}, &block) + def path(template, metadata = {}, &block) metadata[:path_item] = { template: template } describe(template, metadata, &block) 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| api_metadata = { operation: { verb: verb, summary: summary } } describe(verb, api_metadata, &block) end end - [ :operationId, :deprecated, :security ].each do |attr_name| + [:operationId, :deprecated, :security].each do |attr_name| define_method(attr_name) do |value| metadata[:operation][attr_name] = value end @@ -23,13 +24,14 @@ module Rswag # NOTE: 'description' requires special treatment because ExampleGroup already # defines a method with that name. Provide an override that supports the existing # functionality while also setting the appropriate metadata if applicable - def description(value=nil) + def description(value = nil) return super() if value.nil? + metadata[:operation][:description] = value end # 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| metadata[:operation][attr_name] = value end @@ -125,13 +127,12 @@ module Rswag # end # end - def parameter(attributes) if attributes[:in] && attributes[:in].to_sym == :path attributes[:required] = true end - if metadata.has_key?(:operation) + if metadata.key?(:operation) metadata[:operation][:parameters] ||= [] metadata[:operation][:parameters] << attributes else @@ -140,7 +141,7 @@ module Rswag end end - def response(code, description, metadata={}, &block) + def response(code, description, metadata = {}, &block) metadata[:response] = { code: code, description: description } context(description, metadata, &block) end @@ -165,6 +166,7 @@ module Rswag # rspec-core ExampleGroup def examples(example = nil) return super() if example.nil? + metadata[:response][:examples] = example end diff --git a/rswag-specs/lib/rswag/specs/extended_schema.rb b/rswag-specs/lib/rswag/specs/extended_schema.rb index e662904..9923bda 100644 --- a/rswag-specs/lib/rswag/specs/extended_schema.rb +++ b/rswag-specs/lib/rswag/specs/extended_schema.rb @@ -5,7 +5,6 @@ require 'json-schema' module Rswag module Specs class ExtendedSchema < JSON::Schema::Draft4 - def initialize super @attributes['type'] = ExtendedTypeAttribute @@ -15,11 +14,11 @@ module Rswag end 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 # 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 + super end end diff --git a/rswag-specs/lib/rswag/specs/railtie.rb b/rswag-specs/lib/rswag/specs/railtie.rb index 6786044..0644f3c 100644 --- a/rswag-specs/lib/rswag/specs/railtie.rb +++ b/rswag-specs/lib/rswag/specs/railtie.rb @@ -3,9 +3,8 @@ module Rswag module Specs class Railtie < ::Rails::Railtie - rake_tasks do - load File.expand_path('../../../tasks/rswag-specs_tasks.rake', __FILE__) + load File.expand_path('../../tasks/rswag-specs_tasks.rake', __dir__) end generators do diff --git a/rswag-specs/lib/rswag/specs/request_factory.rb b/rswag-specs/lib/rswag/specs/request_factory.rb index 95f3002..5af3649 100644 --- a/rswag-specs/lib/rswag/specs/request_factory.rb +++ b/rswag-specs/lib/rswag/specs/request_factory.rb @@ -8,7 +8,6 @@ require 'byebug' module Rswag module Specs class RequestFactory - def initialize(config = ::Rswag::Specs.config) @config = config end @@ -54,7 +53,7 @@ module Rswag if doc_version(swagger_doc).start_with?('2') (swagger_doc[:securityDefinitions] || {}).slice(*scheme_names).values 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)') swagger_doc[:components] ||= { securitySchemes: swagger_doc[:securityDefinitions] } swagger_doc.delete(:securityDefinitions) @@ -67,8 +66,8 @@ module Rswag def resolve_parameter(ref, swagger_doc) key = key_version(ref, swagger_doc) definitions = definition_version(swagger_doc) - raise "Referenced parameter '#{ref}' must be defined" unless definitions && definitions[key] + definitions[key] end @@ -89,7 +88,7 @@ module Rswag if doc_version(swagger_doc).start_with?('2') swagger_doc[:parameters] 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)') swagger_doc[:parameters] else @@ -106,21 +105,21 @@ module Rswag def add_path(request, metadata, swagger_doc, parameters, example) 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| - template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) + path_template.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) end parameters.select { |p| p[:in] == :query }.each_with_index do |p, i| - template.concat(i == 0 ? '?' : '&') - template.concat(build_query_string_part(p, example.send(p[:name]))) + path_template.concat(i.zero? ? '?' : '&') + path_template.concat(build_query_string_part(p, example.send(p[:name]))) end end end def build_query_string_part(param, value) 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] when :ssv @@ -139,43 +138,43 @@ module Rswag def add_headers(request, metadata, swagger_doc, parameters, example) tuples = parameters .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 produces = metadata[:operation][:produces] || swagger_doc[:produces] if produces accept = example.respond_to?(:Accept) ? example.send(:Accept) : produces.first - tuples << [ 'Accept', accept ] + tuples << ['Accept', accept] end # Content-Type header consumes = metadata[:operation][:consumes] || swagger_doc[:consumes] if consumes content_type = example.respond_to?(:'Content-Type') ? example.send(:'Content-Type') : consumes.first - tuples << [ 'Content-Type', content_type ] + tuples << ['Content-Type', content_type] end # Rails test infrastructure requires rackified headers rackified_tuples = tuples.map do |pair| [ case pair[0] - when 'Accept' then 'HTTP_ACCEPT' - when 'Content-Type' then 'CONTENT_TYPE' - when 'Authorization' then 'HTTP_AUTHORIZATION' - else pair[0] + when 'Accept' then 'HTTP_ACCEPT' + when 'Content-Type' then 'CONTENT_TYPE' + when 'Authorization' then 'HTTP_AUTHORIZATION' + else pair[0] end, pair[1] ] end - request[:headers] = Hash[ rackified_tuples ] + request[:headers] = Hash[rackified_tuples] end def add_payload(request, parameters, example) content_type = request[:headers]['CONTENT_TYPE'] 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) else request[:payload] = build_json_payload(parameters, example) @@ -189,7 +188,7 @@ module Rswag # PROS: simple to implement, CONS: serialization/deserialization is bypassed in test tuples = parameters .select { |p| p[:in] == :formData } - .map { |p| [ p[:name], example.send(p[:name]) ] } + .map { |p| [p[:name], example.send(p[:name])] } Hash[tuples] end diff --git a/rswag-specs/lib/rswag/specs/response_validator.rb b/rswag-specs/lib/rswag/specs/response_validator.rb index 2254758..e1eed40 100644 --- a/rswag-specs/lib/rswag/specs/response_validator.rb +++ b/rswag-specs/lib/rswag/specs/response_validator.rb @@ -69,12 +69,12 @@ module Rswag if version.start_with?('2') swagger_doc.slice(:definitions) 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)') - return swagger_doc.slice(:definitions) + swagger_doc.slice(:definitions) else components = swagger_doc[:components] || {} - return { components: { schemas: components[:schemas] } } + { components: { schemas: components[:schemas] } } end end end diff --git a/rswag-specs/lib/rswag/specs/swagger_formatter.rb b/rswag-specs/lib/rswag/specs/swagger_formatter.rb index 73eefbd..06eb611 100644 --- a/rswag-specs/lib/rswag/specs/swagger_formatter.rb +++ b/rswag-specs/lib/rswag/specs/swagger_formatter.rb @@ -6,7 +6,6 @@ require 'swagger_helper' module Rswag module Specs class SwaggerFormatter - # NOTE: rspec 2.x support if RSPEC_VERSION > 2 ::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 # docs. 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]) - if !doc_version(swagger_doc).start_with?('2') + unless doc_version(swagger_doc).start_with?('2') upgrade_request_type!(metadata) upgrade_servers!(swagger_doc) upgrade_oauth!(swagger_doc) @@ -43,7 +42,7 @@ module Rswag swagger_doc.deep_merge!(metadata_to_swagger(metadata)) end - def stop(_notification=nil) + def stop(_notification = nil) @config.swagger_docs.each do |url_path, doc| ## OA3 # # remove 2.0 parameters @@ -68,7 +67,7 @@ module Rswag file_path = File.join(@config.swagger_root, url_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.write(pretty_generate(doc)) @@ -96,7 +95,7 @@ module Rswag def metadata_to_swagger(metadata) response_code = metadata[:response][:code] - response = metadata[:response].reject { |k,v| k == :code } + response = metadata[:response].reject { |k, _v| k == :code } ## OA3 # content_type = metadata[:response][:content].present? ? metadata[:response][:content].keys.first : 'application/json' # # need to merge in to response @@ -111,12 +110,12 @@ module Rswag verb = metadata[:operation][:verb] operation = metadata[:operation] - .reject { |k,v| k == :verb } + .reject { |k, _v| k == :verb } .merge(responses: { response_code => response }) path_template = metadata[:path_item][:template] path_item = metadata[:path_item] - .reject { |k,v| k == :template } + .reject { |k, _v| k == :template } .merge(verb => operation) { paths: { path_template => path_item } } @@ -141,31 +140,31 @@ module Rswag end def upgrade_servers!(swagger_doc) - if swagger_doc[:servers].nil? && swagger_doc.has_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)') + return unless swagger_doc[:servers].nil? && swagger_doc.key?(:schemes) - swagger_doc[:servers] = { urls: [] } - swagger_doc[:schemes].each do |scheme| - swagger_doc[:servers][:urls] << scheme + '://' + swagger_doc[:host] + swagger_doc[:basePath] - end + 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.delete(:schemes) - swagger_doc.delete(:host) - swagger_doc.delete(:basePath) + swagger_doc[:servers] = { urls: [] } + swagger_doc[:schemes].each do |scheme| + swagger_doc[:servers][:urls] << scheme + '://' + swagger_doc[:host] + swagger_doc[:basePath] end + + swagger_doc.delete(:schemes) + swagger_doc.delete(:host) + swagger_doc.delete(:basePath) end def upgrade_oauth!(swagger_doc) # find flow in securitySchemes (securityDefinitions will have been re-written) schemes = swagger_doc.dig(:components, :securitySchemes) - if schemes && schemes.any?{ |_k, v| v.has_key?(:flow) } - schemes.each do |name, v| - if v.has_key?(:flow) - 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) - swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow]) - end - end + return unless schemes&.any? { |_k, v| v.key?(:flow) } + + schemes.each do |name, v| + 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)") + flow = swagger_doc[:components][:securitySchemes][name].delete(:flow) + swagger_doc[:components][:securitySchemes][name].merge!(flows: [flow]) end end end diff --git a/rswag-specs/lib/tasks/rswag-specs_tasks.rake b/rswag-specs/lib/tasks/rswag-specs_tasks.rake index 54412a2..41f264c 100644 --- a/rswag-specs/lib/tasks/rswag-specs_tasks.rake +++ b/rswag-specs/lib/tasks/rswag-specs_tasks.rake @@ -1,8 +1,9 @@ +# frozen_string_literal: true + require 'rspec/core/rake_task' namespace :rswag do namespace :specs do - desc 'Generate Swagger JSON files from integration specs' RSpec::Core::RakeTask.new('swaggerize') do |t| t.pattern = ENV.fetch( @@ -12,12 +13,12 @@ namespace :rswag do # NOTE: rspec 2.x support 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 - t.rspec_opts = [ '--format Rswag::Specs::SwaggerFormatter', '--order defined' ] + t.rspec_opts = ['--format Rswag::Specs::SwaggerFormatter', '--order defined'] end end end end -task :rswag => ['rswag:specs:swaggerize'] +task rswag: ['rswag:specs:swaggerize'] diff --git a/rswag-specs/rswag-specs.gemspec b/rswag-specs/rswag-specs.gemspec index 8facb84..0f7dbc6 100644 --- a/rswag-specs/rswag-specs.gemspec +++ b/rswag-specs/rswag-specs.gemspec @@ -1,6 +1,6 @@ # 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: 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.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 'railties', '>= 3.1', '< 7.0' diff --git a/rswag-specs/spec/generators/rspec/swagger_generator_spec.rb b/rswag-specs/spec/generators/rspec/swagger_generator_spec.rb index f11ea65..1349230 100644 --- a/rswag-specs/spec/generators/rspec/swagger_generator_spec.rb +++ b/rswag-specs/spec/generators/rspec/swagger_generator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'generator_spec' require 'generators/rspec/swagger_generator' require 'tmpdir' @@ -9,12 +11,11 @@ module Rspec before(:all) do prepare_destination - fixtures_dir = File.expand_path('../fixtures', __FILE__) + fixtures_dir = File.expand_path('fixtures', __dir__) FileUtils.cp_r("#{fixtures_dir}/spec", destination_root) end after(:all) do - end it 'installs the swagger_helper for rspec' do @@ -31,11 +32,11 @@ module Rspec def fake_routes { - "/posts/{post_id}/comments/{id}" => { - :params => ["post_id", "id"], - :actions => { - "get" => { :summary=>"show comment" }, - "patch" => { :summary=>"update_comments comment" } + '/posts/{post_id}/comments/{id}' => { + params: ['post_id', 'id'], + actions: { + 'get' => { summary: 'show comment' }, + 'patch' => { summary: 'update_comments comment' } } } } diff --git a/rswag-specs/spec/generators/rswag/specs/install_generator_spec.rb b/rswag-specs/spec/generators/rswag/specs/install_generator_spec.rb index 809e4f6..ffd7ddc 100644 --- a/rswag-specs/spec/generators/rswag/specs/install_generator_spec.rb +++ b/rswag-specs/spec/generators/rswag/specs/install_generator_spec.rb @@ -1,16 +1,17 @@ +# frozen_string_literal: true + require 'generator_spec' require 'generators/rswag/specs/install/install_generator' module Rswag module Specs - RSpec.describe InstallGenerator do include GeneratorSpec::TestCase - destination File.expand_path('../tmp', __FILE__) + destination File.expand_path('tmp', __dir__) before(:all) do prepare_destination - fixtures_dir = File.expand_path('../fixtures', __FILE__) + fixtures_dir = File.expand_path('fixtures', __dir__) FileUtils.cp_r("#{fixtures_dir}/spec", destination_root) run_generator diff --git a/rswag-specs/spec/rswag/specs/configuration_spec.rb b/rswag-specs/spec/rswag/specs/configuration_spec.rb index 48fce51..98345d4 100644 --- a/rswag-specs/spec/rswag/specs/configuration_spec.rb +++ b/rswag-specs/spec/rswag/specs/configuration_spec.rb @@ -4,7 +4,6 @@ require 'rswag/specs/configuration' module Rswag module Specs - RSpec.describe Configuration do subject { described_class.new(rspec_config) } diff --git a/rswag-specs/spec/rswag/specs/example_group_helpers_spec.rb b/rswag-specs/spec/rswag/specs/example_group_helpers_spec.rb index 1bbce4f..2112bd1 100644 --- a/rswag-specs/spec/rswag/specs/example_group_helpers_spec.rb +++ b/rswag-specs/spec/rswag/specs/example_group_helpers_spec.rb @@ -4,7 +4,6 @@ require 'rswag/specs/example_group_helpers' module Rswag module Specs - RSpec.describe ExampleGroupHelpers do subject { double('example_group') } @@ -50,12 +49,12 @@ module Rswag it "adds to the 'operation' metadata" do expect(api_metadata[:operation]).to match( - tags: [ 'Blogs', 'Admin' ], + tags: ['Blogs', 'Admin'], description: 'Some description', operationId: 'createBlog', - consumes: [ 'application/json', 'application/xml' ], - produces: [ 'application/json', 'application/xml' ], - schemes: [ 'http', 'https' ], + consumes: ['application/json', 'application/xml'], + produces: ['application/json', 'application/xml'], + schemes: ['http', 'https'], deprecated: true ) end @@ -76,12 +75,12 @@ module Rswag it "adds to the 'operation' metadata" do expect(api_metadata[:operation]).to match( - tags: [ 'Blogs', 'Admin' ], + tags: ['Blogs', 'Admin'], description: 'Some description', operationId: 'createBlog', - consumes: [ 'application/json', 'application/xml' ], - produces: [ 'application/json', 'application/xml' ], - schemes: [ 'http', 'https' ], + consumes: ['application/json', 'application/xml'], + produces: ['application/json', 'application/xml'], + schemes: ['http', 'https'], deprecated: true, security: { api_key: [] } ) @@ -123,14 +122,13 @@ module Rswag # end describe '#parameter(attributes)' do - context "when called at the 'path' level" do before { subject.parameter(name: :blog, in: :body, schema: { type: 'object' }) } let(:api_metadata) { { path_item: {} } } # i.e. operation not defined yet it "adds to the 'path_item parameters' metadata" do expect(api_metadata[:path_item][:parameters]).to match( - [ name: :blog, in: :body, schema: { type: 'object' } ] + [name: :blog, in: :body, schema: { type: 'object' }] ) end end @@ -141,7 +139,7 @@ module Rswag it "adds to the 'operation parameters' metadata" do expect(api_metadata[:operation][:parameters]).to match( - [ name: :blog, in: :body, schema: { type: 'object' } ] + [name: :blog, in: :body, schema: { type: 'object' }] ) end end @@ -152,7 +150,7 @@ module Rswag it "automatically sets the 'required' flag" do expect(api_metadata[:operation][:parameters]).to match( - [ name: :id, in: :path, required: true ] + [name: :id, in: :path, required: true] ) end end @@ -162,7 +160,7 @@ module Rswag let(:api_metadata) { { operation: {} } } 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 diff --git a/rswag-specs/spec/rswag/specs/example_helpers_spec.rb b/rswag-specs/spec/rswag/specs/example_helpers_spec.rb index b4e7be8..52394b4 100644 --- a/rswag-specs/spec/rswag/specs/example_helpers_spec.rb +++ b/rswag-specs/spec/rswag/specs/example_helpers_spec.rb @@ -4,7 +4,6 @@ require 'rswag/specs/example_helpers' module Rswag module Specs - RSpec.describe ExampleHelpers do subject { double('example') } @@ -34,7 +33,7 @@ module Rswag operation: { verb: :put, summary: 'Updates a blog', - consumes: [ 'application/json' ], + consumes: ['application/json'], parameters: [ { name: :blog_id, in: :path, type: 'integer' }, { name: 'id', in: :path, type: 'integer' }, diff --git a/rswag-specs/spec/rswag/specs/request_factory_spec.rb b/rswag-specs/spec/rswag/specs/request_factory_spec.rb index 62a06b3..2f2e5ff 100644 --- a/rswag-specs/spec/rswag/specs/request_factory_spec.rb +++ b/rswag-specs/spec/rswag/specs/request_factory_spec.rb @@ -4,7 +4,6 @@ require 'rswag/specs/request_factory' module Rswag module Specs - RSpec.describe RequestFactory do subject { RequestFactory.new(config) } @@ -55,7 +54,7 @@ module Rswag allow(example).to receive(:q2).and_return('bar') 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') end end @@ -65,40 +64,40 @@ module Rswag metadata[:operation][:parameters] = [ { 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 context 'collectionFormat = csv' do 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') end end context 'collectionFormat = ssv' do 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') end end context 'collectionFormat = tsv' do 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') end end context 'collectionFormat = pipes' do 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') end end context 'collectionFormat = multi' do 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') end end @@ -106,7 +105,7 @@ module Rswag context "'header' parameters" 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') end @@ -129,9 +128,9 @@ module Rswag end end - context "consumes content" do + context 'consumes content' do before do - metadata[:operation][:consumes] = [ 'application/json', 'application/xml' ] + metadata[:operation][:consumes] = ['application/json', 'application/xml'] end context "no 'Content-Type' provided" do @@ -152,18 +151,18 @@ module Rswag context 'JSON payload' 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') end 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 context 'form payload' do before do - metadata[:operation][:consumes] = [ 'multipart/form-data' ] + metadata[:operation][:consumes] = ['multipart/form-data'] metadata[:operation][:parameters] = [ { name: 'f1', in: :formData, type: :string }, { name: 'f2', in: :formData, type: :string } @@ -183,7 +182,7 @@ module Rswag context 'produces content' do before do - metadata[:operation][:produces] = [ 'application/json', 'application/xml' ] + metadata[:operation][:produces] = ['application/json', 'application/xml'] end context "no 'Accept' value provided" do @@ -194,7 +193,7 @@ module Rswag context "explicit 'Accept' value provided" do before do - allow(example).to receive(:'Accept').and_return('application/xml') + allow(example).to receive(:Accept).and_return('application/xml') end it "sets 'HTTP_ACCEPT' header to example value" do @@ -207,7 +206,7 @@ module Rswag context 'swagger 2.0' do before do swagger_doc[:securityDefinitions] = { basic: { type: :basic } } - metadata[:operation][:security] = [ basic: [] ] + metadata[:operation][:security] = [basic: []] allow(example).to receive(:Authorization).and_return('Basic foobar') end @@ -220,7 +219,7 @@ module Rswag let(:swagger_doc) { { openapi: '3.0.1' } } before do swagger_doc[:components] = { securitySchemes: { basic: { type: :basic } } } - metadata[:operation][:security] = [ basic: [] ] + metadata[:operation][:security] = [basic: []] allow(example).to receive(:Authorization).and_return('Basic foobar') end @@ -234,7 +233,7 @@ module Rswag before do allow(ActiveSupport::Deprecation).to receive(:warn) swagger_doc[:securityDefinitions] = { basic: { type: :basic } } - metadata[:operation][:security] = [ basic: [] ] + metadata[:operation][:security] = [basic: []] allow(example).to receive(:Authorization).and_return('Basic foobar') end @@ -254,7 +253,7 @@ module Rswag # swagger_doc[:components] = { securitySchemes: { # 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') end @@ -294,12 +293,12 @@ module Rswag context 'oauth2' do before do - swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: [ 'read:blogs' ] } } + swagger_doc[:securityDefinitions] = { oauth2: { type: :oauth2, scopes: ['read:blogs'] } } ## OA3 # swagger_doc[:components] = { securitySchemes: { # 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') end @@ -319,26 +318,26 @@ module Rswag # basic: { type: :basic }, # 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(:api_key).and_return('foobar') 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[:path]).to eq('/blogs?api_key=foobar') end end - context "path-level parameters" do + context 'path-level parameters' do before do - metadata[:operation][:parameters] = [ { name: 'q1', in: :query, type: :string } ] - metadata[:path_item][:parameters] = [ { name: 'q2', in: :query, type: :string } ] + metadata[:operation][:parameters] = [{ name: 'q1', 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(:q2).and_return('bar') 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') end end @@ -347,7 +346,7 @@ module Rswag context 'swagger 2.0' do before do 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') end @@ -360,7 +359,7 @@ module Rswag let(:swagger_doc) { { openapi: '3.0.1' } } before do 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') end @@ -374,7 +373,7 @@ module Rswag before do allow(ActiveSupport::Deprecation).to receive(:warn) 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') end @@ -396,20 +395,20 @@ module Rswag end end - context "global consumes" do - before { swagger_doc[:consumes] = [ 'application/xml' ] } + context 'global consumes' do + before { swagger_doc[:consumes] = ['application/xml'] } it "defaults 'CONTENT_TYPE' to global value(s)" do expect(request[:headers]).to eq('CONTENT_TYPE' => 'application/xml') end end - context "global security requirements" do + context 'global security requirements' do before do swagger_doc[:securityDefinitions] = { apiKey: { type: :apiKey, name: 'api_key', in: :query } } ## OA3 # 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') end diff --git a/rswag-specs/spec/rswag/specs/response_validator_spec.rb b/rswag-specs/spec/rswag/specs/response_validator_spec.rb index 24882fe..a4c2bc6 100644 --- a/rswag-specs/spec/rswag/specs/response_validator_spec.rb +++ b/rswag-specs/spec/rswag/specs/response_validator_spec.rb @@ -4,7 +4,6 @@ require 'rswag/specs/response_validator' module Rswag module Specs - RSpec.describe ResponseValidator do subject { ResponseValidator.new(config) } @@ -23,7 +22,7 @@ module Rswag schema: { type: :object, properties: { text: { type: :string } }, - required: [ 'text' ] + required: ['text'] } ## OA3 # content: { @@ -49,21 +48,21 @@ module Rswag ) end - context "response matches metadata" do + context 'response matches metadata' do it { expect { call }.to_not raise_error } end - context "response code differs from metadata" do + context 'response code differs from metadata' do before { response.code = '400' } it { expect { call }.to raise_error(/Expected response code/) } end - context "response headers differ from metadata" do + context 'response headers differ from metadata' do before { response.headers = {} } it { expect { call }.to raise_error(/Expected response header/) } end - context "response body differs from metadata" do + context 'response body differs from metadata' do before { response.body = '{"foo":"Some comment"}' } it { expect { call }.to raise_error(/Expected response body/) } end @@ -75,7 +74,7 @@ module Rswag 'blog' => { type: :object, properties: { foo: { type: :string } }, - required: [ 'foo' ] + required: ['foo'] } } metadata[:response][:schema] = { '$ref' => '#/definitions/blog' } @@ -96,7 +95,7 @@ module Rswag 'blog' => { type: :object, properties: { foo: { type: :string } }, - required: [ 'foo' ] + required: ['foo'] } } } @@ -116,7 +115,7 @@ module Rswag 'blog' => { type: :object, properties: { foo: { type: :string } }, - required: [ 'foo' ] + required: ['foo'] } } metadata[:response][:schema] = { '$ref' => '#/definitions/blog' } diff --git a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb index 2be7294..af89503 100644 --- a/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb +++ b/rswag-specs/spec/rswag/specs/swagger_formatter_spec.rb @@ -5,7 +5,6 @@ require 'ostruct' module Rswag module Specs - RSpec.describe SwaggerFormatter do subject { described_class.new(output, config) } @@ -15,7 +14,7 @@ module Rswag end let(:config) { double('config') } 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 before do @@ -69,19 +68,21 @@ module Rswag end context 'with metadata upgrades for 3.0' do - let(:swagger_doc) { { - openapi: '3.0.1', - basePath: '/foo', - schemes: ['http', 'https'], - host: 'api.example.com', - components: { - securitySchemes: { - my_oauth: { - flow: :anything + let(:swagger_doc) do + { + openapi: '3.0.1', + basePath: '/foo', + schemes: ['http', 'https'], + host: 'api.example.com', + components: { + securitySchemes: { + my_oauth: { + flow: :anything + } } } } - } } + end let(:document) { nil } it 'converts query and path params, type: to schema: { type: }' do @@ -128,7 +129,7 @@ module Rswag describe '#stop' 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( 'v1/swagger.json' => { info: { version: 'v1' } }, 'v2/swagger.json' => { info: { version: 'v2' } } @@ -160,7 +161,7 @@ module Rswag end 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 diff --git a/rswag-specs/spec/spec_helper.rb b/rswag-specs/spec/spec_helper.rb index 63504e1..eba5ebe 100644 --- a/rswag-specs/spec/spec_helper.rb +++ b/rswag-specs/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Rails module VERSION MAJOR = 3 diff --git a/rswag-specs/spec/swagger_helper.rb b/rswag-specs/spec/swagger_helper.rb index 330a7a4..434e237 100644 --- a/rswag-specs/spec/swagger_helper.rb +++ b/rswag-specs/spec/swagger_helper.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + # 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