Skip to content

Commit 206d15e

Browse files
committed
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 2f0f7ed commit 206d15e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def initialize(data, logger = nil)
3737
@off_variation = data[:offVariation]
3838
check_variation_range(self, errors, @off_variation, "off variation")
3939
@prerequisites = (data[:prerequisites] || []).map do |prereq_data|
40-
Prerequisite.new(prereq_data, self, errors)
40+
Prerequisite.new(prereq_data, self)
4141
end
4242
@targets = (data[:targets] || []).map do |target_data|
4343
Target.new(target_data, self, errors)
@@ -118,13 +118,12 @@ def to_json(*a)
118118
end
119119

120120
class Prerequisite
121-
def initialize(data, flag, errors_out = nil)
121+
def initialize(data, flag)
122122
@data = data
123123
@key = data[:key]
124124
@variation = data[:variation]
125125
@failure_result = EvaluatorHelpers.evaluation_detail_for_off_variation(flag,
126126
EvaluationReason::prerequisite_failed(@key))
127-
check_variation_range(flag, errors_out, @variation, "prerequisite")
128127
end
129128

130129
# @return [Hash]

spec/impl/evaluator_prereq_spec.rb

Lines changed: 32 additions & 0 deletions
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)