Skip to content

Commit

Permalink
fix: Remove invalid prereq check_variation_range check (#261)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
keelerm84 authored Mar 28, 2024
1 parent 6cb7a94 commit 960e392
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
5 changes: 2 additions & 3 deletions lib/ldclient-rb/impl/model/feature_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
32 changes: 32 additions & 0 deletions spec/impl/evaluator_prereq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit 960e392

Please sign in to comment.