Skip to content

Commit 960e392

Browse files
authored
fix: Remove invalid prereq check_variation_range check (#261)
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
1 parent 6cb7a94 commit 960e392

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/ldclient-rb/impl/model/feature_flag.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def initialize(data, logger = nil)
3333
@off_variation = data[:offVariation]
3434
check_variation_range(self, errors, @off_variation, "off variation")
3535
@prerequisites = (data[:prerequisites] || []).map do |prereq_data|
36-
Prerequisite.new(prereq_data, self, errors)
36+
Prerequisite.new(prereq_data, self)
3737
end
3838
@targets = (data[:targets] || []).map do |target_data|
3939
Target.new(target_data, self, errors)
@@ -108,13 +108,12 @@ def to_json(*a)
108108
end
109109

110110
class Prerequisite
111-
def initialize(data, flag, errors_out = nil)
111+
def initialize(data, flag)
112112
@data = data
113113
@key = data[:key]
114114
@variation = data[:variation]
115115
@failure_result = EvaluatorHelpers.evaluation_detail_for_off_variation(flag,
116116
EvaluationReason::prerequisite_failed(@key))
117-
check_variation_range(flag, errors_out, @variation, "prerequisite")
118117
end
119118

120119
# @return [Hash]

spec/impl/evaluator_prereq_spec.rb

+32
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@ module Impl
4242
expect(result2.detail).to be result1.detail
4343
end
4444

45+
it "returns off variation and event if prereq condition is invalid" do
46+
flag = Flags.from_hash(
47+
{
48+
key: 'feature0',
49+
on: true,
50+
prerequisites: [{ key: 'feature1', variation: 2 }], # there are only 2 variations, so variation index 2 is invalid
51+
fallthrough: { variation: 0 },
52+
offVariation: 1,
53+
variations: %w[a b c],
54+
version: 1,
55+
}
56+
)
57+
flag1 = Flags.from_hash(
58+
{
59+
key: 'feature1',
60+
on: true,
61+
fallthrough: { variation: 0 },
62+
variations: %w[d e],
63+
version: 2,
64+
}
65+
)
66+
context = LDContext.create({ key: 'x' })
67+
detail = EvaluationDetail.new('b', 1, EvaluationReason::prerequisite_failed('feature1'))
68+
expected_prereqs = [
69+
PrerequisiteEvalRecord.new(flag1, flag, EvaluationDetail.new('d', 0, EvaluationReason::fallthrough())),
70+
]
71+
e = EvaluatorBuilder.new(logger).with_flag(flag1).with_unknown_flag('feature2').build
72+
result = e.evaluate(flag, context)
73+
expect(result.detail).to eq(detail)
74+
expect(result.prereq_evals).to eq(expected_prereqs)
75+
end
76+
4577
it "returns off variation and event if prerequisite of a prerequisite is not found" do
4678
flag = Flags.from_hash(
4779
{

0 commit comments

Comments
 (0)