From ff1342d1251f2b2f723b4f2ed0707a56411c5f6c Mon Sep 17 00:00:00 2001 From: Loic Ginoux Date: Sat, 11 Mar 2017 15:38:22 +0100 Subject: [PATCH] failing test custom validator keeping ref to instance var --- spec/grape/api/custom_validations_spec.rb | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/spec/grape/api/custom_validations_spec.rb b/spec/grape/api/custom_validations_spec.rb index f69ec51998..d11aa9c5c4 100644 --- a/spec/grape/api/custom_validations_spec.rb +++ b/spec/grape/api/custom_validations_spec.rb @@ -3,6 +3,54 @@ require 'spec_helper' describe Grape::Validations do + + context "when a validator uses an instance variable" do + before do + module CustomValidationsSpec + module HelperMethods + extend Grape::API::Helpers + def max_ref(req_params) + return @max unless @max.nil? + @max = req_params[:max].to_i + end + end + + class DefaultLength < Grape::Validations::Base + include CustomValidationsSpec::HelperMethods + def validate_param!(attr_name, params) + return if params[attr_name].length <= max_ref(params) + fail Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: "must be at the most #{max_ref(params)} characters long" + end + end + end + end + + subject do + Class.new(Grape::API) do + params do + requires :text, default_length: 140 + requires :max + end + get do + 'bacon' + end + end + end + + def app + subject + end + + it 'between 2 calls, helper inside a validator does not keep old reference of instance variable' do + get '/', text: 'a' * 130, max: 140 + expect(last_response.status).to eq 200 + + get '/', text: 'a' * 130, max: 120 + expect(last_response.status).to eq 400 + end + end + + context 'using a custom length validator' do before do module CustomValidationsSpec