diff --git a/CHANGELOG.md b/CHANGELOG.md index a5cd07963..eb7c5f0d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * [#2572](https://github.com/ruby-grape/grape/pull/2572): Drop support ruby 2.7 and active_support 6.1 - [@ericproulx](https://github.com/ericproulx). +* [#2573](https://github.com/ruby-grape/grape/pull/2573): Clean up deprecated code - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes @@ -1176,3 +1177,4 @@ ### 0.1.0 (2010/11/13) * Initial public release - [@mbleigh](https://github.com/mbleigh). + diff --git a/UPGRADING.md b/UPGRADING.md index c1baa6ae6..8eb728265 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1,6 +1,16 @@ Upgrading Grape =============== +### Upgrading to >= 3.0.0 + +#### Old Deprecations Clean Up + +- `rack_response` has been removed in favor of using `error!`. +- `Grape::Exceptions::MissingGroupType` and `Grape::Exceptions::UnsupportedGroupType` aliases `MissingGroupTypeError and `UnsupportedGroupType` have been removed. +- `Grape::Validations::Base` has been removed in favor of `Grape::Validations::Validators::Base`. + +See [2573](https://github.com/ruby-grape/grape/pull/2573) for more information. + ### Upgrading to >= 2.4.0 #### Grape::Middleware::Auth::Base diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 2d7568afa..93976f948 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -178,24 +178,6 @@ def error!(message, status = nil, additional_headers = nil, backtrace = nil, ori original_exception: original_exception end - # Creates a Rack response based on the provided message, status, and headers. - # The content type in the headers is set to the default content type unless provided. - # The message is HTML-escaped if the content type is 'text/html'. - # - # @param message [String] The content of the response. - # @param status [Integer] The HTTP status code. - # @params headers [Hash] (optional) Headers for the response - # (default: {Rack::CONTENT_TYPE => content_type}). - # - # Returns: - # A Rack::Response object containing the specified message, status, and headers. - # - def rack_response(message, status = 200, headers = { Rack::CONTENT_TYPE => content_type }) - Grape.deprecator.warn('The rack_response method has been deprecated, use error! instead.') - message = Rack::Utils.escape_html(message) if headers[Rack::CONTENT_TYPE] == 'text/html' - Rack::Response.new(Array.wrap(message), Rack::Utils.status_code(status), headers) - end - # Redirect to a new url. # # @param url [String] The url to be redirect. diff --git a/lib/grape/exceptions/missing_group_type.rb b/lib/grape/exceptions/missing_group_type.rb index 9a7d3180a..eac7ddb8d 100644 --- a/lib/grape/exceptions/missing_group_type.rb +++ b/lib/grape/exceptions/missing_group_type.rb @@ -9,5 +9,3 @@ def initialize end end end - -Grape::Exceptions::MissingGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::MissingGroupTypeError', 'Grape::Exceptions::MissingGroupType', Grape.deprecator) diff --git a/lib/grape/exceptions/unsupported_group_type.rb b/lib/grape/exceptions/unsupported_group_type.rb index 4c5e6396a..dff4d38ff 100644 --- a/lib/grape/exceptions/unsupported_group_type.rb +++ b/lib/grape/exceptions/unsupported_group_type.rb @@ -9,5 +9,3 @@ def initialize end end end - -Grape::Exceptions::UnsupportedGroupTypeError = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Grape::Exceptions::UnsupportedGroupTypeError', 'Grape::Exceptions::UnsupportedGroupType', Grape.deprecator) diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index b80664e6f..cb536edba 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -331,30 +331,23 @@ def validates(attrs, validations) doc.type = coerce_type default = validations[:default] - - if (values_hash = validations[:values]).is_a? Hash - values = values_hash[:value] - # NB: excepts is deprecated - excepts = values_hash[:except] - else - values = validations[:values] - end + values = validations[:values].is_a?(Hash) ? validations.dig(:values, :value) : validations[:values] doc.values = values - except_values = options_key?(:except_values, :value, validations) ? validations[:except_values][:value] : validations[:except_values] + except_values = validations[:except_values].is_a?(Hash) ? validations.dig(:except_values, :value) : validations[:except_values] # NB. values and excepts should be nil, Proc, Array, or Range. # Specifically, values should NOT be a Hash # use values or excepts to guess coerce type when stated type is Array - coerce_type = guess_coerce_type(coerce_type, values, except_values, excepts) + coerce_type = guess_coerce_type(coerce_type, values, except_values) # default value should be present in values array, if both exist and are not procs - check_incompatible_option_values(default, values, except_values, excepts) + check_incompatible_option_values(default, values, except_values) # type should be compatible with values array, if both exist - validate_value_coercion(coerce_type, values, except_values, excepts) + validate_value_coercion(coerce_type, values, except_values) doc.document attrs @@ -462,18 +455,14 @@ def guess_coerce_type(coerce_type, *values_list) coerce_type end - def check_incompatible_option_values(default, values, except_values, excepts) + def check_incompatible_option_values(default, values, except_values) return unless default && !default.is_a?(Proc) raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :values, values) if values && !values.is_a?(Proc) && !Array(default).all? { |def_val| values.include?(def_val) } - if except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) } - raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, except_values) - end + return unless except_values && !except_values.is_a?(Proc) && Array(default).any? { |def_val| except_values.include?(def_val) } - return unless excepts && !excepts.is_a?(Proc) - raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, excepts) \ - unless Array(default).none? { |def_val| excepts.include?(def_val) } + raise Grape::Exceptions::IncompatibleOptionValues.new(:default, default, :except, except_values) end def validate(type, options, attrs, doc, opts) diff --git a/lib/grape/validations/validators/base.rb b/lib/grape/validations/validators/base.rb index beaba3502..0704b5715 100644 --- a/lib/grape/validations/validators/base.rb +++ b/lib/grape/validations/validators/base.rb @@ -79,10 +79,3 @@ def fail_fast? end end end - -Grape::Validations::Base = Class.new(Grape::Validations::Validators::Base) do - def self.inherited(*) - Grape.deprecator.warn 'Grape::Validations::Base is deprecated! Use Grape::Validations::Validators::Base instead.' - super - end -end diff --git a/spec/grape/api/custom_validations_spec.rb b/spec/grape/api/custom_validations_spec.rb index 6ed10527a..e8c9cf27c 100644 --- a/spec/grape/api/custom_validations_spec.rb +++ b/spec/grape/api/custom_validations_spec.rb @@ -1,16 +1,6 @@ # frozen_string_literal: true -require 'shared/deprecated_class_examples' - describe Grape::Validations do - describe 'Grape::Validations::Base' do - let(:deprecated_class) do - Class.new(Grape::Validations::Base) - end - - it_behaves_like 'deprecated class' - end - describe 'using a custom length validator' do subject do Class.new(Grape::API) do diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 7467e7f1b..ad5515d05 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -4679,26 +4679,6 @@ def uniqe_id_route end end - context 'rack_response deprecated' do - let(:app) do - Class.new(described_class) do - rescue_from :all do - rack_response('deprecated', 500, 'Content-Type' => 'text/plain') - end - - get 'test' do - raise ArgumentError - end - end - end - - it 'raises a deprecation' do - expect(Grape.deprecator).to receive(:warn).with('The rack_response method has been deprecated, use error! instead.') - get 'test' - expect(last_response.body).to eq('deprecated') - end - end - context 'rescue_from context' do subject { last_response } diff --git a/spec/grape/exceptions/missing_group_type_spec.rb b/spec/grape/exceptions/missing_group_type_spec.rb index 7a6ca5269..e967a1d7a 100644 --- a/spec/grape/exceptions/missing_group_type_spec.rb +++ b/spec/grape/exceptions/missing_group_type_spec.rb @@ -1,17 +1,9 @@ # frozen_string_literal: true -require 'shared/deprecated_class_examples' - RSpec.describe Grape::Exceptions::MissingGroupType do describe '#message' do subject { described_class.new.message } it { is_expected.to include 'group type is required' } end - - describe 'Grape::Exceptions::MissingGroupTypeError' do - let(:deprecated_class) { Grape::Exceptions::MissingGroupTypeError } - - it_behaves_like 'deprecated class' - end end diff --git a/spec/grape/exceptions/unsupported_group_type_spec.rb b/spec/grape/exceptions/unsupported_group_type_spec.rb index b6282ab81..33e62ac28 100644 --- a/spec/grape/exceptions/unsupported_group_type_spec.rb +++ b/spec/grape/exceptions/unsupported_group_type_spec.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'shared/deprecated_class_examples' - RSpec.describe Grape::Exceptions::UnsupportedGroupType do subject { described_class.new } @@ -10,10 +8,4 @@ it { is_expected.to include 'group type must be Array, Hash, JSON or Array[JSON]' } end - - describe 'Grape::Exceptions::UnsupportedGroupTypeError' do - let(:deprecated_class) { Grape::Exceptions::UnsupportedGroupTypeError } - - it_behaves_like 'deprecated class' - end end diff --git a/spec/grape/validations/validators/values_validator_spec.rb b/spec/grape/validations/validators/values_validator_spec.rb index 7ef60a58e..437202ac8 100644 --- a/spec/grape/validations/validators/values_validator_spec.rb +++ b/spec/grape/validations/validators/values_validator_spec.rb @@ -63,21 +63,6 @@ def default_excepts get '/lambda' do { type: params[:type] } end - - params do - requires :type, except_values: { value: ValuesModel.excepts, message: 'value is on exclusions list' }, default: 'default exclude message' - end - get '/exclude/exclude_message' - - params do - requires :type, except_values: { value: -> { ValuesModel.excepts }, message: 'value is on exclusions list' } - end - get '/exclude/lambda/exclude_message' - - params do - requires :type, except_values: { value: ValuesModel.excepts, message: 'default exclude message' } - end - get '/exclude/fallback_message' end params do @@ -106,13 +91,6 @@ def default_excepts { type: params[:type] } end - params do - optional :type, except_values: ValuesModel.excepts, default: 'valid-type2' - end - get '/default/except' do - { type: params[:type] } - end - params do optional :type, values: -> { ValuesModel.values }, default: 'valid-type2' end @@ -188,41 +166,6 @@ def default_excepts end get '/optional_with_required_values' - params do - requires :type, except_values: ValuesModel.excepts - end - get '/except/exclusive' do - { type: params[:type] } - end - - params do - requires :type, type: String, except_values: ValuesModel.excepts - end - get '/except/exclusive/type' do - { type: params[:type] } - end - - params do - requires :type, except_values: ValuesModel.excepts - end - get '/except/exclusive/lambda' do - { type: params[:type] } - end - - params do - requires :type, type: String, except_values: -> { ValuesModel.excepts } - end - get '/except/exclusive/lambda/type' do - { type: params[:type] } - end - - params do - requires :type, type: Integer, except_values: -> { [3, 4, 5] } - end - get '/except/exclusive/lambda/coercion' do - { type: params[:type] } - end - params do requires :type, type: Integer, values: 1..5, except_values: [3] end @@ -324,30 +267,6 @@ def default_excepts end end - context 'with a custom exclude validation message' do - it 'does not allow an invalid value for a parameter' do - get('/custom_message/exclude/exclude_message', type: 'invalid-type1') - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type value is on exclusions list' }.to_json) - end - end - - context 'with a custom exclude validation message' do - it 'does not allow an invalid value for a parameter' do - get('/custom_message/exclude/lambda/exclude_message', type: 'invalid-type1') - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type value is on exclusions list' }.to_json) - end - end - - context 'exclude with a standard custom validation message' do - it 'does not allow an invalid value for a parameter' do - get('/custom_message/exclude/fallback_message', type: 'invalid-type1') - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type default exclude message' }.to_json) - end - end - it 'allows a valid value for a parameter' do get('/', type: 'valid-type1') expect(last_response.status).to eq 200 @@ -390,12 +309,6 @@ def default_excepts expect(last_response.body).to eq({ type: 'valid-type2' }.to_json) end - it 'allows a default value with except' do - get('/default/except') - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 'valid-type2' }.to_json) - end - it 'allows a valid default value' do get('/default/hash/valid') expect(last_response.status).to eq 200 @@ -631,66 +544,6 @@ def app end end - context 'exclusive excepts' do - it 'allows any other value outside excepts' do - get '/except/exclusive', type: 'value' - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 'value' }.to_json) - end - - it 'allows any other value outside excepts when type is included' do - get '/except/exclusive/type', type: 'value' - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 'value' }.to_json) - end - - it 'rejects values that matches except' do - get '/except/exclusive', type: 'invalid-type1' - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json) - end - - it 'rejects an array of values if any of them matches except' do - get '/except/exclusive', type: %w[valid1 valid2 invalid-type1 valid4] - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json) - end - end - - context 'exclusive excepts with lambda' do - it 'allows any other value outside excepts when type is included' do - get '/except/exclusive/lambda/type', type: 'value' - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 'value' }.to_json) - end - - it 'allows any other value outside excepts' do - get '/except/exclusive/lambda', type: 'value' - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 'value' }.to_json) - end - - it 'rejects values that matches except' do - get '/except/exclusive/lambda', type: 'invalid-type1' - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json) - end - end - - context 'exclusive excepts with lambda and coercion' do - it 'allows any other value outside excepts' do - get '/except/exclusive/lambda/coercion', type: '10010000' - expect(last_response.status).to eq 200 - expect(last_response.body).to eq({ type: 10_010_000 }.to_json) - end - - it 'rejects values that matches except' do - get '/except/exclusive/lambda/coercion', type: '3' - expect(last_response.status).to eq 400 - expect(last_response.body).to eq({ error: 'type has a value not allowed' }.to_json) - end - end - context 'with mixed values and excepts' do it 'allows value, but not in except' do get '/mixed/value/except', type: 2 diff --git a/spec/shared/deprecated_class_examples.rb b/spec/shared/deprecated_class_examples.rb deleted file mode 100644 index 7909798ea..000000000 --- a/spec/shared/deprecated_class_examples.rb +++ /dev/null @@ -1,16 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_examples 'deprecated class' do - subject { deprecated_class.new } - - around do |example| - old_deprec_behavior = Grape.deprecator.behavior - Grape.deprecator.behavior = :raise - example.run - Grape.deprecator.behavior = old_deprec_behavior - end - - it 'raises an ActiveSupport::DeprecationException' do - expect { subject }.to raise_error(ActiveSupport::DeprecationException) - end -end