From b2285fc73afe3d213e26d129233809972a276d4a Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Thu, 21 Mar 2024 15:23:50 -0400 Subject: [PATCH] fix: Remove invalid prereq `check_variation_range` check When parsing flag payload information, the SDK attempts to pre-check several failure conditions. One of those conditions was to ensure that a provided prereq has a valid variation index. However, the system cannot actually perform this check at parse time since the prerequisite flag might not have been parsed yet. As this check served only as a nice to have, I have removed it and added a test verifying the prereq behavior still operates as expected. Fixes #260 --- lib/ldclient-rb/impl/model/feature_flag.rb | 5 ++-- spec/impl/evaluator_prereq_spec.rb | 32 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/ldclient-rb/impl/model/feature_flag.rb b/lib/ldclient-rb/impl/model/feature_flag.rb index 34dd1bed..f996bd6b 100644 --- a/lib/ldclient-rb/impl/model/feature_flag.rb +++ b/lib/ldclient-rb/impl/model/feature_flag.rb @@ -33,7 +33,7 @@ def initialize(data, logger = nil) @off_variation = data[:offVariation] check_variation_range(self, errors, @off_variation, "off variation") @prerequisites = (data[:prerequisites] || []).map do |prereq_data| - Prerequisite.new(prereq_data, self, errors) + Prerequisite.new(prereq_data, self) end @targets = (data[:targets] || []).map do |target_data| Target.new(target_data, self, errors) @@ -108,13 +108,12 @@ def to_json(*a) end class Prerequisite - def initialize(data, flag, errors_out = nil) + def initialize(data, flag) @data = data @key = data[:key] @variation = data[:variation] @failure_result = EvaluatorHelpers.evaluation_detail_for_off_variation(flag, EvaluationReason::prerequisite_failed(@key)) - check_variation_range(flag, errors_out, @variation, "prerequisite") end # @return [Hash] diff --git a/spec/impl/evaluator_prereq_spec.rb b/spec/impl/evaluator_prereq_spec.rb index 3440f916..813e5b41 100644 --- a/spec/impl/evaluator_prereq_spec.rb +++ b/spec/impl/evaluator_prereq_spec.rb @@ -42,6 +42,38 @@ module Impl expect(result2.detail).to be result1.detail end + it "returns off variation and event if prereq condition is invalid" do + flag = Flags.from_hash( + { + key: 'feature0', + on: true, + prerequisites: [{ key: 'feature1', variation: 2 }], # there are only 2 variations, so variation index 2 is invalid + fallthrough: { variation: 0 }, + offVariation: 1, + variations: %w[a b c], + version: 1, + } + ) + flag1 = Flags.from_hash( + { + key: 'feature1', + on: true, + fallthrough: { variation: 0 }, + variations: %w[d e], + version: 2, + } + ) + context = LDContext.create({ key: 'x' }) + detail = EvaluationDetail.new('b', 1, EvaluationReason::prerequisite_failed('feature1')) + expected_prereqs = [ + PrerequisiteEvalRecord.new(flag1, flag, EvaluationDetail.new('d', 0, EvaluationReason::fallthrough())), + ] + e = EvaluatorBuilder.new(logger).with_flag(flag1).with_unknown_flag('feature2').build + result = e.evaluate(flag, context) + expect(result.detail).to eq(detail) + expect(result.prereq_evals).to eq(expected_prereqs) + end + it "returns off variation and event if prerequisite of a prerequisite is not found" do flag = Flags.from_hash( {